Based on the usage metrics of an application, clients usually ask us to test their application against most using devices, browsers by its customers. Maintaining all these sets of Operating systems, devices and other test assets will be a burden to both client and company. One solution to overcome the above problem is to use a third party Cloud services like Sauce Labs, Browser Stack etc. In this post will be knowing about Sauce Labs and how we can execute scripts against specific configuration.
Sauce Labs is a cloud platform which provides a comprehensive test infrastructure for automated and manual testing of desktop and mobile applications thus allowing users to run tests in the cloud on different browser platform, operating system and device combinations. For Mobile, Sauce Labs helps in automating Native, Hybrid and Mobile Web Apps across emulators, simulators and real devices.
To run our test scripts in Sauce labs, we have to use Desired Capabilities and Remote WebDriver/AndroidDriver objects where the former one is used to set the commands like in which OS/Browser/Device/Version we have to use and the latter helps in pointing our tests to Sauce labs cloud, for this we need to create an account in Sauce labs.
Steps to get the SauceCloud Url:
- Create a free trial account in Sauce labs from https://saucelabs.com/signup/trial
- Get the access key of the account created from https://saucelabs.com/beta/user-settings as we need to pass Username and AccessKey in the Cloud Url
- Form the cloud Url with the following syntax “http://”+userName+ ” : ” +accessKey+”@ondemand.saucelabs.com:80/wd/hub”
The important pre-requisite to test any hybrid or native mobile application in Sauce Labs is to upload the application you want to test to Sauce Storage (temporary storage area of Sauce labs) using SauceLabs REST API, and then access it for testing by specifying sauce-storage:<UploadedApp> in the application desired capabilities in your test script.
Upload Files in Sauce-Storage:
We will use Curl commands to send App from our local machine to Sauce Storage in the following manner:
- Download the stable curl version from this link https://curl.haxx.se/dlwiz/?type=bin&os=Win32&flav=-&ver=*
- Unzip the folder and set the path variable to /bin
- Enter the following command after setting the above path in command prompt:
Command:
curl -u <sauce_username>:<sauce_access_key> -X POST -H “Content-Type: application/octet-stream” https://saucelabs.com/rest/v1/storage/<sauce_username>/<upload_filename>?overwrite=true –data-binary @/<path/to/your_file_name>
where <upload_filename> refers to the name you want to use for the file under Sauce Storage. For example, Snapdeal.apk and
<path/to/your_file_name> refers to the path where the file is saved on your local system, for example F:\SnapDeal\sdqa\src\test\resources\Snapdeal.apk
Example:
4. We can check for the uploaded app in SauceStorage by using the following command.
Command:
curl -u YOUR_USERNAME:YOUR_ACCESS_KEY https://saucelabs.com/rest/v1/storage/YOUR_USERNAME
Example:
Note: Here Sauce Storage is a temporary storage area where it can store apps for Seven days. After seven days, all assets are cleared from your Sauce Storage and we need to upload it again.
A Sample program to launch SnapDeal App in Saucelabs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class SauceLabsTest { private AndroidDriver driver; private DesiredCapabilities capabilities; private String userName = "Vinaylabs"; // use your userName private String accessKey = "a9930972-239a-441c-9cde-1d84eeeef901"; //use your AccesKey private String url;</code> public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public void setCapabilities(){ capabilities= new DesiredCapabilities(); capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); //Set the Platform capabilities.setCapability(MobileCapabilityType.APP, "sauce-storage:SnapdealOnlineShoppingIndia_6.1.4_apk-dl.com.apk"); //Name of the App you want to work capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT,10); // Set synchronization time capabilities.setCapability("name","First Mobile test in SauceLab"); //Set the name for automation tests capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Google Nexus 7 HD Emulator"); //Device in which scripts should invoke capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION,"4.4"); // Android OS Version setUrl("http://"+userName+ " : " +accessKey+"@ondemand.saucelabs.com:80/wd/hub"); } @Test public void sampleTestToLaunchAppInCloud() throws MalformedURLException{ setCapabilities(); //This method will set the desired capabilities and sauce labs url driver = new AndroidDriver(new URL(getUrl()), capabilities); // Creates the Driver instance driver.quit(); // Kills the driver instance } } |
Tests execution in Sauce Labs will be shown as follows: