Apache Ant is an open source build tool. A build tool can be used to compile the source code, creating the build artifacts such as JAR, WAR, and EAR files. Some of the other usage of ANT is to run unit tests, do the application deployment on containers such as JBoss, Tomcat, WebSphere, WebLogic, GlassFish, etc, and to run Automated Selenium Tests.
Ant is a powerful build tool and also is very much extensible. There are several open source Ant libraries available which need to be just downloaded, unzipped, and copied (JAR file) into the Ant’s “lib” folder. Once the library file is copied, then we can utilize the “Tasks” in the Ant’s build.xml file. Ant’s “contrib library, jsch library, etc” are the best examples for such libraries.
Let us create a new project in Eclipse and write a build.xml file to execute the Java files in this project (For details on how to setup Eclipse, please visit How to Setup Eclipse for Selenium Test Automation):
- Create a new project – SampleTests
- Create a new package (under src) – com.selftechy.seltests
- Under this package, create new Java class – SeleniumTest.java
- Record a Selenium Test and export in JUnit 4 format as below
package com.selftechy.seltests;
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
public class SeleniumTest extends SeleneseTestCase{
private static SeleniumServer seleniumServer;
@Before
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");
seleniumServer = new SeleniumServer();
seleniumServer.start();
selenium.start();
}
@Test
public void testGoogleSearch() throws Exception {
selenium.open("/");
selenium.click("link=Advanced search");
selenium.waitForPageToLoad("30000");
selenium.type("as_q", "selftechy, selenium");
selenium.click("//input[@value='Advanced Search']");
selenium.waitForPageToLoad("30000");
}
@After
public void tearDown() throws Exception {
selenium.stop();
seleniumServer.stop();
}
}
Let us try to create a build.xml file for execution of this Java class using Ant:
<?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="com.selftechy.seltests.SeleniumTest" 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>
Looking at the above build.xml file makes you feel it is very much complex but that is not the case if it is understood properly.
Let us try to explore all the XML tags and attributes of the build.xml file.
basedir – value of this is “.” (just a dot or period) that is the path of the build.xml file. i.e. In this example I have kept the build.xml file in the path – F:\Helios-Workspace\SelfTechy\SampleTests
default – “exec” – this refers to the default ant target which will be executed when there is no ant target is mentioned while executing the ant command in the command prompt. For example we can just say “ant” in the command prompt without specifying the target as argument. If we need to execute a specific target then we can specify the target as “ant compile or ant init or ant run”, etc.
One more thing I want to clarify is “./”, this refers to the current working directory. If you want to refer to any of the parent folder then use “../” in any of the ant targets.
Each <target> will have different ant tasks such as javac, mkdir, delete, junit, etc. These are actually the actions performed on the Java code provided in the folders.
The Ant targets written in this build.xml file are exec, compile, and init. In each of the <target> we have specified “depends” attribute which says Ant to execute the target specified first and then the current target. Hence, Ant will be executing the different targets in a sequential manner.
init – This Ant target first deletes the bin folder and then recreates it.
compile – This Ant target compiles the java code in the src folder and then puts the binary (class) files in the bin folder.
exec – this executes the class files in the bin folder and then generates report folder and copies all the HTML / XML files into this folder.
Install and configure JDK and ANT
Before doing above all ANT and JDK has to be downloaded and installed on the system.
Download JDK from – JDK download link
Download ANT from Apache web site – ANT download link
Unzip the ant and copy to C:\Ant folder
Now we need to configure environment variables: ANT_HOME and JAVA_HOME
Copy the path to both Ant’s bin folder and JDK’s bin folder should be (C:\Ant\bin and C:\program files\Java\JDK\bin) copied to “path” environment variable. System has to be restarted once after the environment variables are set.
Execute JUnit test from Ant and generate HTML reports
Go to command prompt – > navigate to folder where the build.xml is kept (F:\Helios-Workspace\SelfTechy\SampleTests) and then execute the following command:
F:\Helios-Workspace\SelfTechy\SampleTests>ant (press enter)
This should fire the Selenium tests and generate the HTML / XML reports and put them into the report folder.
After the execution, it creates one more folder – > report and it contains HTML reports that should look as below: