Using Ant for Selenium WebDriver test execution

Apache Ant is a popular build management 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.

Ant is another choice to run Selenium WebDriver tests from the command line or through continuous integration tools such as Jenkins.

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

Getting ready

You can download and install WinAnt on Windows. WinAnt comes with an installer that can configure Ant through the installer. The WinAnt installer is available at http://code.google.com/p/winant/.

You can also download and configure Ant from http://ant.apache.org/bindownload.cgi for other OS platforms. This recipe uses WinAnt on the Windows OS.

How to do it...

Let's set up the SeleniumCookbook 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.
  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" />
            </fileset>
        </path>
    
        <target name="init">
            <delete dir="${bin}" />
            <mkdir dir="${bin}" />
        </target>
    
        <target name="compile" depends="init">
            <javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" >
                <classpath>
                            <pathelement path="${bin}">
                            </pathelement>
                    <fileset dir="${lib}">
                        <include name="**/*.jar" />
                    </fileset>
                </classpath>
            </javac>
        </target>
    
        <target name="exec" depends="compile">
            <delete dir="${report}" />
            <mkdir dir="${report}" />
                <mkdir dir="${report}/xml" />
            <junit printsummary="yes" haltonfailure="no">
                <classpath>
                    <pathelement location="${bin}" />
                    <fileset dir="${lib}">
                        <include name="**/*.jar" />
                    </fileset>
                </classpath>
    
                <test name="seleniumcookbook.examples.test.GoogleSearchTest" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
                    <formatter type="xml" />
                </test>
            </junit>
            <junitreport todir="${report}">
                <fileset dir="${report}/xml">
                    <include name="TEST*.xml" />
                </fileset>
                <report format="frames" todir="${report}/html" />
            </junitreport>
        </target>
    </project>
  3. Navigate to the project directory through the command line and type the following command:
    ant

    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 that are needed 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 to define steps in the build process.

Ant also needs the necessary library/JAR files that are needed to build the project to be copied in the lib folder.

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

See also

  • The Using Jenkins and Ant for Selenium WebDriver test execution in continuous integration recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.131.47