Monday, October 31

Executing Selenium TestNG tests using ANT

ANT is software build automating tool for Java code. Uses XML file named ‘build.xml’ as the input for the build tasks. If one wants to run TestNG task using ANT, XML should contain information for testng calls and this can be built in two ways
1. By defining a external task of type TestNG and referring this task in another target ANT call further down in the code
2. By defining as Java command in ANT XML
The command line syntax to run TestNG task is   java org.testng.TestNG testng1.xml [testng2.xml testng3.xml ...]. The second option takes this approach of calling testNG using command line syntax but using ANT. Lets have look at the first option here in this post

Defining TestNG task
The ANT xml tag to define external Task is by <taskdef> and this takes two attributes @name and @classname. And the tag <Classpath> includes the TestNG jar from the defined path .Hence below is the way to define a ANT task of type TestNG
 1:    <taskdef name="testng" classname="org.testng.TestNGAntTask" > 
2:      <classpath>  
3:        <pathelement location="lib/testng-6.2.jar"/>  
4:      </classpath>  
5:    </taskdef>
The path in line 3 for me is lib/testng-6.2,jar. This is because have this TestNG jar file in lib folder and my build xml is same folder as my lib folder is as shown is figure beside. so the value for location attribute of pathelement should be relative path of TestNG jar file to your build file

Once the above taskdef is done, next define the actual target for ANT call. Any ANT task are referred by tags <target>. Within this <target> container, we will create a output directory for the testng reports, and then call the testng task that we just have defined

1:    <target name="runTest">  
2:      <mkdir dir="testng_output"/><!-- Command to create the output directory. -->  
3:      <testng outputdir="testng_output" classpathref="class.path">  
4:        <xmlfileset dir="." includes="TestNG1.xml"/>   
5:        <xmlfileset dir="." includes="TestNG2.xml"/>   
6:      </testng>  
7:    </target>

In above XML, the attribute classpathref actually refers to container of type path having name as ‘class.path’ . So for you , you need to define this path as per below xml before the target tag  “runTest”.  Below is the xml for defining path containing all classes and jar files from my project folder. And below is the folder structure I have for my project


1: <property name="src" location="src" />
2:  <property name="bin" location="bin" />  3:  <property name="libs" location="lib" />
3:  <path id="class.path">
4:      <pathelement location="${libs}/testng-6.2.jar" />
5:      <pathelement location="${libs}/selenium-java-client-driver.jar" />
6:      <pathelement location="${libs}/selenium-server-standalone-2.6.0.jar" />
7:      <pathelement location="${bin}" />
8:  </path>

Final XML

Hence the final XML, after defining project name etc, it should look like this below

1:  <project basedir="." default="runTest" name="Ant file for TestNG">
3:  <property name="src" location="src" />
4:  <property name="bin" location="bin" />
5:  <property name="libs" location="lib" />
7:  <path id="class.path">
8:      <pathelement location="${libs}/testng-6.2.jar" />
9:      <pathelement location="${libs}/selenium-java-client-driver.jar" />
10:      <pathelement location="${libs}/selenium-server-standalone-2.6.0.jar" />
11:      <pathelement location="${bin}" />
12:  </path>
14:    <taskdef name="testng" classname="org.testng.TestNGAntTask">
15:      <classpath>
16:        <pathelement location="lib/testng-6.2.jar"/>
17:      </classpath>
18:    </taskdef>
20:     <target name="runTest">
22:      <mkdir dir="testng_output"/><!-- Create the output directory. -->
24:      <testng outputdir="testng_output" classpathref="class.path">
25:        <xmlfileset dir="." includes="TestNG1.xml"/>
26:        <xmlfileset dir="." includes="TestNG2.xml"/>
27:      </testng>
29:    </target>
30:  </project>

Now to execute this ant file, go to the directory where the build.xml resides in a new command line window, And then enter command “ant” and ant will execute the testng tests. If your ant file is having different name than build.xml, then you need to run command “ant –f < yourfilename>” to run the testng tests. Since we defined the default task is “runTest” in project attributes, running ant without specifing target name will execute default target (here runTest). For more options on ant command check this link for running ant


Related Posts Plugin for WordPress, Blogger...