Integration for Selenium testing running with your continuous integration builds with Sauce OnDemand and Sauce Connect is made easy through the plugins we've developed for Bamboo and Jenkins. These plugins allow you to specify the browser to be used by your tests and launch Sauce Connect prior to the running of your tests. They set several environment variables that include the settings for your build, and your tests will then need to reference these environment variables as part of the Selenium setup. However, explicitly referencing these variables can make your test code cluttered and it can be cumbersome to get the tests structured so that they work against either a local browser or Sauce OnDemand. Rather than updating your code to explicitly reference the environment variables the plugins set, you can instead use the Selenium Client Factory library. This library will read the environment variables set by the CI plugin, and instantiate a Selenium/WebDriver instance that will run your tests against Sauce OnDemand. Selenium Client Factory is a Java library, which can be incorporated into your tests by referencing the jar file or if you're using Maven, by including the following dependency:
<dependency> <groupId>com.saucelabs.selenium</groupId> <artifactId>selenium-client-factory</artifactId> <version>2.1</version> <scope>test</scope> </dependency>
While this is great for Java applications, Selenium and the CI tools themselves support a number of languages. One of our users has developed a Python implementation of the Selenium Client Factory code. The factory object reads environments variables setup by the Bamboo plugin and creates a remote Sauce OnDemand session accordingly. If the Sauce-specific environment variables aren't set, the library will create a local Selenium configuration. In addition, the factory will output the Sauce OnDemand Session Id to the build output, and the Sauce CI plugins will parse this id and associate the Sauce Job with the CI build. - The Selenium Client Factory library makes integrating your tests with Sauce OnDemand and your continuous integration build tool easy - you can configure your local tests to run against a local browser, and use your CI environment to access Sauce OnDemand, all via properties and without having to change any code. To use Selenium Client Factory under Python, just instantiate Selenium via:
from SeleniumFactory import *
#For selenium 2 webDriver: webDriver = SeleniumFactory().createWebDriver()
#For selenium 1 RC: browser = SeleniumFactory().create()
The code for the Java and Python implementations is open source, so if you'd like to implement the Selenium Client Factory in your language of choice, either take a look at the code and have a go, or let us know and we can take a look at it.