Executing mobile Automation test cases on Sauce Labs cloud

Hi Friends,

In my last post, we saw how can we setup different mobile platforms to execute mobile test cases. Here we will see how can we integrate our execution environment to Sauce Labs i.e executing the test cases on sauce labs cloud.

A brief introduction about Sauce Labs:

Sauce Labs is a cloud platform which allows users to run tests in the cloud on more than 700 different browser platform, operating system and device combinations, providing a comprehensive test infrastructure for automated and manual testing of desktop and mobile applications using Selenium, Appium and JavaScript unit testing frameworks.

In easy words, you do not have to setup your own infrastructure with various devices, OS and browser to run test cases. Buy a sauce lab subscription and you are all set to go.

Now let us see how can we execute our mobile test cases on Sauce labs cloud.

The very first step for executing our test cases on Sauce labs is to create Sauce lab account. Sauce labs provides a 14 days free trial to explore its various features. Lets create a free account first.

Steps to create a Free Trial account on Sauce Labs

1. Go to http://saucelabs.com

2. Click on the Free trial button at the top right corner.

3. Fill in all the details on following screen.

4. Click on the Create account button.

5. An account verification mail will be sent to your email id. Click on the link provided in the email to confirm the sign up.

6. Now click on Sign in button from top right corner.

7. Sign in with the newly created account details.

8. After successful login click on the arrow next to your name at the top right corner and click on My account link.

9. Scroll down a bit and click on Show button corresponding to the Access Key.

10. Enter your password in the prompt and click on the Authorize button.

11. Your Access Key will be displayed. Copy and keep it aside for further usage.

Now we are done with setting up the free account on Sauce lab and got our Access Key (authentication token), next step is to upload our apk(for Android), ipa/app(for IOS) to the Sauce lab cloud.

Command to upload Android test application (apk file) on Sauce Labs

curl -u <saucelabUserName>:<saucelabAccessKey> -X POST -H “Content-Type: application/octet-stream” https://saucelabs.com/rest/v1/storage/<saucelabUserName>/<name of app file>?overwrite=true –data-binary @<absolute local path of app file>

Following parameter need to be changed before executing the above command.

saucelabUserName — username/email to access sauce lab account.

saucelabAccessKey — Access key associated with your Sauce Lab account.

name of app file — name of the apk file (for Android).

absolute local path of app file — absolute path of your apk file (the actual location of the file on the system’s hard drive) eg: /Users/Documents/testApps/testapp.apk

I have download one sample apk file to demonstrate how we can execute test cases for Android native platform on Sauce labs. The app name is ApiDemos-debug.apk and it is placed in my system’s Downloads folder. You can download it from here. download ApiDemos-debug.apk

Now considering your saucelabUserName is testuser and your saucelabAccessKey is testaccesskey, all required parameters should look like following.

saucelabUserName — testuser

saucelabAccessKey– testaccesskey

name of app file — ApiDemos-debug.apk

absolute local path of app file with file name — /Users/ngoyal/Downloads/ApiDemos-debug.apk

Your command should be like this now.

curl -u testuser: testaccesskey -X POST -H “Content-Type: application/octet-stream” https://saucelabs.com/rest/v1/storage/testuser/ApiDemos-debug.apk?overwrite=true –data-binary @/Users/ngoyal/Downloads/ApiDemos-debug.apk

Now open Terminal/CommandPrompt and execute the above command. This may take few seconds to upload your file on the Sauce Labs cloud. After successful upload you should see something like this in the Terminal.

Note – By any chance, If you see the “size” as 0 that means your file is not uploaded on the Sauce labs cloud. Check the command and run it again ensuring all parameters are configured correctly in the command.

After uploading test app (ApiDemos-debug.apk) on the Sauce labs successfully, we will start a virtual device with specific configuration on the Sauce labs cloud manually using the appium server to ensure if all our capabilities are correct.

Steps to start a virtual device manually on the Sauce Labs

  1. Start the appium server.

2. Once the server is started, click on the Search icon (the first icon at the top right corner)

 3. Now click on the Sauce Labs tab from the next screen and enter your Sauce Username and Access key

4. Now click on Desired Capabilities and add the following capabilities one by one by clicking on + icon

Note – the value of ‘app’ capability should be sauce-storage:<your test app name>

5. Now click on Start Session button.

6. A rotating loader should be displayed on the screen as following.

7. Now go to http://saucelabs.com, sign in with your account. Go to Dashboard and click on Automated Tests.

8. You will see a job running with name “Unnamed job with c66ob………..”

9. Click on the job, you will see a message “Loading Live video”. This may take some time to launch the live video of your running test.

10. After few seconds you will be able to see the live video of your test running on the device (6.0 in our case) that you asked for.

This means all our configurations for Android device is perfect and our test is launched on the virtual device. Now lets see how can we do it for IOS platforms.

Command to upload IOS test application (ipa/app file) on Sauce Labs

curl -u <saucelabUserName>:<saucelabAccessKey> -X POST -H “Content-Type: application/octet-stream” https://saucelabs.com/rest/v1/storage/<saucelabUserName>/<name of app file>?overwrite=true –data-binary @<absolute local path of app file>

The command is same as we used to upload the Android apk but one thing that should be kept in mind while uploading the IOS app is “the app should be in zip format“.  Yes, you read it correct. You have to compress your app to zip format and configure the app name and absolute path accordingly.

Following parameter need to be changed before executing the above command.

saucelabUserName — username/email to access sauce lab account.

saucelabAccessKey — Access key associated with your Sauce Lab account.

name of app file — name of the ipa/app file in zip format(for IOS)

absolute local path of app file — absolute path of your apk or ipa/app file in zip format (the actual location of the file on the system’s hard drive) eg: /Users/Documents/testApps/testiosapp.zip

I have download one sample app file to demonstrate how we can execute test cases for IOS platform on Sauce labs. The app name is UICatalog.app and it is placed in my system’s Downloads folder.

After converting it into zip it becomes UICatalog.zip

Now considering your saucelabUserName is testuser and your saucelabAccessKey is testaccesskey, we have all required parameters as following.

saucelabUserName — testuser

saucelabAccessKey– testaccesskey

name of app file –UICatalog.zip

absolute local path of app file with file name — /Users/ngoyal/Downloads/UICatalog.zip

Your command should look like this now.

curl -u testuser: testaccesskey -X POST -H “Content-Type: application/octet-stream” https://saucelabs.com/rest/v1/storage/testuser/UICatalog.zip?overwrite=true –data-binary @/Users/ngoyal/Downloads/UICatalog.zip

Now open Terminal/CommandPrompt and execute the above command. This may take few seconds to upload your file on the Sauce Labs cloud. After successful upload you should see something like this in the Terminal

By any chance, If you see the “size” as 0 that means your file is not uploaded on the Sauce labs cloud. Check the command and run it again ensuring all parameters are configured correctly in the command.

After uploading test app (UICatalog.app) on the Sauce labs successfully, we will start a virtual device with specific configuration on the Sauce labs cloud manually using the appium server.

Follow the same steps as explained for Android but use the following capabilities to start the IOS virtual device.

After clicking on the Start Session button, you can go to Sauce labs and see the live execution happening on the virtual device (IOS Simulator)

By this, we are done with setting up and executing our test on Android Emulator and IOS Simulator respectively manually.

Now we will see how can we do that programatically. We just have to use two additional steps other than we did in manual setup and we are all set 🙂

Set following as environmental variables in your system.

SAUCE_USERNAME

SAUCE_ACCESS_KEY

If you are a mac user, you can set these environmental variables as following:

export SAUCE_USERNAME=<your sauce lab user name>
export SAUCE_ACCESS_KEY=<your sauce lab access key>

Launching and Running the Android Native test cases on Sauce labs programatically

Following capabilities can be used to run Android native test cases on Sauce labs.

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “6.0”);

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);

capabilities.setCapability(MobileCapabilityType.APP, “sauce-storage:<your apk file name>”);

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,“Android Emulator”);

URL url = new URL(“https://<saucelab username>:<saucelab access key>@ondemand.saucelabs.com:443/wd/hub“);

AppiumDriver driver = new AndroidDriver(url,capabilities);

These capabilities will launch the apk file on virtual device on Sauce labs. Further you can write your test cases to be executed.

Note – Platform version and the device name may vary based on the device to be configured

 

Launching and Running the Android Web test cases on Sauce labs programatically

Following capabilities can be used to run Android web test cases on Sauce labs.

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “6.0”);

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,“Android Emulator”);

capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,“Chrome”);

URL url = new URL(“https://<saucelab username>:<saucelab access key>@ondemand.saucelabs.com:443/wd/hub“);

AppiumDriver driver = new AndroidDriver(url,capabilities);

These capabilities will launch the virtual device on Sauce labs with Chrome browser opened. Further you can write your test cases to be executed.

Note – Platform version and the device name may vary based on the device to be configured.

 

Launching and Running the IOS Native test cases on Sauce labs programatically

Following capabilities can be used to run IOS native test cases on Sauce labs.

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “10.2”);

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “ios”);

capabilities.setCapability(MobileCapabilityType.APP, “sauce-storage:<your zip file name>”);

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,“iPhone 6 Simulator”);

URL url = new URL(“https://<saucelab username>:<saucelab access key>@ondemand.saucelabs.com:443/wd/hub“);

AppiumDriver driver = new AndroidDriver(url,capabilities);

Note – Platform version may change based on mac version being used and the device name may vary based on the device to be configured.

 

Launching and Running the IOS Web test cases on Sauce labs programatically

Following capabilities can be used to run IOS web test cases on Sauce labs.

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, “10.2”);

capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, “ios”);

capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,“iPhone 6 Simulator”);

capabilities.setCapability(MobileCapabilityType.BROWSER_NAME,“Safari”);

URL url = new URL(“https://<saucelab username>:<saucelab access key>@ondemand.saucelabs.com:443/wd/hub“);

AppiumDriver driver = new AndroidDriver(url,capabilities);

Note – Platform version may change based on mac version being used and the device name may vary based on the device to be configured.

Please refer the following link to check various capabilities required for different platforms and versions.

https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#

I hope this post will help you guys setup execution environment on Sauce labs for mobile test cases. Please do comment in case you feel any difficulty while setting this up.

Thank You!


 

Leave a Reply

Your email address will not be published. Required fields are marked *