Friday, December 11, 2009

ANT with ReportNG, TestNG and Eclipse

After exploring the TestNG, I came to know about two more things that can help in Selenium Automation. One is ANT and another is TestNG. Let’s have a look what both of these are.
ReportNG
ANT
Download Demo Project
Run Demo Project

ReportNG
ReportNG is a plug-in for TestNG which generates nicer reports than TestNG. TestNG has some interfaces that allow you to modify the behavior of TestNG, they call it listeners. One of the listeners is for reporting and ReportNG has used these listeners to make the reports better.
You can check out the sample reports here.
Sample Report

ANT
Ant is an Apache java based build tool, just like make but better than that. It is used to automate your JAVA building process. You can use ANT to do so many tasks but these are few which would help in running TestNG test cases.
  • Start the selenium server
  • Compile the java files and Run it using TestNG
  • Overwrites the TestNG report with ReportNG plug-in
  • Open the Report in browser after completation of test.
Following is the ANT file, which will perform above all task.

Just change the paths of jar file to location on your machine.
<project name="ANT_with_ReportNG" default="run" basedir=".">

 <property name="classes.dir" value="bin" />
 <property name="src.dir" value="src" />
 <property name="report.dir" value="reports" />
 <property name="logs.dir" value="logs" />
 <property name="browser" value="C:/Program Files/Mozilla Firefox/firefox.exe"/>

 <path id="libs">
  <fileset dir="lib">
   <include name="*.jar"/>
  </fileset>
  <pathelement path="${basedir}\${classes.dir}"/>
 </path>

 <target name="run">
  <antcall target="startSeleniumServer"/>
  <antcall target="init"/>
  <antcall target="compile"/>
  <antcall target="runTests"/>
  <antcall target="stopSeleniumServer"/>
  <antcall target="openReport"/>
  </target>
 
 <!-- Start the server -->
 <target name="startSeleniumServer">
  <echo>Starting Selenium Server...</echo>
  <java jar="lib\selenium-server-1.0.2.jar" fork="true" spawn="true">
   <arg line="-singlewindow -log ${logs.dir}\selenium_server_log.txt"/>
  </java>
  
 </target>
 
 <target name="stopSeleniumServer">
  <echo> Trying to stop selenium server ... </echo>
  <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer"
   dest="${logs.dir}\selenium_server_shutdown_result.txt" ignoreerrors="true" />
  <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
 </target>
 
 <!-- Delete old data and create new directories -->
 <target name="init" >
  <echo>Initlizing...</echo>
  <delete dir="${classes.dir}" />
  <mkdir dir="${classes.dir}"/>
  <delete dir="${report.dir}" />
  <mkdir dir="${report.dir}"/>
  <mkdir dir="${logs.dir}"/>
 </target>

 <!-- Complies the java files -->
 <target name="compile">
  <echo>Compiling...</echo>
  <javac includeantruntime="false" debug="true" srcdir="${src.dir}" destdir="${classes.dir}"   classpathref="libs" />
 </target>

 <!-- Runs the file and generates Reportng report -->
 <target name="runTests" description="Running tests" >
  <echo>Running Tests...</echo>
  <taskdef resource="testngtasks" classpathref="libs"/>
  <testng outputDir="${report.dir}"
    haltonfailure="true"
    useDefaultListeners="false"
    listeners="org.uncommons.reportng.HTMLReporter"
    classpathref="libs">
   <classfileset dir="${classes.dir}" includes="**/*.class" />
  </testng>
 </target>

 <!-- Open the report in browser.-->
 <target name="openReport"> 
  <exec executable="${browser}" spawn="yes"> 
   <arg line="'${report.dir}\html\index.html'" /> 
  </exec>
 </target>
</project>


Download Demo project
you can download the demo project from the following location
Download

Run Demo Project.
You can run project in two waya. From command prompt and from IDE. 

To Run the project from the command prompt do following steps
But in order to run ant file from command prompt you need to have ANT on your machine and it's path in ur PATH variable. 
  • Open the command prompt and navigate to extracted folders. 
  • Now type the ant command

follow the given steps to run the project from eclipse IDE
  • Extract the ZIP file
  • Open the Eclipse and navigate to file->New ->Java Project...
  • Choose "Create project from existing source" and browse to the extracted folder.
  • Now to run project open the Ant build.xml file and right click on it and choose Run as->Ant Build

14 comments:

sasidhar said...

throwing error for "includeantruntime"

warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

Prasad said...

I followed the steps mentioned by you
But I got error

BUILD FAILED
C:\Documents and Settings\koti\ANT_with_ReportNG\ANT_with_ReportNG\build.xml:54: The tests failed.

Please let me know further

usmanmohd said...

I am able to run the test successfully. Great thanks Gaurang

vinod said...

one thing, i'm using workflow for my project. Can we use testng.xml in the build.xml instead of .class files?

Abhi_Mishra said...

anyways it worked fr me when i changed -singleWindow to -multiWindow in build.xml...

As -singleWindow works fr those pages which do not contains frames..

Thanks Gaurang

lr said...

Thanks this post really helped me.

I need some help here. I have added the below code in my build.xml. Issue here is The test will run twice one with ANT_Test Suite and one more My Suite. Not sure how to disable Ant Suite



Running Tests...

Vinayak said...

Hey Gaurang..I am new to TestNG and Ant.I have been given a task to run TestNg test cases using ant without using Testng.xml.

So,My question is whether it is possible to run Testng without using testng.xml through ant??

Looking Forward to your reply..:-)

ankita said...

Hey Gaurang..I am new to TestNG..i have been given a task to run test cases using TestNG i did that but how to generate report that i dnt no..
so,can you please help me??

Looking Forward to your reply..:-)

Unknown said...

Hi Ankita,
If you have run you testcases through TestNG, reports are already generated in your project folder. Please check that.

Unknown said...

omg ..it's not my cup of tea

Rahul Raut said...

I am facing same issue as sasidhat
"throwing error for "includeantruntime"

warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds"

how to deal with this?

Unknown said...

@rahul
It's due to ANT 1.8 version. I have changed the code little bit.
Have added includeantruntime="false" in Javac task. not it shouldn't give this warning.

Unknown said...

Hey Gaurang..I am new to TestNG..i have been given a task to work with Visual studio(selenium web driver c#).So can i do this???
Thanks in advance

Life With Me- said...

Hi
I am looking for some way to run the TestNG suites, but with ReportNG HTML email format.

If i add the plugins to pom.xml and do mvn clean install, it works.

But i want to run test-suites independent of the build, i.e., i want to run just the test suite from command line or script and still get the reportNG HTML email.

Please let me know, if there is a way or an alternate contact person for this support

Thanks
-Rakesh

Post a Comment