Articles

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 http://download.eclipse.org/technology/m2e/releases
  • Click Add…
  • Give a name for the repository, such as:
    m2eclipse
  • 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:
    com.yourcompany.selenium
  • Type in field Artifact Id:
    yourclientprojectname
  • Click: Finish

Edit pom.xml

  • Update JUnit version to 4.9 and use the latest Maven repository release of selenium-java:
junit
junit
4.9
test
  • Add Selenium dependency:
org.seleniumhq.selenium
selenium-java
2.25.0
  • 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 AppTest.java 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:
    WhenSearchingForDrupalUsingGoogleTest
  • 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 java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
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;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WhenSearchingForDrupalUsingGoogleTest {

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

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

@After
public void saveScreenshotAndCloseBrowser() throws IOException {
screenshotHelper.saveScreenshot("screenshot.png");
driver.quit();
}

@Test
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(By.name("q"));
searchField.sendKeys("Drupal!");
searchField.submit();
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=http://www.google.com
  • You should see compilation messages and finally something resembling the following:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
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] BUILD SUCCESS
[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.