Selenium WebDriver - the simplest possible start?

Filed under: Java, Maven, Selenium, Test automation, — Tags: WebDriver — Thomas Sundberg — 2012-10-29

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:

pom.xml

<?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:

src/test/java/AskTest.java

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("http://bing.com/");

        WebElement inputField = browser.findElement(By.id("sb_form_q"));
        inputField.sendKeys("WebDriver");

        WebElement searchButton = browser.findElement(By.id("sb_form_go"));
        searchButton.click();

        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:

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

Resources



(less...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
Executable specification
Gradle
Guice
J2EE
JUnit
Java
Linux
Load testing
Maven
Mockito
Pair programming
PicoContainer
Programming
Public speaking
Quality
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS