Thomas Sundberg

October 29, 2012

Selenium WebDriver – the simplest possible start?

Filed under: Automation, Java, Maven, Selenium, Test Automation — Tags: , , , — Thomas Sundberg @ 13:27

This post has been migrated to

Getting started with Selenium WebDriver may be an issue. You must write some code and get the code running. I have created what I think is the smallest possible solution that could work. It consists of two files, a project definition and the actual test.

You will need to have Java and Maven installed. I will not tell you how this should be done, it depends on your environment and operating system.

Lets start with a project definition for Maven that will download the dependencies needed and execute a test. The simplest possible Maven pom that will work is this:


<?xml version="1.0" encoding="UTF-8"?> <project> <modelVersion>4.0.0</modelVersion> <groupId>se.somath</groupId> <artifactId>web-driver-example</artifactId> <version>1.0.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.25.0</version> </dependency> </dependencies> </project>

The two most important things here are the two dependencies. The first one will include JUnit so a test can be written. The second one will include WebDriver so a browser can be started and a web page can be interacted with.

A test that will connect to a web site and execute something may look like this:


import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class AskTest { @Test public void shouldAskGoogleForWebDriver() { WebDriver browser = new FirefoxDriver(); browser.get(""); WebElement inputField = browser.findElement("sb_form_q")); inputField.sendKeys("WebDriver"); WebElement searchButton = browser.findElement("sb_form_go"));; browser.close(); } }

To execute this test, you need to execute Maven:

mvn test

All dependencies you need will be downloaded. A Firefox should be launched and Bing should be searched for WebDriver.

The files has to live in this structure:

|-- pom.xml
`-- src
    `-- test
        `-- java



  1. Thank you. It was brief, simple and to the point.

    Comment by Anonymous — May 23, 2013 @ 00:15

    • I am glad you liked it.


      Comment by Thomas Sundberg — May 26, 2013 @ 22:01

      • I am getting a problem though. Once I rename any folders in “src/test/java/”, test doesn’t get run. I mean, if I name “src/tests/java/”, it won’t run. Then I rename “src/test/asdf/”, it won’t run either.
        So is it critical to have exact wording for “test” and “java” folder? How come?

        Comment by Rod — May 28, 2013 @ 23:28

      • Hi!

        The folder structure is critical. The reason is that I use Maven to build and execute the project.
        Maven is about convention over configuration. The convention is that all source code should live in the directory src. The production code should live in main and test code should live in test. Any Java code should live in java. If you organize you Java code in packages, then will the Java directory be the package root and you need to add the proper directories in the Java directory to reflect your packages.


        Comment by Thomas Sundberg — May 29, 2013 @ 06:43

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: