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:
$ sauce_config YOUR-SAUCE-USERNAME YOUR-SAUCE-API-KEY
Successfully configured Sauce to use account YOUR-SAUCE-USERNAME
$ sauce_connect
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.'
iPhone:
context:
class: 'FeatureContext'
extensions:
Behat\MinkExtension\Extension:
selenium2:
browser: iphone
wd_host: SAUCE-USERNAME:SAUCE-API-KEY@ondemand.saucelabs.com/wd/hub
capabilities: { platform:Mac 10.,version: 6}
Now, run your test with 'iPhone' profile like this
$ behat -p iPhone
You can see the job running live on Sauce Labs if you visit your account page. Watch the video of this test on Sauce Labs.
iPad Sauce
In order to run test on an iPad, we need to create the 'iPad' profile in the 'behat.yml.' Just paste following code into your 'behat.yml.'
iPad:
context:
class: 'FeatureContext'
extensions:
Behat\MinkExtension\Extension:
selenium2:
browser: iPad
wd_host: SAUCE-USERNAME:SAUCE-API-KEY@ondemand.saucelabs.com/wd/hub
capabilities: { platform: Mac 10.8,version: 6}
Now, run your test with the 'iPhone' profile like this:
$ behat -p iPad
You will see the job running on Sauce Labs. Watch the video of this test on Sauce. Android
Sauce
In order to run test on Android, we need to create an 'Android' profile in the 'behat.yml'. Just paste following code into your 'behat.yml'.
Android:
context:
class: 'FeatureContext'
extensions:
Behat\MinkExtension\Extension:
selenium2:
browser: android
wd_host: SAUCE-USERNAME:SAUCE-API-KEY@ondemand.saucelabs.com/wd/hub
capabilities: { platform:Linux,version:4}
Now, run your test with the 'Android' profile like this:
$ behat -p Android
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. Summary of Steps
Clone Repository
$ git clone git@github.com:Shashikant86/SaucyBehat-iOS-Android.git
$ cd SaucyBehat-iOS-Anroid
Download Composer
$curl http://getcomposer.org/installer | php
$php composer.phar install
Update 'behat.yml' with YOUR-SAUCE-USERNAME YOUR-SAUCE-API-KEY
Run Selenium Server if you wish to tests locally. Don't need to run selenium to run tests on Saucelabs.
Execute build to run all tests using Apache ANT
$ant SaucyBehat
Watch the Video Demo on Youtube.
Final Thoughts
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! LINKS & RESOURCES Source Code on Github : SaucyBehat-iOS-Android Video Demo on Youtube : Behat Installation & iOS-Android -Saucelabs Demo Behat Official Documentation : Behat Docs Sauce Labs Documentation PHP : Getting Started with PHP and Webdriver My Blog : Let's BDD PHP About Me: SHASHIKANT JAGTAP