Friday, 5 April 2013

Quick Selenium WebDriver TestNG setup

Some Fridays I have no idea how can I squeeze anything out of me other than inappropriate jokes and fantasy stories. However I was curious how one can do a TestNG test using Selenium WebDriver.

I started with Selenium RC long time ago and used it all the time. It's a relatively easy workflow and you don't have to add too much to the system to be able to maintain a proper test suite. The Selenium WebDriver is the new kid on the block - so to say. Instead of injecting JavaScript to browsers - with more or less succes - the WebDriver can handle browsers natively through their automation system. Also it's scalable by using Selenium Grid, can be used with more browsers, can do more actions in the browser and have a better developed ecosystem in general.

It doesn't need a Selenium server to run if you only test in your local environment. However you can use it if you want. I've tried first the PHP scenario. Among many options I chose the Facebook implementation. You can download it and the only extra you need is a runner:


require_once '../PATH_TO_LIB/php-webdriver/__init__.php';

$wd_host = 'http://localhost:4444/wd/hub';
$web_driver = new WebDriver($wd_host);

$session = $web_driver->session('firefox');


You refer to the __init__.php, connect to the running Selenium server, instantiate the driver and you have the browser in your hand now. I was checking out the docs and it seemed it's still not the complete bridge to the WebDriver. So let's see how the Java TestNG version is working.

I used Eclipse IDE with the TestNG plugin installed. Then you need to create the tests. I'm using the Selenium IDE in Firefox with the WebDriver Backed Formatters plugin. When I record a test I export it to Java (TestNG) format. That gives you a java file like:

package com.example.tests;

import junit.framework.TestCase;

import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
import org.testng.annotations.*;

public class Login extends TestCase {

 WebDriver driver;
 Selenium selenium;

 public void startSelenium() {
  driver = new FirefoxDriver();
  selenium = new WebDriverBackedSelenium(driver, "http://localhost/");

 public void stopSelenium() {

 public void testLogin() {"/drupal8/");
  selenium.type("id=edit-name", "admin");
  selenium.type("id=edit-pass", "PASSWORD");"id=edit-submit");
  assertTrue(selenium.isTextPresent("Welcome to Drupal"));"xpath=(//a[contains(text(),'Log out')])[2]");
  assertTrue(selenium.isTextPresent("User login"));


In Eclipse then you create a new project. Add the test files to the appropriate packages. If you want to be pro, organize it around test suites (Selenium IDE can export test suites as well). But Eclipse can run individual tests as well as test suites, so worries if you don't have one.

It will cry aloud because it feels lost a bit. You have to add JUnit and the Selenium bindings. Add all *.jars as referenced libraries. Now it should be all right, you can run the test with the TestNG runner configuration (not the JUnit).

As SeleniumHQ suggests it's better to start all with Maven. I created the project from the pom.xml in InjelliJ but then I realized I'm tired. What I'd like to check how is it possible to connect the tests with data sheets (such as Excel documents) where normal humans can edit test cases and input data. Also the WebDriver - as mention earlier - has implementations on many platforms, such as iOS. So next time maybe I can approach ladies in the bar and show them my test running on the phone. Pretty neat, huh?




  1. Nice article.
    Also there is a cool service for Selenium based CI and cross-browser testing:

  2. Hi Adam,
    Thanks for reming me. I'm following SauceLabs almost for 5 years. Got so bored with the tweets I actually forgot to re-check their site :) Apart from their system it seems to me that testing is as big as the other parts of IT as a whole :P
    Are you using SauceLabs btw?

  3. At Cheppers ( one client asked for tests in SauceLabs. I think there were no problems at all, but at agency level they don't use it as a standard CI solution.

  4. Okay, good to know if works fine :) Thanks Adam for sharing! :)

  5. hi Peter,

    There is more you can do with Selenium IDE now. You may be interested in SeLite. It's a framework for Selenium IDE that allows your test to access (read and write to) a test database (isolated from the database of the tested application).

    SeLite is ideal for Drupal, since all Drupal text content is in DB. Also, Drupal can work with SQLite DB, which is perfect for SeLite - so if (some of) your test deployments can use SQLite, your test data lifecycle would be very easy.

    See and

    1. Hi Peter,
      Awesome, I'm checking it out right now. Thank you for the great suggestion. The db driven methodology sounds a bit odd, but I'll check if it fits our needs.
      Thanks again ;)