January 17, 2013

How to Use Selenium in Continuous Integration Testing

Selenium provides web developers with a suite of tools to automate web browsers across many platforms. At 3Pillar, we use Selenium in conjunction with continuous integration to ensure web applications are automatically tested via scripts as opposed to manually.


Before we start we will need the following installed on our local machine: Firefox (at least version 14), Java JDK (preferably the latest version: 7), Maven 3 (Maven download & setup tutorial: http://www.avajava.com/tutorials/lessons/what-is-maven-and-how-do-i-install-it.html?page=1). Maven is a tool that can now be used for building and managing any Java-based project.

Setting up a Selenium project

First we will create a folder for our Selenium project. In it we will place a pom.xml file containing the following structure:


                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Now open a command line console, navigate to the folder where the pom.xml is and type the following command. This will download Selenium and all the needed dependencies, and will add them to the project.

mvn clean install

Importing the project in the development environment

We will demonstrate how to import the project in Eclipse. Importing it in other IDEs is similar. Before importing, make sure you have the Maven for Eclipse plug-in installed. You can download it dirrectly from the Eclipse marketplace.

Maven for Eclipse

After this, go to File > Import. From the filters, choose Maven > Existing Maven Projects. Next, browse to the folder where our pom.xml file resides, and set it as a root directory. Eclipse should detect the project and display the pom file. Click Finish.

Maven for Eclipse Image

Now we need to create a package where our tests will be placed. The maven structure is: src.test.java. Make sure you stick to this naming up to the java level, or else the builder will not identify your test files.

test java

After the package is created, add src in the build path. Right click on the project name (in this example seleniumTrainingCI), choose Properties > Java Build Path. Click add and choose the src folder.

Java Build Path

First test

Create a new test class in the src.test.java package. The class name must contain the word Test, or else Maven will not consider it a test class. The first test is the one presented in the seleniumhq.org documentation. The content of the java file is the following:

package test.java;
import static junit.framework.Assert.assertTrue;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class TestLocalRun {
     private WebDriver driver;
     private WebDriverWait wait;
      public void setUp() {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 10);
      public void tearDown() throws IOException {
      public void pageTitleAfterSearchShouldBeginWithDrupal() throws IOException {
        WebElement searchField = driver.findElement(By.name("q"));
        assertTrue("The page title should start with the search string after the search.",
            wait.until(new ExpectedCondition() {
              public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith("drupal!");

Now let's run this thing! Right-click anywhere in the editor window, Run As > JUnit Test. This should open a Firefox window, run the test and publish the result in Eclipse.

TestLocalRun Java Image


Creating a Git repository

Now we need a remote place to keep our stuff; we need this because for several reasons, one of them being that we want to run the code in CI. It needs to be able to download it from somewhere. This is where GitHub (or Git for short) comes in place. First you will need a Git account. It's free to create, so go ahead. (When creating tests for the software you dev team is creating, it is probable that you will need a repository in your organization, not in your personal account. Your local IT support / teammates will help you with this).

Login and click the Create a new repo button located in the top right-hand corner. Type a title in the Repository name field, select Public (this is the only option available for the free accounts) and click Create repository.

Now we need to upload our local project folder on Github. To do this open a shell and CD to the folder where the previously created pom.xml file is located. You will need to type in the following commands, one row at a time. Replace account-name and repository-name with your Git username and the title of the repository we've just created online.

git init
git status
git add *
git commit -m "first commit"
git remote add origin https://github.com/[account-name]/[repository-name].git
git push -u origin master

Creating a CI plan

We will demonstrate how a plan is created in Bamboo, but other CI systems (ie: Jenkins) follow roughly the same steps.

Click the Create Plan button in the top right-hand corner > Create a New Plan. Choose the project where you want your plan to be created in. Type a plan name and key (only capital letters and numbers here). For Source Repository choose Git, type the Remote Repository URL (the same URL used with the git remote add command in the previous section). For Build Strategy just choose Manual for now. Click Configure Tasks.

The next step is to define a Task (the build command that will compile our source files and run the tests). Click Add Task and choolse Maven 3.x. Type a suggestive Task Description (ie: run tests). At the bottom of the page there is a section called "Where should Bamboo look for the test result files?". Here choose Specify custom results directories; the default given location(**/target/surefire-reports/*.xml) should work. Click Save.

Maven Bamboo Task Image

On the next screen choose "Yes please!" when asked to enable the plan, and click Create. Navigate to the Project you created your plan in and open it, because there is one last configuration we need to do. In the top right corner, click Actions > Configure Plan.

Configure Build Plan Summary

Select Stages > Default Job > Requirements. Make that your build will only be triggered on machines that have all the needed capabilities. Bamboo agents need the following requirements. Your machines might need other requirements, but you get the picture.

Requirements Image

Now let's run the build plan! Click Run > Run Plan

Run Summary Image

After the build is finished, you will see the status (Build Sumary tab) and the test results (Tests tab).