Creating and running a simple Selenium WebDriver test

Jani Palsamäki
By Jani Palsamäki

NOTE, 30.11.2012: This post is now updated to use ChromeDriver instead of FirefoxDriver, since current versions of Firefox seem to have issues when it comes to running these tests.

In this blog post, I will demonstrate how to write and run a simple test using Selenium WebDriver.

This example is written in Java, but WebDriver supports many other programming languages (C#, Python and Ruby).

Download and install Chrome and chromedriver

Current versions of Firefox (> 16) seem to have issues when it comes to running these tests. You can try to debug and fix them, but another option is to use Chrome and chromedriver. Install Chrome. Download and unzip chromedriver in some location. Include the location of chromedriver in your path.

Download and install Eclipse

Download and install Eclipse. I chose Eclipse IDE for Java EE Developers, but other distributions should work fine, too.

Apache Maven

Apache Maven should come pre-installed with Mac OS X Snow Leopard and Lion. You can test if you have Maven installed by typing the following command in terminal:

mvn -version

The output should be similar to the following:

Apache Maven 3.0.3 (r1075438; 2011-02-28 19:31:09+0200)
Maven home: /usr/share/maven

I do not cover the installation of Maven in this blog, but there are plenty of tutorials available for all supported operating systems.

Install m2eclipse

Eclipse does not have integrated Maven support out of the box. To add support, I am going to use Maven Integration (m2e).

  • In Eclipse: Help-> Install New Software…
  • Type the following URL in field Work with
  • Click Add…
  • Give a name for the repository, such as:
  • Click OK
  • Select the checkbox Maven Integration for Eclipse
  • Click Next etc. to move forward and choose to restart Eclipse when prompted

Create a new Maven project

  • In Eclipse: File-> New-> Other…-> Maven-> Maven project
  • Click: Next, Next, Next
  • Type in field Group Id:
  • Type in field Artifact Id:
  • Click: Finish

Edit pom.xml

  • Update JUnit version to 4.9 and use the latest Maven repository release of selenium-java:
  • Add Selenium dependency:
  • Eclipse should detect the pom.xml changes automatically, build the project and download required dependencies
  • After the build is finished, there should be selenium*.jarfiles under Maven Dependencies under the Project Explorer pane

Cleaning up

  • Delete example class called under src/test/java

Create a new test class

  • Right-click over package com.yourcompany.selenium.yourclientprojectname under src/test/java and select New-> Class
  • Type in field Name:
  • Click Finish

Implement the test class

Type or paste in the following code:

package com.yourcompany.selenium.yourclientprojectname;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;


import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class WhenSearchingForDrupalUsingGoogleTest {

private String baseUrl;
private WebDriver driver;
private ScreenshotHelper screenshotHelper;

public void openBrowser() {
baseUrl = System.getProperty("webdriver.base.url");
driver = new ChromeDriver();
screenshotHelper = new ScreenshotHelper();

public void saveScreenshotAndCloseBrowser() throws IOException {

public void pageTitleAfterSearchShouldBeginWithDrupal() throws IOException {
assertEquals("The page title should equal Google at the start of the test.", "Google", driver.getTitle());
WebElement searchField = driver.findElement("q"));
assertTrue("The page title should start with the search string after the search.",
(new WebDriverWait(driver, 10)).until(new ExpectedCondition() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("drupal!");

private class ScreenshotHelper {

public void saveScreenshot(String screenshotFileName) throws IOException {
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(screenshotFileName));

Run the test

  • Using terminal, navigate to your project folder under your workspace folder
  • Type in command:
    mvn clean test -Dwebdriver.base.url=
  • You should see compilation messages and finally something resembling the following:
Running com.yourcompany.selenium.yourclientprojectname.WhenSearchingForDrupalUsingGoogleTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.95 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

If everything went well, there should exist a file named screenshot.png at the root of your project folder. If there were errors, you can go and find more information under target/surefire-reports.