All IT Courses 50% Off
Selenium Tutorials

Apache ANT with Selenium

What is Apache Ant?

An Ant is an open-source tool that automates the software build process. Apache Ant is a build tool developed using java. It was implemented for the Java project because it has an in-build feature with java but it can be still used for applications built on other languages. Apache Ant is the most popular and conventional build tool. It was provided by Apache Software Foundation and freely distributed under the GNU license. In day to day work schedule, Apache Ant plays an important role in developers as well as Testers environment. It has very immense power to build the code into deployment utilities.

In general, while developing a software product, we need to take care of classpath, cleaning of executable binary files, compiling and execution of source code, creation of reports, etc. If we manually do all these tasks one by one, it will take more time, and the process will lead to errors. To overcome this situation we use a build tool like Ant. It executes and automates all the processes in a sequential order which are mentioned in the Ants configuration file.

Benefits of Apache Ant

  • Ant is implemented and written in Java language thus is a platform-independent build tool. JDK is the only requirement for the tool.
  • It is Ease to Use and offers a wide variety of tasks that fulfil all the requirements of the user.
  • It creates a complete life cycle i.e compile, clean, executes, report, etc.
  • End to End application is created, delivered, and deployed.

Features of Apache Ant

  • It complies with Java-based applications
  • Creates zip, jar, war, tar files
  • Creates Java Doc
  • It can copy files to different locations
  • We can move or delete files
  • It supports TestNG, Junit 4, etc.
  • It will convert XML based test reports to HTML reports
  • Emails can be sent to end-users

How to install Ant

The entire step by step setup process is as follows

Step 1: Go to and download the Apache Ant latest version .zip folder ( from the repository.

Apache ANT with Selenium

Step 2: Unzip and extract the folder and copy the path to the root of the unzipped folder.

Extract the zipped folder at your desired location onto the local file system.

Apache ANT with Selenium

Step 3: Right-click on My Computer>Click Properties>click on Advanced system settings> Click on Environment Variables.

Apache ANT with Selenium

Step 4: Environment Variable Set Up

Click on Environment Variables to set up the environment variable. Click on the New button and enter the Variable name and Variable value as the root path of Ant folder till bin and click OK.

Apache ANT with Selenium
Apache ANT with Selenium

Step 5:  Click on Path at System Variable and click ‘Edit’ and append; %ANT_HOME%\bin.

Apache ANT with Selenium

Restart the system once and now you are ready to use the Ant build tool.

Step 6: Validate Build On Cmd

Open CMD prompt and type command: ant -version.

Explanation of Build.xml

Let’s understand the code within a sample build.XML

Sample Build.Xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Selenium_Testing" default="dist" basedir=".">
       <property name="src" value="./src" />
       <property name="lib" value="./lib" />
       <property name="bin" value="./bin" />
       <property name="report" value="./report" />
       <property name="test.dir" value="./src/com/tests" />
       <path id="Selenium_Testing.classpath">
              <pathelement location="${bin}" />
              <fileset dir="${lib}">
                     <include name="**/*.jar" />
       <echo message="--------------------Selenium Testing Programs----------------------" />
       <target name="init" description="Delete the binary folder and create it again">
              <echo message="----------Delete the binary folder and create it again----------" />
              <delete dir="${bin}" />
<!-- Create the build directory structure used by compile -->
              <mkdir dir="${bin}" />
       <target name="compile" depends="init" description=” Source files compilation">
              <echo message="---------- source files compilation----------" />
              <javac source="1.8" srcdir="${src}" fork="true" destdir="${bin}" includeantruntime="false" debug="true" debuglevel="lines,vars,source">
                     <classpath refid="Learning_Selenium.classpath" />
<!--mkdir tag will create new director-->
<mkdir dir="${build.dir}"/>
<echo message="classpath:${test.classpath}"/>
<echo message="compiling.........."/>
<!--javac tag used to compile java program code and move .class files to a new folder-->
<javac destdir="${build.dir}" srcdir="${src.dir}">
<classpath refid="classpath_jars"/>
       <target name="exec" depends="compile" description="Launch the test suite">
              <echo message="----------Launch the test suite----------" />
              <delete dir="${report}" />
              <mkdir dir="${report}" />
              <mkdir dir="${report}/xml" />
<target name="run" depends="compile">
<!--java tag will execute main function from the jar created in compile target section-->
<java jar="${selenium.dir}/Selenium.jar"fork="true"/>

The Project element consists of the project name and basedir attribute.

<project name=“Selenium_Testing” basedir=“.”>

  • Name – The name attribute signifies the name of the project. Here in our case, the project’s name is “Selenium_Testing”.
  • Basedir – The Basedir attribute represents the root directory or base directory of an application. Under this directory, there may be several other folders like src, lib, bin, etc.
  • In the build.XML file, Property tags are used as variables
<property name="build.dir" value="${basedir}/build"/>
		<property name="external.jars" value=".\resources"/>
	<property name="selenium.dir" value="${external.jars}/Selenium"/>
<property name="src.dir"value="${basedir}/src"/>
  • Target tags will execute in sequential order. The name attribute is the name of the target. In a single build.xml, we can have multiple targets
<target name="setClassPath">
  • pathelement tag will set the path to the common location root where all files are stored
 <pathelement path="${basedir}/"/>
  • path tag will bundle all the files
<path id="classpath_jars">
  • fileset tag will set classpath for third party jar in your project
 <fileset dir="${selenium.dir}" includes="*.jar"/>
  • Echo tag is used to print text on the console
<echo message="deleting current build directory"/>
  • Delete tag will clear all the data from the given folder
<delete dir="${build.dir}"/>
  • mkdir tag is used to create a new directory
<mkdir dir="${build.dir}"/>
  • javac tag is used to compile java program and move .class files to a new folder
<javac destdir="${build.dir}" srcdir="${src.dir}">
	<classpath refid="classpath_jars"/>
  • jar tag will create jar file from .class files
<jar destfile="${selenium.dir}/Selenium.jar" basedir="${build.dir}">
  • ‘depends’ attribute is used to depend one target on another target
<target name="exex" depends="compile">

In the Ant build file, all the tasks are defined under Target elements and it corresponds to a particular task or a goal.

<target name=“init” description=“Delete the binary folder and create it again”>

Following goals are created in the above XML code, 

  1. Deleting and creating directories
  2. Code compilation
  3. Test classes execution
  4. Test reports generation

<target name=“exec” depends=“compile” description=“Test suite launch”>

The ‘depends’ attribute is used to depend on one target on another target.

Run Ant using Eclipse plugin

Right-click on build.xml file ->Click on Run as -> Click on Build file

Apache ANT with Selenium

After completion of execution, Ant generates a test execution report inside the “Report” folder.

Executing Selenium Web Driver Scripts using ANT 

Step1: Launch the eclipse and create a new Java ProjectStep2: Create a Class name as “GoogleSearch“

package Ant;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.By;
import org.junit.*;
import static org.junit.Assert.*;
public class GoogleSearch {
private WebDriver driver;
public void setUp() {
// Launch a new Firefox instance
System.setProperty("", "F:\\drivers\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();// Maximize the browser window
// Navigate to Google
public void GoogleSearch() {
// Using the name find the Search Box Text input element
WebElement element = driver.findElement("q"));
// Clear the existing text value
// Enter the Search Keyword
element.sendKeys("Software Testing Tutorials");
// Submit the Search Query
// Timeout after 10 seconds and wait for the page to get rendered
new WebDriverWait(driver,10).until(new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle()
.startsWith("Software Testing Tutorials");
assertEquals("Software Testing Tutorials - Google Search",driver.getTitle());
public void tearDown() throws Exception {
// Close the browser

Execute TestNG Tests from ANT build.xml

Let us create a sample project and execute TestNG tests using ANT build.xml. First we will create a project with some sample testng tests before creating build.xml file.

Step 1: Create a project

Step 2: Create a sample packages as ‘’ and ‘com.packge.two’

Step 3: Create multiple classes with in packages ‘TestOne’ under ‘’ and ‘TestTwo’ under ‘com.packge.two’

Create class ‘TestOne’ under ‘’


import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestOne {
public void setUp() {
System.out.println("*** In class - Test A ***");

public void testOne() {

public void testTwo() {

public void tearDown() {
System.out.println("*** End of class***");
Create class 'TestTwo' under 'com.packge.two'
package com.packge.two;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestTwo {
public void setUp() {
System.out.println("*** In class - Test B ***");

public void testOne() {

public void testTwo() {

public void tearDown() {
System.out.println("*** End of class***");


Step 4: Create testng.xml file and add the classes to test

<!DOCTYPE suite SYSTEM "">
<suite name="Sample Test Suite">
  <test name="Sample Test">
      <class name=""/>
       <class name="com.packge.two.TestTwo"/>

To create build.xml file, Right click on the project -> New -> File -> Enter the file name and Click on the Finish button.

Below is the build.xml file.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. --><project basedir="." default="build" name="AntTestNG">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../eclipse/jee-2020-092/eclipse/"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="11"/>
    <property name="source" value="11"/>
    <path id="TestNG.libraryclasspath">
        <pathelement location="../../.p2/pool/plugins/org.testng_7.3.0.r202008060316.jar"/>
        <pathelement location="../../.p2/pool/plugins/com.beust.jcommander_1.78.0.jar"/>
        <pathelement location="../../.p2/pool/plugins/org.apache-extras.beanshell.bsh_2.0.0.b6.jar"/>
        <pathelement location="../../.p2/pool/plugins/org.yaml.snakeyaml_1.21.0.jar"/>
    <path id="AntTestNG.classpath">
        <pathelement location="bin"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/client-combined-3.141.59.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/client-combined-3.141.59-sources.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/libs/byte-buddy-1.8.15.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/libs/commons-exec-1.3.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/libs/guava-25.0-jre.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/libs/okhttp-3.11.0.jar"/>
        <pathelement location="../../Downloads/selenium-java-3.141.59/libs/okio-1.14.0.jar"/>
        <path refid="TestNG.libraryclasspath"/>
    <target name="init">
        <mkdir dir="bin"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.launch"/>
                <exclude name="**/*.java"/>
    <target name="clean">
        <delete dir="bin"/>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="AntTestNG.classpath"/>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>

Click on File -> click on Export -> General -> Ant Buildfiles and specify the file name, project, etc.

Right-click on Ant build from Eclipse and select Ant Build, you should see a message as ”BUILD SUCCESSFUL” as below:

Apache ANT with Selenium


  • An Ant is an open-source tool that automates the software build process 
  • Apache Ant is a build tool developed using java.
  • Ant is used for compiling and execution of source code, creation of reports, etc.
  • Build.xml file is used to configure execution targets using Ant.
Facebook Comments

Related Articles

Back to top button