Selenium WebDriver - Part 2 - Using Ant to run tests and pass parameters to scripts

In part 1 of the Selenium WebDriver introduction to running tests with different browsers, we proposed an architecture to enable us to achieve our goal. In this part, we’ll talk about how to use Ant to run tests with a specific browser and generate XML result files, all through command line (or in our case, Mac terminal).

Let’s show the basics of our Ant build.xml file. Ant’s documentation is superb, so to learn more go there. This is just the basics needed for a build.xml file. Yours might look different depending on your project structure.

<project name="WebAutomation" default="build" basedir=".">
    <description>
        Build file for controlling the project using ant -- Useful for commandline
    </description>
    <property name="src" location="src" />
    <property name="dest" location="dest" />
    <property name="reports" location="reports" />
    <property name="libs" location="libs" />

    <property name="browser" value="${browser}" />

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

    <path id="build.out">
        <pathelement location="${dest}"/>
        <fileset dir="${dest}">
            <include name="*.class"/>
        </fileset>
    </path>

    <target name="clean">
        <delete dir="${dest}" />
        <delete dir="${reports}" />
    </target>

    <target name="init">
        <tstamp/>
        <mkdir dir="${dest}" />
    </target>

    <target name="build" depends="init">
        <javac srcdir="${src}" destdir="${dest}" includeantruntime="false" >
            <classpath>
                <path refid="classpath.test"/>
            </classpath>
        </javac>
    </target>
</project>

What we have here is:

  • A ‘clean’ target to remove any old build artifacts
  • An ‘init’ target to create the folder where our build artifacts will be
  • A ‘build’ target that uses the javac task to build our project

Now, we decided we wanted 2 more tasks; one for running all tests, and one for running a specific test.

For the purpose of this post, I’ll just show the target for running all tests:

<target name="testall" depends="build">
        <mkdir dir="${reports}"/>
        <junit haltonerror="no" printsummary="yes" logfailedtests="yes" fork="no" showoutput="yes">
            <sysproperty key="browsername" value="${browser}"/>
            <classpath>
                <path refid="build.out"/>
                <path refid="classpath.test"/>
            </classpath>
            <formatter type="xml"/>
            <batchtest todir="${reports}">
                <fileset dir="${dest}">
                    <include name="**/*Tests.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>

The key here is

<property name="browser" value="${browser}" />

and

<sysproperty key="browsername" value="${browser}"/>

We set the the property ‘browser’ by using the -D flag via terminal. We then use that value to create a system property. Recall from Part 1 we had the following:

public class DriverFactory {
    public static WebDriver constructDriver() {
        if(System.getProperty("browsername") != null) {
            String browser = System.getProperty("browsername");
            if (browser.toLowerCase().equals("firefox")) {
                return new Firefox().setDriver();
            }
            else if(browser.toLowerCase().equals("chrome")) {
                return new Chrome().setDriver();
            }
            // Add more browsers here
        }
        return new Firefox().setDriver();
    }
}

This reads the system property (which was set using Ant) to figure out what browser we’d like to run the tests with.

To conclude, here’s what you would type in a terminal:

ant testall -Dbrowser=chrome

I hope this helps someone with setting up a basic but complete framework for running tests via command line while specifying different browsers.