August 25, 2015

ANT with Selenium Webdriver and TestNG (Selenium Ant TestNG)

In this article i would like to write something about Build tool ANT. http://ant.apache.org/

A build tool stores, executes automates all the process in a sequential order(Clean, SetPath, Compile and Execute) mentioned in Ant's build.xml file.


Benefits of ANT:

1. It is a simple build tool all configurations we can do from build.xml file.
2. Configuration is separate from application.
3. ANT is written in Java but its a platform independent tool. (Requires JDK)
4. We can create Jar files, Java doc.
5. Copy files to different locations
6. Can create directories and folders
7. Can create HTML reports.
8. It supports Junit4 and TestNG unit testing frameworks.

How to Setup ANT:

1. Download ant from  Download ANT





2. Unzip ant folder and place it in a folder (D:\ant)
3. Set ANT_HOME and Path in environmental variable.



4. Verify ANT. Open Command prompt and type in ant -version...shows you version of ANT.



Using ANT from Eclipse:

Download Sample ANT Selenium and TestNG 

project From Here..ANT-Selenium-TestNG


Folder Structure of Project Looks like this:



If you want to run ant from command prompt then use brlow command

1. Navigate to project directory
2. use command  --- ant run.

Below is build.xml file.




<project name="Experiment_Ant" basedir=".">

 <!-- Defining property -->

 <property name="project.dir" value="${basedir}" />
 <property name="build.dir" value="${basedir}/build" />
 <property name="jar.dir" value="${basedir}/lib" />
 <property name="src.dir" value="${basedir}/src" />
 <property name="ng.result" value="test-output" />

 <!-- Setting Classpath for jar files -->
 <target name="setClassPath">
  <path id="classpath_jars">
   <pathelement path="${basedir}/" />
   <fileset dir="${jar.dir}">
    <include name="*.jar" />
   </fileset>
  </path>
  <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
 </target>
 <!-- Loading Testng -->
 <target name="loadTestNG" depends="setClassPath">
  <taskdef resource="testngtasks" classpath="${test.classpath}" />
 </target>
 <!-- Deleting directories -->
 <target name="clean">
  <echo message="deleting existing build directory " />
  <delete dir="${build.dir}" />
 </target>
 <!-- Creating build folder to store compiled classes -->
 <target name="init" depends="clean,setClassPath">
  <mkdir dir="${build.dir}" />
 </target>
 <!-- Compiling java files -->
 <target name="compile" depends="clean,init,setClassPath,loadTestNG">
  <echo message="" />
  <echo message="compiling………." />
  <javac destdir="${build.dir}" srcdir="${src.dir}"
   includeantruntime="false" classpath="${test.classpath}" />
 </target>
 <target name="run" depends="compile">
  <testng classpath="${test.classpath}:${build.dir}">
   <xmlfileset dir="${basedir}" includes="testng.xml" />
  </testng>
 </target>

</project>

No comments:

Post a Comment