Book Image

Selenium Testing Tools Cookbook

By : UNMESH GUNDECHA
5 (1)
Book Image

Selenium Testing Tools Cookbook

5 (1)
By: UNMESH GUNDECHA

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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 http://ant.apache.org/bindownload.cgi 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 http://code.google.com/p/winant/.

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 http://selenium-release.storage.googleapis.com/ and JUnit JAR file from https://github.com/junit-team/junit/wiki/Download-and-Install.

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" />
            </fileset>
        </path>
    
        <target name="init">
            <delete dir="${bin}" />
            <mkdir dir="${bin}" />
        </target>
    
        <target name="compile" depends="init">
            <javac source="1.8" srcdir="${src}" fork="true"
                destdir="${bin}" >
                <classpath>
                    <pathelement path="${bin}" />
                    <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="com.secookbook.examples.chapter1.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 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.