Thursday, 3 January 2013

Selenium - Generate HTML Reports using JUnit+ANT

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.

In this example, we will demonstrate how to Generate HTML Reports using ANT. Let's follow the given steps:
Step 1: Download Apache Ant
Download Apache Ant

OSArchive name
Step 2: Set Ant Environment

Set the ANT_HOME environment variable to point to the base directory location where ANT libraries is stored on your machine. For example, We've stored Ant libraries in apache-ant-1.8.4 folder on various Operating Systems as follows.

WindowsSet the environment variable ANT_HOME to C:\Program Files\Apache Software Foundation\apache-ant-1.8.4
Linuxexport ANT_HOME=/usr/local/\apache-ant-1.8.4
Macexport ANT_HOME=/Library/\apache-ant-1.8.4
Append Ant compiler location to System Path is as follows for different OS:
WindowsAppend the string ;%ANT_HOME\bin to the end of the system variable, Path.
Linuxexport PATH=$PATH:$ANT_HOME/bin/
Macnot required
Step 3: Download Junit Archive

Download JUnit Archive
OSArchive name

Let us create a new project in Eclipse and write a build.xml file to execute the Java files in this project 

1) Create a new project – JavaProject
2) Create a new package (under src) – seleniumtest
3) Under this package, create new Java class –
4) Record a Selenium Test and export in JUnit 4 format as below

package seleniumtest;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;

public class AntTest extends SeleneseTestCase{
private static SeleniumServer seleniumServer;
public void setUp() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "");
        seleniumServer = new SeleniumServer();

public void testGoogleSearch() throws Exception {"/");"link=Advanced search");
selenium.type("as_q", "selftechy, selenium");"//input[@value='Advanced Search']");

public void tearDown() throws Exception {


Create ANT Build.xml under JavaProject

<?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" />

<target name="init">
<delete dir="${bin}" />
<mkdir dir="${bin}" />

<target name="compile" depends="init">
<javac source="1.6" srcdir="${src}" fork="true" destdir="${bin}" includeantruntime="false">
<pathelement path="${bin}">
<fileset dir="${lib}">
<include name="**/*.jar" />

<target name="exec" depends="compile">
<delete dir="${report}" />
<mkdir dir="${report}" />
<mkdir dir="${report}/xml" />
<junit printsummary="yes" haltonfailure="no">
<pathelement location="${bin}" />
<fileset dir="${lib}">
<include name="**/*.jar" />

<test name="seleniumtest.AntTest" haltonfailure="no" todir="${report}/xml" outfile="TEST-result">
<formatter type="xml" />
<junitreport todir="${report}">
<fileset dir="${report}/xml">
<include name="TEST*.xml" />
<report format="frames" todir="${report}/html" />

Run the following ant command



  1. Hi,
    Nice blog. I have question when I run ANT and generate Junit reports
    I see that the package name is shown as "None" and the class name is shown as Blank. Can you let me know as to what the issue is.


  2. Please help, How I can send the Selenium test report automatically through build.xml

  3. Hi, I have follow u r step but it create XML file rather than HTML
    pls provide me a steps to generate report in html format

  4. Hi..

    I have created the code given above but i don't know, how to run the ANT command..?

    Can anyone help me on this..

    "E:\HLUII-Testing\JavaProject" this was my workspace.. here there was no files related to ANT.

  5. Hi prasanth,

    can you please tell me how to download the files in any UI using Firefox profiles with set preference without using AutoIT and any other tools.

  6. Thanks for the great information in your blog Selenium Training in Chennai