Migrating Appium Tests to Sauce Labs

Written by dbudim | Published 2022/08/05
Tech Story Tags: debugging | cloud | test-automation | automated-testing | appium | sause-labs | appium-tests | coding

TLDRIn-house infrastructure is one of the most significant challenges in mobile test automation. To run tests on Sauce Labs, replace the host of the in-house hub with a Sauce-managed hub and add some additional capabilities using platform configurator. You can find all needed options on Test Configuration Options page in the official documentation. After the session starts your test on the ResultTest page by name and build number.via the TL;DR App

Preparing the infrastructure is one of the most significant challenges in mobile test automation.

It's an exciting engineering task to build a scalable and reliable environment with emulators or real devices. Previously I’ve shared my experience of creating in-house infrastructure in the topic Mobile Infra from Scratch.

Own infrastructure has a lot of benefits, but if we are talking about huge scaling, it makes sense to think about cloud solutions. Among many providers, one of my favorites is Sauce Labs. I like this service's ease of migration, ease of use, and runtime features.

Usually, creating a new test session for for Appium tests looks like this:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "15.4");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 13 Pro");
capabilities.setCapability(MobileCapabilityType.APP, APP_PATH);
driver = new IOSDriver(new URL(GRID_HOST), capabilities);

WebDriver constructor requires remote server URL and capabilities. A remote server could be directly an Appium server or Grid connecting different devices or servers.

To run tests on Sauce Labs, enough to replace the host of the in-house hub with a Sauce-managed hub and add some additional capabilities using platform configurator.

Sauce-managed hub URL could be found on the “User Settings” page and usually looks like this:

https://${user}:${access_key}@ondemand.eu-central-1.saucelabs.com:443/wd/hub

The next is capabilities configuration. Follow Automated → Platform Configurator on left sidebar and get caps for needed environment:

Here are present souce:options which are specific capabilities for Sauce Labs platform. They should be merged with general WebDriver capabilities that are typically used for your tests.

MutableCapabilities sauceOptions = new MutableCapabilities();
sauceOptions.setCapability("appiumVersion", "1.22.3");
sauceOptions.setCapability("build", "<your build id>");
sauceOptions.setCapability("name", "<your test name>");
capabilities.setCapability("sauce:options", sauceOptions);

You can find all needed options on Test Configuration Options page in the official documentation.

So final code which creates a new session will look like:

DesiredCapabilities capabilities = new DesiredCapabilities();

//Original options used before
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "15.4");
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 13 Simulator");
capabilities.setCapability(MobileCapabilityType.APP, APP_PATH);

//Sauce labs specific options
var sauceOptions = new MutableCapabilities();
sauceOptions.setCapability("appiumVersion", "1.22.3");
sauceOptions.setCapability("build", "001");
sauceOptions.setCapability("name", "My Awesome First Test");

//merging capabilities
capabilities.setCapability("sauce:options", sauceOptions);

driver = new IOSDriver(new URL(SAUSE_LABS_HOST), capabilities);

After the session starts, you can find your test on the "Test Result" page by name and build number, which was provided in capabilities.

One of the greatest features is the session's real-time view - click on test and follow into view.

All information that could be helpful for reporting: logs, video, and screenshots is attached after the session is finished.

What about working in a private network?

A common case is when applications need to have access to the private network of your company, e.g. when you test the compatibility of an old app with a new back-end that is not in production. For this reason, you should use Sauce Connect Proxy which starts in your network and creates a secure tunnel with Sauce infrastructure. This way the application runs in the Sauce cloud and connects to the internal network via this tunnel.

Sauce Connect Proxy Quickstart Guide provides detailed instructions about setup and use. On “TUNNEL PROXIES” page you can find links to binaries and quick start command.

After a successful start, you receive a message in the console "Sauce connect is up.”

This tunnel will be displayed on “TUNNEL PROXIES” with green mark.

To use this tunnel, just add sauce-specific capability tunnelIdentifier and put the tunnel name. After that, all traffic from the launched application will go through Sauce Connect Proxy.

sauceOptions.setCapability("tunnelIdentifier", "awesome_tunnel");

You may need additional tuning of traffic, so my advice is carefully read Sauce Connect Proxy documentation.

E.g., a good way is to set concrete domains that should be proxied, and others go via the public internet to reduce proxy load and traffic amount. It can be done via -tunnel-domains or -t flag.

Also, you might need set domains that do not require SSL resigning with --no-ssl-bump-domain flag. Requests from specified domains will not be re-encrypted.

Conclusion

Sauce Labs presents itself as the world’s most comprehensive Continuous Testing Cloud. It provides numerous combinations of OS/Browsers, mobile emulators & simulators, real devices and always-on scalable infrastructure.

If you think about scaling your current testing processes and reducing infra-support operations, I think it’s a great choice.

I hope this quick overview & guide will be helpful.


Written by dbudim | Software Development Engineer in Test
Published by HackerNoon on 2022/08/05