Create Selenium WebDriver TestNG Project using ANT

Apache Ant is one of the commonly used build tools. In this post, we can see how to set up a Selenium WebDriver TestNG project using ANT. If you are not familiar with ANT, you can check this tutorial. Check this post for installing ANT on your machine.

WebDriver TestNG Ant

So let’s begin.

  • Create a normal Java project in Eclipse. Create a folder named lib and put the required jars (selenium-standalone jar and testng jar)
  • Create a file named ‘build.xml’ in the project root directory

Ant Project Structure

  • Open the build.xml file

Now, let’s focus on a few points on ANT.

The build.xml file should have a root tag of the project which can have three attributes – name, basedir and default target.

Here, basedir sets the base directory of the project and default sets the default target. Target is a collection of tasks that is run as a single unit. In the above case, the default task is a target named ‘printInfo’.

Let’s see how to define a target

Here we have set a property with name ‘projectName’ and defined a target ‘printInfo’ which echoes out the Project name and Ant version.

If you run the ant project now, you will get an output as shown below.

Ant Output

Now, let’s add all the required targets with dummy contents.

You can see that some targets have a depends attribute where another target is mentioned. This ensures that the target is run only after all its dependent targets are executed.

SEE ALSO:   Guidelines for Page Object Model Frameworks

Now that we have seen the basics, let’s get back to our Selenium TestNG setup.

We will see each target separately starting with setting the properties.


The properties lib, src and bin are set relative to basedir folder which was set in project tag (<project name=”WebDriver Ant” basedir=”.” default=”printInfo”>). If it was not set, you would have had to set it as a property (<property name=”basedir” value=”.” />). The property ‘testng-report-dir’ set the output directory for TestNG reports.

Set ClassPath

We set the classpath as shown below.

Classpath is set for the bin directory and the jars in ‘lib’ directory. The selenium and testng jar should be present in the lib directory. If it’s in another directory, you can add another fileset tag to include that as well.


The target clean is usually used to clean up the bin folder.


This target compiles all the java files into java classes and put them in the bin directory.

As you can see, the javac task performs the compilation using the source directory, destination directory and classpath we have provided.

SEE ALSO:   Setting up WebDriver with Maven

Run TestNG

This target performs the execution using TestNG.

The taskdef defines the testng task and the target performs the testng execution. For testng task, we give the output directory where the result html will be written, classpath and the location of testng.xml file.

Now, let’s create a TestNG class and try out the ant build file.

  •  Right click on Eclipse project New > TestNG class
  • Provide the required values as shown below

TestNG Class

Here is the final build.xml file

Execute the Project

  • For executing the project in Eclipse, you can right-click on the build.xml file> Run As > Ant Build. If the default target is not set as ‘run-testng’, you have to use Run As > Ant Build… and select the run-testng target to run.
SEE ALSO:   How to Run TestNG xml Programmatically

Non Default Target

  • For executing from the command line, navigate to the project base folder and use the command ant . Again if run-testng is not the default target, you have to use  ant run-testng

Let me know if  you found this tutorial useful. If you need articles on any particular topic, do let me know in the comments section below.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *