Skip to main content

Selenium Tutorial: Ant Build for Selenium Java project

Ant is a build tool which could be used to have your tests running either from command line or from Hudson CI tool. There is detailed documentation available for ant here but probably you need to know only a little part of it for you selenium tests. The essentials which are needed to know are:
  1. Project
  2. Target (ant execution point and collection of tasks)
  3. Tasks (could be as simple as compilation)
And there would usually be following targets for Selenium tools -
  1. setClassPath - so that ant knows where you jar files are
  2. loadTestNG - so that you could use testng task in ant and use it to execute testng tests from ant
  3. init - created the build file
  4. clean - delete the build file
  5. compile - compiles the selenium tests
  6. run - executes the selenium tests
Here is my project set up for ant -

And the ant file is -

You may also like to use mail task in your build file so that test report is emailed after test execution. You could find on mail task here


  1. Hi Tarun,

    Your selenium blog is excellent helps a lot. I have a query regarding with ANT and Selenium.Do you any idea how to execute ant selenium framework on different environments. In Maven, i use profiles to build the project on diff environment. Any idea with ANT?..Plz assist me.

  2. Looks like this answers your questions -

  3. I have configured hudson to run my test cases by invoking ant.
    But after my test cases ran successfully, build doesn't stop. I have manually stop the build. Is there a way I can fix this issue?

  4. Not sure if there is an issue with version of ant your are using.
    I suppose you mean to say that target execution never reaches completion, right?
    Could you post your question here -
    with the detail of your build and the target which runs infinitely

    1. Hi

      your blog is very useful for the selenium beginners.

      I initially used ant to run my selenium RC and Webdriver test cases using firefox browser and worked fine. But after firefox version 11 the ant build.xml file is not at all launching the firefox browser. The same problem for chrome browser, it opens the browser but not getting the specified URL and for IE i am getting Eval method not available (issue 217)

      Do you have any suggestion?

  5. Hi,

    Your blog was very much useful for Selenium beginners.

    Initially I used to run my selenium Webdriver and Selenium RC test cases using Ant with firefox browser ( it worked initially well)but now ( after firefox version 11)my ant build.xml file is not at all launching the firefox browser. and if at all i try some times,it fails and i had to restart my machine to run the test in eclipse atleast. The same problem with chrome it opens the browser but not pointing to the mentioned URL and with IE I am getting the error
    Eval method not found. only HtmlUnitDriver works under ant.

    do you have anyy suggestion?
    Thanks in advance.


    1. Could you add more details to your question and post it here -

  6. Q1 How can I run selenium script using ANT on MAC?
    Q2 What all changes I need to do in build.xml file which I have created for windows but needs to run on MAC.


Post a Comment

No spam only genuine comments :)

Popular posts from this blog

Return only first or last element from webelements collection

We often come across situation when there are multiple elements on a page and we probably like to exercise only a few of them using selenium webdriver. May be just first and last element. For example on a search result page we may like to click on only first and last link and not all. This is when Iterables API comes handy. (By the way I am assuming that you have already completed watching selenium training videos :)). Once we have collection of web element then we can use Iterables to get only first or last element as following - Consider that we fetch collection of element as - List< WebElement > webElements = getDriver().findElements(By. id ( "htmlID" ));   Now we can get the first web element from this collection as -  WebElement firstElement = Iterables. getFirst (webElements,  getDriver().findElement(By. id ( "defaultElement" )));   Herein second argument -   (getDriver().findElement(By. id ( "defaultElement" )))    in the me

Using chrome console to test xPath and css selectors

Since the advent of selenium there have been many plugin to test xPath / css selectors but you don’t need any of them if you have chrome browser. Using Chrome console you can test both xPath and css selectors. Launch website to be tested in chrome browser and hit F-12 and you would see chrome console opened in lower pane of application - Hit escape key and console would open another pane to write element locators - And now you can start writing xPath or css selectors in chrome console and test them - The syntax for writing css id - $$(“ ”) And hit the enter key. If your expression is right then html snippet of the application element corresponding to the css selector would be displayed - If you mouse over the html snippet in chrome console then it would highlight the corresponding element in application - If you want to clean console of previously written element selectors then just hit ctrl+L keys and chrome console would be empty again. Pro

Using xPath to reach parent of an element

Well, I am big fan of css locator my self there are times when css locators don't fit requirement. One such requirement is when you want to navigate to parent element of an element and may be parent of parent and even more. Unfortunately css locators don't provide any mechanism to navigate to parent of an element. See this for more. Of late I came across a scenario when I wanted to click on a link depending upon the text in a text box. Herein parent of text box and parent of link were at the same location. More over there could have been many such combinations in application. Fortunately I just need to pick first such instance and Web Driver any way considers only first instance when multiple locators are found matching an element. Element in question is in following html - Here I need to click on highlighted anchor on the basis of input element (which is also highlighted in image) Herein first I need to reach div parent (class = 'left couponmainarea