Book Image

Selenium Testing Tools Cookbook

5 (1)
Book Image

Selenium Testing Tools Cookbook

5 (1)

Overview of this book

This book is an incremental guide that will help you learn and use the advanced features of the Selenium toolset including the WebDriver API in various situations to build a reliable test automation. You start off by setting up the test development environment and gain tips on the advanced locater strategy and the effective use of the Selenium WebDriver API. After that, the use of design patterns such as data - driven tests and PageFactory are demonstrated. You will then be familiarised with extending Selenium WebDriver API by implementing custom tasks and setting up your own distributed environment to run tests in parallel for cross-browser testing. Finally, we give you some tips on integrating Selenium WebDriver with other popular tools and testing mobile applications. By the end of this book, you will have learned enough to solve complex testing issues on your own.
Table of Contents (21 chapters)
Selenium Testing Tools Cookbook Second Edition
About the Author
About the Reviewers

Using Ant for the Selenium WebDriver test execution

Apache Ant is a popular build tool available for Java developers. It is similar to Apache Maven, but does not support project management and dependency management features like Maven. It's a pure build tool.

You can run Selenium WebDriver tests using Ant via command line or through continuous integration (CI) tools such as Jenkins.

In this recipe, we will add Ant support to the SeleniumCookbook project created in the Configuring Selenium WebDriver test development environment for Java with Eclipse and Maven recipe.

Getting ready

You can also download and configure Ant from for other OS platforms.

Windows users can download and install WinAnt on Windows. WinAnt comes with an installer that will configure Ant through the installer. The WinAnt installer is available at

This recipe uses WinAnt on the Windows OS.

You will also need Selenium WebDriver and JUnit JAR files. You can download Selenium JAR file from and JUnit JAR file from

How to do it...

Let's set up the SeleniumCookbook created in the previous recipe project for Ant with the following steps:

  1. Create a lib folder and copy the JAR files for the dependencies used for this project, that is, Selenium WebDriver and JUnit, to the lib folder, as shown in screenshot below:

  2. Create the build.xml file in the project folder with the following XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="test" default="exec" basedir=".">
        <property name="src" value="./src" />
        <property name="lib" value="./lib" />
        <property name="bin" value="./bin" />
        <property name="report" value="./report" />
        <path id="test.classpath">
            <pathelement location="${bin}" />
            <fileset dir="${lib}">
                <include name="**/*.jar" />
        <target name="init">
            <delete dir="${bin}" />
            <mkdir dir="${bin}" />
        <target name="compile" depends="init">
            <javac source="1.8" srcdir="${src}" fork="true"
                destdir="${bin}" >
                    <pathelement path="${bin}" />
                    <fileset dir="${lib}">
                      <include name="**/*.jar" />
        <target name="exec" depends="compile">
            <delete dir="${report}" />
            <mkdir dir="${report}" />
                <mkdir dir="${report}/xml" />
            <junit printsummary="yes" haltonfailure="no">
                    <pathelement location="${bin}" />
                    <fileset dir="${lib}">
                        <include name="**/*.jar" />
                <test name="com.secookbook.examples.chapter1.GoogleSearchTest"
                    haltonfailure="no" todir="${report}/xml"
                    <formatter type="xml" />
            <junitreport todir="${report}">
                <fileset dir="${report}/xml">
                    <include name="TEST*.xml" />
                <report format="frames"
                    todir="${report}/html" />
  3. Navigate to the project directory through the command line and type the following command:


    This will trigger the build process. You will see the test running. At the end, Ant will create a report folder in the project folder. Navigate to the html subfolder in the report folder and open the index.html file to view the results.

How it works...

Ant needs a build.xml file with all the configurations and steps required to build the project. We can add steps for report generation, sending e-mail notification, and so on to build.xml. Ant provides a very dynamic framework for defining steps in the build process.

Ant also needs the necessary library/JAR files to be copied in the lib folder, which are needed for building the project.

Ant scans for the complete set of tests in the project and executes these tests in a way similar to Maven.