Friday, 4 April 2014

ANT

 Apache Ant is a Java based build tool from Apache Software Foundation. Apache Ant's build files are written in XML. In selenium Ant is used to compile test cases, run test cases , generate reports , email the reports from command line.

Install and Configure ANT
  1. Download the ANT binary distributions from link http://ant.apache.org/bindownload.cgi 
  2. Unzip the ANT and copy to C:\apache-ant folder
  3. Create a new Environment variable called ANT_HOME that points to the ANT folder (C:\apache-ant folder\apache-ant-1.8.4)
  4. Now select the path and click on 'Edit' button.
  5. Now give the semicolon (;) at the end and add ANT path up to bin folder (C:\apache-ant folder\apache-ant-1.8.4\bin)
  6. Click on OK
  7. Let us cross check if ANT is installed or not
  8. Open the command Prompt
  9. Enter 'ant -version' and hit enter
  10. Message should be displayed like 'Apache Ant(TM) version 1.8.4 compiled  on May 22 2012' 

How to write your own ANT file(build.xml) for your project

Ant's build file, build.xml should live in the project's base directory.

<?xml version='1.0'?>
<project name="Test" basedir="." default="email">
<property name="src" location="src"/>
<property name="target" location="target"/>
<property name="target.classes" location="${target}/classes"/>
<property name="reports" location="reports"/>
<property name="reports.testNg" location="${reports}/testNg" />
<property name="jars" location="lib"/>
<property name="zip" value="zip" />

 // Before writing the targets , we need to import testNg to this file. This can be done using Task Def
<taskdef resource="testngtasks">
        <classpath>
            <pathelement location="${jars}/testng-6.0.1-nobsh-noguice.jar"/>
            <pathelement location="${jars}/testng.jar"/>
        </classpath>
</taskdef>

// Now let us set the path. A path-like structure can include a reference to another path-like structure. We will include all the jar files here
<path id="loadJars">
        <fileset dir="${jars}" includes="*.jar"></fileset>
        <pathelement location="${target.classes}"/>
</path>

// First target should be clean the reports and targets folder. Below target will delete the directories
<target name="clean">
        <delete dir="${target}" />
        <delete dir="${reports}" />
        <delete dir="${zip}" />
</target>

 // Create the directories
<target name="setup" depends="clean" >
        <mkdir dir="${target}"/>
        <mkdir dir="${reports}"/>
        <mkdir dir="${target.classes}"/>
        <mkdir dir="${reprots.testNg}"/>
        <mkdir dir="${zip}"/>
</target>

// Compile the classes . Below target will compile the java source files and the class files are stored in the folder "classes". classpathref attribute is reference where the jar files exists for compiling the source files
<target name="compile" depends="setup">
        <javac srcdir="${src}" destdir="${target.classes}" classpathref="loadJars" debug="true" />
</target>

// Run the test cases. Below target will run the test scripts based on the tests exists in 'testng.xml' and generate the reports in folder "testNg".
<target name="run" depends="compile">
        <testng classpathref="loadJars" outputdir="${reprots.testNg}">
            <xmlfileset dir="" includes="testng.xml"/>
        </testng>
</target>

// Below target will zip the reports.
<target name="ZipReports" depends="run">
        <zip destfile="${zip}/testReport.zip" duplicate="preserve">
            <zipfileset dir="${reports}" />
        </zip>
</target>

// Below target will mail the zip reports to the email ids
 <target name="email" depends="ZipReports">
        <mail encoding="mime" mailhost="smtp.gmail.com" mailport="465"
            subject="***Subject of the Email***" ssl="on" user="***@****.com"
            password="****">
            <from address="****@****.com" />
            <cc address="****@****.com" />
            <message>
                    **Body of the Email**
            </message>
            <attachments>
                <fileset dir="${zip}" />
            </attachments>
        </mail>
</target>

</project>

No comments:

Post a Comment