Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
Ross Rowe edited this page Aug 13, 2013 · 5 revisions

Description/Features

A plugin for Bamboo that provides pre- and post-build actions connect to Sauce OnDemand for the running of Selenium tests.

NOTE: As of version 1.5.0, the plugin use the 'Atlassian Plugin Framework 2', and should be installed in the BAMBOO_HOME/plugins directory, rather than the BAMBOO/webapp/WEB-INF/lib directory.

Why use Sauce OnDemand?

  • Sauce OnDemand allows you to run your Selenium tests against a Selenium server in the 'cloud'
  • Frees your local infrastructure up
  • View movies of failed tests

For more information, visit the Sauce features page.

Why use the plugin?

The Bamboo Sauce OnDemand plugin includes the following features:

  • Run Selenium tests using SSH Tunnel.
  • Support for continuous builds running in a variety of languages (eg. C#, Ruby, Java, Python)
  • View details and videos of test results within the Bamboo UI

How does the plugin work?

  • The plugin provides the ability to set several options that control the interaction with the Sauce server (eg....)
  • If the 'Enable SSH Tunneling' option is enabled, the plugin will open a SSH Tunnel before the tests run, allowing the Selenium tests to run against the Sauce server.
  • Prior to the build running, the plugin will set several environment variables that contain the values entered into the plugin configuration. The Selenium tests for the project should reference these environment variables
  • After the tests have finished, the SSH tunnel is closed and the environment variables are reset

What do you need to use the plugin?

  • Existing Selenium tests
  • An account with Sauce OnDemand
  • Update Selenium tests to reference environment variables instead of hard coded URLs (see below)
  • Configure plugin to use username/accesskey

There are several mechanisms that your tests can use to reference the variables set in Bamboo:

Selenium Client Factory

The plugin will set a SELENIUM_DRIVER environment variable which will be in the format of 'sauce-ondemand:?...'. The selenium-client-factory libraries will then instantiate a Selenium instance using the parameters contained in the environment variable

import com.saucelabs.selenium.client.factory.SeleniumFactory;
import com.thoughtworks.selenium.Selenium;
...

public class SeleniumClientFactoryTest {
    @Test
    public void selenium() throws Exception {
        Selenium s = SeleniumFactory.create();
        s.open("http://www.google.com/");
        assertEquals("Google", webDriver.getTitle());
        s.stop();
    }
}

The latest version of the selenium-client-factory libraries also support Selenium 2, so you can construct WebDriver instances using the SeleniumFactory.

import com.saucelabs.selenium.client.factory.SeleniumFactory;
import org.openqa.selenium.WebDriver;
...

public class SeleniumClientFactoryTest {
    @Test
    public void webDriver() throws Exception { 
        WebDriver s = SeleniumFactory.createWebDriver();
        s.get("http://www.google.com/");
        assertEquals("Google", webDriver.getTitle());
        s.quit();
    }
}

Using the selenium-sauce-wrapper library

The selenium-sauce-wrapper library will wrap either a Selenium (for Selenium 1) or WebDriver (for Selenium 2). The wrapper classes will log the Sauce Job Id to the System.out, which will be parsed by the Bamboo Sauce plugin.

To use the wrapper classes, either construct a wrapper instance or invoke the static helper method, eg.

@Test
public void selenium1WrappedInstance() throws Exception {
    Selenium selenium = new SauceSeleniumWrapper(new DefaultSelenium(...));
    selenium.start();
    ...
}
@Test
public void selenium1StaticHelper() throws Exception {
    Selenium selenium = new DefaultSelenium(...);
    selenium.start();
    //helper must be invoked *after* selenium has been started
    SauceSeleniumWrapper.dumpSessionId(selenium);
    ...
}
@Test
public void selenium2WrappedInstance() throws Exception {
    Driver driver = new SauceWebDriverWrapper(new RemoteWebDriver(...));
    driver.start();
    ...
}
@Test
public void selenium2StaticHelper() throws Exception {
    Driver driver = new RemoteWebDriver(...);
    SauceWebDriverWrapper.dumpSessionId(driver);
    ...
}

Referencing Environment Variables

The plugin will set a series of environment variables based on the information provided via the Plan Configuration. These environment variables can be explicitly referenced by your unit tests.

The environment variables that the Sauce plugin sets are:

| Variable                 | Description                                                           |
+==================================================================================================+
| SELENIUM_HOST            | The hostname of the selenium server (defaults to                      |
|                          | ondemand.saucelabs.com)                                               |
| SELENIUM_PORT            | The port of the selenium server                                       |
| SELENIUM_DRIVER          | Contains the operating system, version and browser name of the        |
|                          | selected browser, in a format used by Selenium Client Factory         | 
| SELENIUM_PLATFORM        | The operating system for the selected browser                         |
| SELENIUM_VERSION         | The version number of the selected browser                            |
| SELENIUM_BROWSER         | The browser name of the selected browser                              |
| SELENIUM_STARTING_URL    | The initial URL to load when the test begins                          |
| SELENIUM_MAX_DURATION    | The entered maximum duration for the tests                            |
| SELENIUM_IDLE_TIMEOUT    | The entered idle timeout for the tests                                |
| SAUCE_USER_NAME          | The user name used to invoke Sauce Labs                               |
| SAUCE_API_KEY            | The access key for the user used to invoke Sauce Labs                 |
| bamboo_SAUCE_ONDEMAND_BROWSERS  | A JSON-formatted string representing the selected browser(s)   |
| BAMBOO_BUILD_NUMBER      | The Bamboo build number                                               |
+==================================================================================================+

In order for the plugin to correlate Selenium tests with the Bamboo test run, the following code should be added to your Selenium test.

Selenium selenium = new DefaultSelenium(...);
//read the value of the sauce.bamboo.buildNumber environment variable, which will be set by the Bamboo Sauce plugin
//and will contain the Bamboo build number (eg. TST-1)
String bambooData = System.getenv("SAUCE_BAMBOO_BUILDNUMBER");
//invoke setContext, which will send this information to Sauce OnDemand
this.selenium.setContext(bambooData);