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="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 
    &lt;groupId&gt;de.viaboxx.tests&lt;/groupId&gt;
    &lt;artifactId&gt;integrationtest&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
 
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.testng&lt;/groupId&gt;
            &lt;artifactId&gt;testng&lt;/artifactId&gt;
            &lt;version&gt;5.13.1&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
 
        &lt;dependency&gt;
            &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
            &lt;artifactId&gt;selenium&lt;/artifactId&gt;
            &lt;version&gt;2.0a6&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;dependency&gt;
            &lt;groupId&gt;org.seleniumhq.selenium&lt;/groupId&gt;
            &lt;artifactId&gt;selenium-firefox-driver&lt;/artifactId&gt;
            &lt;version&gt;2.0a6&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
 
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;skip&gt;true&lt;/skip&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;integration-test&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;test&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;skip&gt;false&lt;/skip&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;source&gt;1.5&lt;/source&gt;
                    &lt;target&gt;1.5&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;</pre>
[/codesyntax]

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

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

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