Start your webtests with Selenium2, Maven, TestNG – now

Every time you are playing around with web tests you think: why have I waited so long. Doesn’t matter, you have done it and that’s it what matters.

The last time I used Selenium 1 which is very often used for web tests. Selenium 1 is great, but has a few limitations. It is based around an injected Javascript library – the Selenium core. It works, but starting the browser with the Selenium core is often a little bit tricky. With every browser update you hope that it is still running or hope for the update, which needs to be included in the Maven plugin, is pushed to the Maven mirrors, is really working on all systems, …

But now there is Selenium 2 with the merge of Webdriver. It is no longer a Javascript core, it is a native browser plugin. And now you are much deeper inside the browser. It feels more stable from the first second and it works really well. With the Webdriver part of Selenium 2 there is a new API. But it isn’t very complicated, just different.

I can give you a damn short example which works really well for me and runs through our entire webpage. The great part: it takes screenshots of the complete page (not just the visible part) and stores them in the target folder.

First of all, the Maven pom:

[codesyntax lang="xml"]
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns=""

And it follows the hole logic for the test placed under src/test/java/de/viaboxx/tests/

[codesyntax lang="java"]
<pre>package de.viaboxx.tests;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class SeleniumTest {
    private static final String FOLDER_NAME = "target/screenshots/";
    public void website() throws IOException, InterruptedException {
        FirefoxDriver driver = new FirefoxDriver();
        Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Innovation");
        FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "startpage.png"));
        Assert.assertEquals(driver.findElementByXPath("//h2").getText(), "Team");
        FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "team.png"));
        FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "blog.png"));
        FileUtils.copyFile(driver.getScreenshotAs(OutputType.FILE), new File(FOLDER_NAME + "impressum.png"));

Run it with:

mvn integration-test

Readable, isn’t it. And that’s all – a complete working infrastructure for your web tests. The example works with Firefox, but you can also run headless with HtmlUnit, or make your tests running in IE or Chrome. The great thing: just put the application in your continuous integration server and run it on your defined build agent.

After just one evening I fall in love with the new Selenium 2. Why? Because it works much more stable in kind of browser starting and running. I think it is a great way to go.

Watch the screenshots, just taken from the target folder:

More documentation:

Scroll to Top