Apache ANT

Apache ANT with Selenium

Table of Contents

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 http://ant.apache.org/bindownload.cgi and download the Apache Ant latest version .zip folder (apache-ant-1.9.15-bin.zip) 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" />
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </fileset>
Ā Ā Ā Ā Ā Ā Ā </path>
Ā Ā Ā Ā Ā Ā Ā <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>
Ā Ā Ā Ā Ā Ā Ā <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" />
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </javac>
Ā Ā Ā Ā Ā Ā Ā </target>
<!--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"/>
</javac>
Ā Ā Ā Ā Ā Ā Ā <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"/>
</target>
</project>

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"/>
</javac>
  • 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 Project

Step2: 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.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.*;
import static org.junit.Assert.*;
public class GoogleSearch {
private WebDriver driver;
@Before
public void setUp() {
// Launch a new Firefox instance
System.setProperty("webdriver.chrome.driver", "F:\\drivers\\chromedriver.exe");
//create chrome instance
driver = new ChromeDriver();// Maximize the browser window
driver.manage().window().maximize();
// Navigate to Google
driver.get("http://www.google.com");
}
@Test
public void GoogleSearch() {
// Using the name find the Search Box Text input element
WebElement element = driver.findElement(By.name("q"));
// Clear the existing text value
element.clear();
// Enter the Search Keyword
element.sendKeys("Software Testing Tutorials");
// Submit the Search Query
element.submit();
// 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());
}
@After
public void tearDown() throws Exception {
// Close the browser
driver.quit();
}
}

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 ‘com.packge.one’ and ‘com.packge.two’

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

Create class ‘TestOne’ under ‘com.packge.one’

package com.packge.one;

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

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

@Test
public void testOne() {
System.out.println("hello");
}

@Test
public void testTwo() {
System.out.println("hello");
}

@AfterClass
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 {
@BeforeClass
public void setUp() {
System.out.println("*** In class - Test B ***");
}

@Test
public void testOne() {
System.out.println("hello");
}

@Test
public void testTwo() {
System.out.println("hello");
}

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

}

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample Test Suite">
Ā Ā <test name="Sample Test">
Ā Ā Ā Ā <classes>
Ā Ā Ā Ā Ā Ā <class name="com.packge.one.TestOne"/>
Ā Ā Ā Ā Ā Ā Ā <class name="com.packge.two.TestTwo"/>
Ā Ā Ā Ā </classes>
Ā Ā </test>
</suite>

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>
Ā Ā Ā Ā <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"/>
Ā Ā Ā Ā </path>
Ā Ā Ā Ā <target name="init">
Ā Ā Ā Ā Ā Ā Ā Ā <mkdir dir="bin"/>
Ā Ā Ā Ā Ā Ā Ā Ā <copy includeemptydirs="false" todir="bin">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <fileset dir="src">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <exclude name="**/*.launch"/>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <exclude name="**/*.java"/>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā </fileset>
Ā Ā Ā Ā Ā Ā Ā Ā </copy>
Ā Ā Ā Ā </target>
Ā Ā Ā Ā <target name="clean">
Ā Ā Ā Ā Ā Ā Ā Ā <delete dir="bin"/>
Ā Ā Ā Ā </target>
Ā Ā Ā Ā <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.project.name}: ${ant.file}"/>
Ā Ā Ā Ā Ā Ā Ā Ā <javac debug="true" debuglevel="${debuglevel}" destdir="bin" includeantruntime="false" source="${source}" target="${target}">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <src path="src"/>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <classpath refid="AntTestNG.classpath"/>
Ā Ā Ā Ā Ā Ā Ā Ā </javac>
Ā Ā Ā Ā </target>
Ā Ā Ā Ā <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"/>
Ā Ā Ā Ā Ā Ā Ā Ā </copy>
Ā Ā Ā Ā Ā Ā Ā Ā <unzip dest="${ant.library.dir}">
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <patternset includes="jdtCompilerAdapter.jar"/>
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
Ā Ā Ā Ā Ā Ā Ā Ā </unzip>
Ā Ā Ā Ā </target>
Ā Ā Ā Ā <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
Ā Ā Ā Ā Ā Ā Ā Ā <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
Ā Ā Ā Ā Ā Ā Ā Ā <antcall target="build"/>
Ā Ā Ā Ā </target>
</project>

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

Conclusion:

  • 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.
Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class