This is a short guide to getting started with Selenium - an automated website testing library.

What you will learn

You will learn how to automatically test a simple web form using Selenium. Examples will be created using Java and Maven. If you would like to follow along with this tutorial, you can download this project from github (look for the package called selenium1).

What is Selenium?

Selenium is a library for interacting with a web browser. It enables you to simulate how users may use your site, like filling up forms for example. What is important about Selenium - it can test JavaScript code. You can open up a browser of your choice and enter any site, even with advanced scripting. In the next sections we will dive into details how to do it.

Maven Dependency

First you need to add a dependency to the pom.xml:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.15.0</version>
</dependency>

Creating a WebDriver

Next you should create a WebDriver object. WebDriver is an interface representing a general browser. For specific browsers you have to use concrete implementations. Currently supported out-of-the-box are:

  • Chrome
  • Internet Explorer
  • Firefox
  • Opera

For starting Firefox you would write:

WebDriver driver = new FirefoxDriver();

To open a specific page use the get method:

driver.get("http://google.com");

Simple Scenario

Supposedly, we have a form like:

<form>
    <label>User:</label>
    <input name="user" type="text" />

    <label>Password:</label>
    <input name="password" type="password" />

    <input id="login" type="submit" value="Log in" />
</form>

To mimic a user logging into our application we could use this code:

WebElement user = driver.findElement(By.name("user"));
user.sendKeys("Admin");

WebElement password = driver.findElement(By.name("password"));
password.sendKeys("HackMe!");

WebElement login = driver.findElement(By.id("login"));
login.click();

Three important things happen here:

  • entering text into fields using sendKeys method
  • clicking the "Log in" button with click method
  • looking up web elements in the page structure (in the DOM model to be specific)

Entering text and clicking is quite straightforward, however finding web elements can be quite complex. You can specify search criteria by:

  • By.id(String id)
  • By.name(String name)
  • By.tagName(String tagName)
  • By.className(String className)
  • By.linkText(String linkText) - will search for the "a" HTML element with the given text
  • By.partialLinkText(String linkText) - same as above, but even partial match will succeed
  • By.xpath(String xpathExpression)
  • By.cssSelector(String selector)

For simple look ups, choice of a specific method will be quite obvious. Most of the time you will use By.id, By.name or By.className.

For the non-trivial cases you will have to choose between xpath and css selectors. It is best to decide based on your knowledge and complexity of the result query. Sometimes xpath will be simpler, sometimes a css selector will work out better.

The End

Here ends this quick tour on Selenium basics. In further articles I will try to expand the topic with more complex scenarios.