Selenium is probably the most popular web functional testing automation tool out there. Functional testing means testing your application as if you were a user (clicking links, entering information in fields, etc…). And thanks to selenium this can be automated.

Recently selenium released a new version (2) that is basically a merge with another project called WebDriver. This merge provides developers and testers with a very neat Object Oriented interface to interact with browsers easily from Java.

In this post I am going to explain my first successful experience with Selenium 2 / Webdriver (I had some unsuccessful experiences in the past). I couldn’t have made this post without the great help of http://www.qaautomation.net/?p=263, so thanks a lot to qaautomation.net for their awesome post.

Getting the necessary stuff

There are some things you need to download in order to compile and run your functional tests with selenium 2

JDK. You can get this from oracle or if you have apt-get you can simply do:

1
apt-get install default-jdk

Ant. You can get this from apache or with apt-get:

1
apt-get install ant

Selenium 2 (Selenium Server). You can get the jar file from selenium downloads page. Current version is 2.19.0 but download the latest available by the time you read this.

Creating the Java project

To be able to build and run our test we need to create a Java project. For doing this we need first a folder for our project, I will use this one: /home/adrian/myproject/. And now we need to create this folder structure inside that folder:

1
2
3
4
5
- build
- java
    - classes
    - src
- lib

Later we will need a file to automate our build. The name of that file needs to be build.xml and it should live in the root of your project (/home/adrian/myproject/build.xml):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<project basedir="." name="Test Automation">
    <property name="src.dir" value="${basedir}/java/src"/>
    <property name="classes.dir" value="${basedir}/java/classes/main"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="build.dir" value="${basedir}/build"/>
    <property name="testautomation.jar" value="${build.dir}/testautomation.jar"/>

    <path id="testautomation.classpath">
        <file file="${testautomation.jar}"/>
        <fileset dir="${lib.dir}">
            <include name="*.jar" />
        </fileset>
    </path>

    <target name="build" description="sets up the environment for test execution">
        <mkdir dir="${classes.dir}"/>
        <mkdir dir="${build.dir}"/>
        <javac debug="true"
              srcdir="${src.dir}"
              destdir="${classes.dir}"
              includeAntRuntime="false"
              classpathref="testautomation.classpath"/>
        <jar basedir="${classes.dir}" jarfile="${testautomation.jar}"/>
    </target>

    <target name="run-example" description="run command-line example">
        <java classname="net.qaautomation.examples.${example}"
               failonerror="true"
               classpathref="testautomation.classpath"/>
    </target>
</project>

Now move the .jar file you downloaded from selenium site to your projects lib folder (/home/adrian/myproject/lib/)

Creating the test

I am going to use the same example from qaautomation.net’s post. We need to create a file named GoogleSearch.java in our src folder (/home/adrian/myproject/java/src/) with this content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package net.qaautomation.examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Search Google example.
 *
 * @author Rahul
 */
public class GoogleSearch {
    static WebDriver driver;
    static Wait<WebDriver> wait;

    public static void main(String[] args) {
        driver = new FirefoxDriver();
        wait = new WebDriverWait(driver, 30);
        driver.get("http://www.google.com/");

        boolean result;
        try {
            result = firstPageContainsQAANet();
        } catch(Exception e) {
            e.printStackTrace();
            result = false;
        } finally {
            driver.close();
        }

        System.out.println("Test " + (result? "passed." : "failed."));
        if (!result) {
            System.exit(1);
        }
    }

    private static boolean firstPageContainsQAANet() {
        //type search query
        driver.findElement(By.name("q")).sendKeys("qa automation\n");

        // click search
        driver.findElement(By.name("btnG")).click();

        // Wait for search to complete
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver webDriver) {
                System.out.println("Searching ...");
                return webDriver.findElement(By.id("resultStats")) != null;
            }
        });

        // Look for QAAutomation.net in the results
        return driver.findElement(By.tagName("body")).getText().contains("qaautomation.net");
    }
}

Running the test

Thanks to ant, running and building the project and running the test is very simple. First we need to build:

1
ant build

Now we can use a rule (run-example) from our build.xml file to run the tests:

1
ant run-example -Dexample=GoogleSearch

The test should open firefox, do a google search and then close firefox again. Now you can create other selenium tests and add them to your src folder.

[ automation  java  testing  ]
Monitoring Kubernetes Resources with Fabric8 Informers
Introduction to Bazel
Introduction to Apache Ant
Introduction to CircleCI
Using testify for Golang tests