The official Sauce Labs Blog

Testing Your Mobile Apps with Behat & Sauce (Pt. 2)

October 31st, 2012 by Shashikant Jagtap

This is a guest blog post by Shashikant Jagtap. It is the second post in a 2-part series on configuring Behat with Selenium and Sauce to test mobile devices. Part 1 is here. You can read more about the author here.

Last week, I did a guest post that talked about some of the great new additions to latest release of Behat and how to integrate that with Sauce. In this part of the blog series, we’ll cover mobile test automation in the cloud using Apple Sauce and Android Sauce with Behat. Let’s get started!

Create Your Sauce Account

So far, we have successfully implemented features to run locally in a browser. Now it’s time to add some Sauce to it. First, you’ll need to sign up for a Sauce Labs account. Once you’ve done that, you will see your own personal Sauce USERNAME and SAUCE-ACCESS KEY [SAUCE-API-KEY] on your account page, which we’ll use to configure the Behat tests with Sauce Labs.

Sauce Connect [Composer]

Before we continue, we should figure out with we’re testing internal application. If yes, then we need to use ‘Sauce Connect‘.

During the set up process covered in the last post, we downloaded Sauce Connect and Sauce-Config with Composer. It should be in the ./bin directory. Assuming that you have made Sauce-Config and Sauce Connect global, you can configure your account with Sauce Connect as follow:

This will launch Sauce Connect, which tests your internal applications by reading your local configuration.

You can also start Sauce Connect using the following command once you have downloaded the jar file:

java -jar Sauce-Connect.jar YOUR-SAUCE-USERNAME YOUR-SAUCE-API-KEY

Note: We don’t need Sauce Connect for this tutorial as we are testing live websites.

In order to run Behat Tests on iOS and Android devices, we need to create profiles and run tests with those. Mink Extension has Selenium 2/WebDriver, which prvides different capabilities. Selenium 2/WebDriver for Mink has three options:

Browser: Here you can specify the ‘desiredCapabilities’ of the webdriver eg. iPhone, iPad, Android, Safari, etc.

wd_host: Here you have to provide your Sauce credentials, such as your username and API key e.g SAUCE-USERNAME:SAUCE-API-KEY@ondemand.saucelabs.com/wd/hub

capabilities: Here you can define the platform and version of the device. You can use Sauce Capabilities here e.g { “platform”: “Mac 10.8″, “version”: “6”}

The list of supported Browsers, OS, Devices can be found on SauceLabs’ Available Browsers page. You need to select ‘webdriver’ and ‘php’ code from the dropdown in order to see which combinations are supported for PHP and Behat.

iPhone Sauce:

In order to run test on iPhone, we need to create the ‘iPhone’ profile in the ‘behat.yml.’ Just paste the following code into your ‘behat.yml.’ We need to set the browser parameter to ‘iPhone’ and add the Sauce credentials in the ‘wd_host’ parameter. We also need to select the proper ‘platform’ and ‘version’ combination from ‘capabilities.’

You will see the job running on Sauce Labs. Watch the video of this test on Sauce.

Generating HTML reports

Create an empty directory called ‘report’ to store reports generated by Behat.

$ behat -p iPhone -f html --out report/report.html

This will generate a nice looking HTML report in the ‘reports’ directory that looks like this:

Build Features With Apache ANT

We can run all the feature files in one go using a build tool called ‘Apache ANT‘. You need to install it using the installation guild on ANT documentation. Next step is to create ‘build.xml’ file into your project. Copy build.xml from GitHub. You can now run all the features using the command:

$ ant SaucyBehat

Source Code and Usage on Github

If you have any issues while setting up Behat with Sauce Labs, be sure to check out the source code with build script on GitHub. The source code is in a repository called ‘SaucyBehat-iOS-Android‘, and there is documentation on how to use this repository in the README file.

Mobile test automation, while challenging, is becoming a bit easier and simpler with the help of Behat, Mink Extension and Sauce Labs. Dependency management using Composer makes our life easy while handling third-party repositories and simple configurations in Behat make it possible to run our BDD features on mobile devices with help of WebDriver’s desired capabilities. As of this poisting, we are adding Sauce’s Capabilities to Mink Extension. Stay tuned for more exciting stuff related to Behat and Sauce Labs!