Automation with Selenium,Junit, Ant

What is Selenium
How does Selenium work
History and how it started
About Ant
And Junit

Much of the technologies above do not or will not need an introduction if you already know them or can read them from the links above.

More over today’s article is more about how we can use all the three selenium, ant and junit to come up with an automated solution for regressive testing.

Please refer to the documentation links above for the basic knowledge on any of the used tools.

I am assuming that you know how to record a test in selenium if you dont then go here

Now that you do know how to record and save the tests simply save them in any directory structure you like as long as they follow the right package convention. like
this would mean the file should be in directory like com/foo/bar. Conventionally and logically today that is how the TestCase is compiled through the ant script. If the package is not the right path then compile time errors will occur.

My calling target for running the whole procedure should look like this

< target name="build_tests" depends="clean, compile, start-server,  tests, stop-server" / >

Details of the depending targets are as follows.
clean: would clean the entire build directories etc to make sure nothing from our past is carried forward into the future.

compile: should compile

start-server: should start the selenium server.

tests: should run the selenium tests.

stop-server: should stop the selenium server once all tests have been executed.

How to start a selenium server from Ant?

    < target name="start-server" >
        < java jar="lib/selenium-server.jar" fork="true" spawn="true" >
            < arg line="-timeout 30" / >
            < jvmarg value="" / >
            < jvmarg value="-Dhttp.proxyPort=44444" / >
        < / java >
    < / target >


The target above is simply taking the selenium-server.jar which is in the classpath and running the main-class from it. the proxy parameters are optional.

How to stop the server from Ant?

 < target name="stop-server" >
        < get taskname="selenium-shutdown" 
            dest="result.txt" ignoreerrors="true" / >
        < echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" / >
    < / target >

The selenium server starts on port 4444 by default and to tell it to shutdown is simple, the command in the src is passed to it. cmd=shutDown
That should just shutdown the server.

Executing the test cases?
Selenium Test cases are Junit tests cases and that’s how they are treated in this tutorial.
Thus some of you will be very much familiar with the ant targets junit and junitreport. However I will describe how the following is working.
the following target will run the selenium tests and print a summary report to the ${dir}
The includes and excludes, simply tell the target which files to include or exclude while running the test cases. This is typically done when you don’t want some tests cases to be included or your source for tests and the application are in the same directory and you only want to include something like Test*.class.

  < target name="tests" depends="compileonly" description="runs JUnit tests" >
    < echo message="running JUnit tests" / >
    < junit printsummary="on" dir=".." haltonfailure="off" haltonerror="off" timeout="${junit.timeout}" fork="on" maxmemory="512m" showoutput="true" >
      < formatter type="plain" usefile="false" / >
      < formatter type="xml" usefile="true" / >
      < batchtest todir="${testoutput}" filtertrace="on" >
        < fileset dir="${src}" >
          < includesfile name="${tests.include}" / >
          < excludesfile name="${tests.exclude}" / >
        < / fileset >
      < / batchtest >
      < classpath >
        < pathelement path="${classes}" / >
        < pathelement path="${build.classpath}" / >
      < / classpath >
    < / junit >

The following will take the formatted output from the lines above and generate a report out of it in xml and html and place the results in the ${reports}/index.html
A sample Junit test report might look like this.

   < echo message="running JUnit Reports" / >
   < junitreport todir="${reports}" >
      < fileset dir="${reportdir}" >
        < include name="Test*.xml" / >
      < / fileset >
      < report format="frames" todir="${reports}" / >
    < / junitreport >
    < echo message="To see your Junit results, please open ${reports}/index.html}" / >
  < / target >

In general you can add all of this to your nightly build through any of the CI servers like Cruise Control. Also as a general practice you will need to do a little more then just executing this target every night depending on your application. For instance cleaning up of resource centers like Databases etc.

13 thoughts on “Automation with Selenium,Junit, Ant

  1. We have been working on a framework built on top of Selenium called Tellurium. Groovy + Java + Selenium + JUnit+ TestNG + data driven testing = Tellurium.

    If you have some time, give it a try and give us some feedback.


  2. Thanks for the article, very helpful for a newbie like me.

    Just a quick question. Which is better for reporting, JUnitReport or TestNG? I see people are using the two together, but I wwould like a more simple setup with a focus on reporting on the test runs.

    1. Hi Jason,
      I havnt worked with TestNG that much but still…

      I think both the reports are triggered through ant. Nothing will complicate it that much. I would choose the one I am comfortable with If I were you 🙂 as both reports are very similar or perhaps identical.

      Try looking at the following link
      Its similar to what the Junit report looks like. Cant find a sample that I could show you.

  3. Thanks Shaaf, that report looks good. Downloaded Ant and TestNG so will start playing around now, but from the looks of things Ant will be easier for me to implement.

    But that report you posted looks hot!

  4. I have selenium test framework using JUnit. I have very similar ant targets. My problem is when I open the index file there is no data in it. I check individual xml files respect to tests, sure enough they have all the information with proper error tags etc. I don’t have a suite that runs all the test, each test run individually (looks like you are doing the same). In command line I get the following msg, wonder not having a suite is the issues (because *TestSuite.xml doesn’t have any data. How do i resolve this issue if possible. Thank You in advance.

    here is the command line message I get,
    [echo] running JUnit Report
    [junitreport] Processing C:\..\reports\TESTS-TestSuites.xml to C:\DOCUME~1\..\LOCALS~1
    [junitreport] Loading stylesheet jar:file:/C:/../apache-ant-1.7.1/lib/ant-junit.jar!/org/apache
    [junitreport] Transform time: 766ms

    here is my target

  5. @Anu
    Hey Anu, Its not correct that the TESTS-TestSuites.xml should be empty. It is the xml file that forms the basis of the report. I cant see that you have put in your target here. But please check that all your tests run fine and if they do then you have the right include name e.g.

Leave a Reply

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