Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Selenium Testing Tools Cookbook Second Edition

You're reading from   Selenium Testing Tools Cookbook Second Edition Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher
ISBN-13 9781784392512
Length 374 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
UNMESH GUNDECHA UNMESH GUNDECHA
Author Profile Icon UNMESH GUNDECHA
UNMESH GUNDECHA
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting Started 2. Finding Elements FREE CHAPTER 3. Working with Elements 4. Working with Selenium API 5. Synchronizing Tests 6. Working with Alerts, Frames, and Windows 7. Data-Driven Testing 8. Using the Page Object Model 9. Extending Selenium 10. Testing HTML5 Web Applications 11. Behavior-Driven Development 12. Integration with Other Tools 13. Cross-Browser Testing 14. Testing Applications on Mobile Browsers 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:
    How to do it...
  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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image