Sunday, March 25, 2012

Ant task/script for generating TestNg report

Here is an example explaining an ant task for generating TestNG report after execution of classes defined in TestNG xml file.
  1. <property name="src.dir" value="src" />
  2. <property name="lib.dir" value="lib" />
  3. <property name="log.dir" value="logs" />
  4. <property name="build.loc" value="build" />
  5. <property name="classes.dir" value="${build.loc}/classes" />
  6. <property name="reports.dir" value="${build.loc}/reports" />
  7. <property name="testNG.report" value="${reports.dir}/TestNG" />
  8. <property name="suite.dir" value="suite" />
  9. <!-- Class-Path -->
  10. <path id="classpath">
  11.     <pathelement location="${classes.dir}"/>
  12.     <fileset dir="${lib.dir}" includes="*.jar"/>
  13. </path>
  14. <!-- Delete directories that are not needed -->
  15. <target name="delete-dir" >
  16.         <delete dir="${build.loc}"/>
  17.         <echo> /* Deleted existing Compiled Directory Classes */ </echo>
  18. </target>
  19. <!-- Create Directories -->
  20. <target name="create-source-dir" depends="delete-dir">
  21.     <mkdir dir="${classes.dir}"/>
  22.     <mkdir dir="${testNG.report}"/>
  23.     <echo> /* Created Directories */ </echo>
  24. </target>
  25. <!-- Compiling Tests -->
  26. <target name="compile-classes" depends="create-source-dir">
  27.     <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  28.         <classpath refid="classpath"/>
  29.     </javac>
  30.     <echo> /* Compiled Directory Classes */ </echo>
  31. </target>
  32. <!-- Running Tests and TestNG report generation -->
  33. <target name="testNGreport" depends="compile-classes">
  34.     <taskdef resource="testngtasks" classpathref="classpath"/>
  35.     <testng classpathref="classpath" outputDir="${testNG.report}" haltOnfailure="true">
  36.           <xmlfileset dir="." includes="${suite.dir}/testng.xml" />
  37.     </testng>
  38.     <echo> /* Run Directory Classes */ </echo>
  39. </target>
By the above target 'testNGreport' you can load TestNG task in ant and execute you selenium tests. It is just that ant would access your testng.xml file to execute tests. This is same as how you be executing tests from with eclipse using TestNG - Eclipse plugin. 
Below is the testNG file that is used in the above build file.
//TestNG.xml
  1. <suite name="Suite1" verbose="1" >
  2.   <test name="Regression1">
  3.     <classes>
  4.       <class name="test.sample.ExampleTest1"/>
  5.       <class name="test.sample.ExampleTest2"/>
  6.     </classes>
  7.   </test>
  8. </suite>

Monday, March 19, 2012

Ant task/script for generating JUnit report

Here is an example explaining an ant task for running compiled classes and generating JUnit report after execution of classes.
  1. <target name="run-classes" depends="compile-classes">
  2.     <!-- Define the Ant tasks for running classes -->
  3.     <junit fork="yes" printsummary="no" haltonfailure="no">    
  4.         <batchtest fork="yes" todir="${build.loc}/reports" >
  5.           <fileset dir="${classes.dir}">
  6.             <include name="**/.class" />
  7.           </fileset>
  8.         </batchtest>
  9.         <formatter type="xml" />
  10.         <classpath refid="classpath" />
  11.     </junit>
  12.    
  13.     <!-- Ant task for generating reports -->
  14.     <junitreport todir="${build.loc}/reports">
  15.         <fileset dir="${build.loc}/reports">
  16.             <include name="TEST-*.xml"/>
  17.         </fileset>
  18.         <report todir="${build.loc}/reports"/>
  19.     </junitreport>     
  20.     <echo> Genereated JUnit Reports </echo>
  21. </target>

In the above ant target there are 2 sub-tasks i.e junit and junitreport.The junit task is for running test classes and junitreport is for generating JUnit report.

Explanation:
fork
     if true, it 'll run the tests in separate virtual machine.
printsummary:
     if yes, at the end of the test, a one-line summary will be printed.
haltonfailure:
     if yes, the build process will be stopped if the test fails.
The batchtest defines a number of tests based on pattern matching.
In this case formatter type is xml so an XML result will be output to result xml file

        The batchtest task of junit task 'll run all the tests classes that are in ${classes.dir} directory and produce xml files in the ${build.loc}/reports directory then the junitreport task take all the xml files that are starting with 'TEST-' and 'll generate report in the same directory.

Friday, March 16, 2012

Ant task for compiling classes

The following build.xml file contains 3 targets for deleting unnecessary directories, creating required directories and compiling classes in a specified directory.

//build.xml
  1. <project name="SampleBuildFile" default="compile-classes" basedir=".">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="build.loc" value="E:/temp/build" />
  5.     <property name="classes.dir" value="${build.loc}/classes" />
  6.     <!-- Setting ClassPath -->
  7.     <path id="classpath">
  8.         <pathelement location="${classes.dir}"/>
  9.         <fileset dir="${lib.dir}" includes="*.jar"/>
  10.     </path>
  11.     <!-- Delete directories that are not needed -->
  12.     <target name="delete-dir" >
  13.             <delete dir="${build.loc}"/>
  14.             <echo> /* Deleted existing Directories */ </echo>
  15.     </target>
  16.     <!-- Create Directories -->
  17.     <target name="create-dir" depends="delete-dir">
  18.         <mkdir dir="${classes.dir}"/>
  19.         <echo> /* Created Directories */ </echo>
  20.     </target>
  21.     <target name="compile-classes" depends="create-dir">
  22.         <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  23.             <classpath refid="classpath"/>
  24.         </javac>
  25.         <echo> /* Compiled Classes */ </echo>
  26.     </target>
  27. </project>
In the above build file,

              Property task is used to set a property by name and value which are case sensitive.The attribute 'name' represents name of property and 'value' represents value to the property. By using this we can have an advantage when we want any modification further on property values because value represented by property name is used through out the build file so no need to change the property value for all.

Path task is used to building class path required to execute different ant targets in the build file.

             Here by default the 'compile-classes' target 'll call first when the build file 'll run, but this target depends on 'create-dir' target so it 'll call 'create-dir' target before it run.Again the 'create-dir' depends upon 'delete-dir' so it 'll call the 'delete-dir' target before 'create-dir' run.Clearly, the order of execution 'll be 
                       delete-dir   =>  create-dir   => compile-classes

             In the 3rd target the 'javac' task 'll compile all the java classes that are in the directory specified by 'srcdir' attribute i.e 'src' directory and all the compiles classes 'll be stored in the directory specified by 'destdir' attribute i.e 'E:/temp/build/classes'

The <classpath> is used to specify the PATH references using path tasks 'id' attribute value.

Ant script for create or delete directory

Ant Task for Deleting directories:
Below is an ant task that deletes the directory specified to 'dir' attribute of delete tag.
  1. <target name="delete-dir" >
  2.     <delete dir="build}"/>
  3.     <echo> /* Deleted existing directory */ </echo>
  4. </target>

The delete task deletes 'build' directory including all the files and subdirectories of 'build'.
The echo task just prints the message in console.

  1. <target name="delete-dir" >
  2.     <delete>
  3.         <fileset dir="." includes="**/*.java"/>
  4.     </delete>
  5. </target>

Here the delete task deletes all files with the extension '.java' from the current directory(represented by '.') and any subdirectories of it.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build"/>
  4.     </delete>
  5. </target>
Here the deletetask deletes all files and subdirectories of 'build', including build itself.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build" includes="**/*"/>
  4.     </delete>
  5. </target>
Here the delete task deletes all files and subdirectories of build, without build itself.

Ant Task for Making Directory:
Below is an ant task for making required directories specified to 'dir' attribute of 'mkdir' task.
  1. <target name="create-dir" >
  2.     <mkdir dir="build"/>
  3.     <echo> /* Created Directory */ </echo>
  4. </target>

The mkdir task creates a directory 'build' in to the root directory.
Note: mkdir creates the directory if not exists only, if exists it does nothing.

Thursday, March 15, 2012

Getting Started with Apache Ant

Prerequisite: 
  1. Java environment installed (Make sure that environmental variables JAVA_HOME is set to the directory where your JDK is installed and JAVA_HOME  is added to PATH)
Installation:
  1. Download the latest stable version of Ant from the Ant web page http://ant.apache.org/
  2. Unzip the downloaded file into a directory in your local drive.
  3. Set environmental variables JAVA_HOME to your Java environment i.e to the directory where your JDK is installed.
  4. Set the ANT_HOME environment variable to the directory you unzipped Ant to or where you installed Ant.
  5. Add %ANT_HOME%/bin (for Windows) to your PATH
Creating build file:
It is an xml file generally referred by build.xml 
  1. Create a project(create package and test classes to be execute) or open existing project.
  2. Create a file namely 'build.xml' parallel to 'src' directory.
Apache Ant's build file structure:
  1. Build file contains one project and at least one (default) target.
  2. The main components of ant are property, path, target, task etc.
A sample build file is shown below:

//build.xml
  1. <project name="GoogleSearch" default="compile" basedir=".">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="build.loc" value="${build}" />
  5.     <property name="classes.dir" value="${build.loc}/classes" />
  6.    
  7.     <path id="classpath">
  8.         <pathelement location="${classes.dir}"/>
  9.         <fileset dir="${lib.dir}" includes="*.jar"/>
  10.     </path>
  11.        
  12.     <target name="compile">
  13.         <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  14.             <classpath refid="classpath"/>
  15.         </javac>
  16.         <echo> /* Compiled Directory Classes */ </echo>
  17.     </target>
  18. </project>

Run Build file:
We can run an ant file from
  1. IDE(in my case eclipse) by right click on build.xml -> Run as -> Ant Build
  2. Command line by ant <target>  
In first case the default target assigned to ant's project will run and in second case if you 'll run simply ant it 'll run the default target assigned or ant <target> 'll run the specific target you have given in command line.
        In the above sample build.xml, when it 'll run, it 'll execute the 'compile' target of build file which 'll compile all the classes under 'src' directory of the project.