Tuesday, 13 March 2012

Build XML for Selenium+TestNG+ANT framework

I have developed a build.xml that can handle the the build of a test suite and do the reporting. It even send the report as mail. For reporting we are using TestNG-XSLT

<project name="Automation" default="clean" basedir=".">
    <property name="build.dir" value="${basedir}/build"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="src.dir" value="${basedir}/src"/>
    <property name="browser" value="/home/nxavier/Downloads/firefox/firefox"/>
    <target name="setClassPath">
        <path id="classpath_jars">
            <pathelement path="${basedir}/" />
            <fileset dir="${lib.dir}" includes="*.jar" />
        </path>
        <pathconvert pathsep=":" property="test.classpath" refid="classpath_jars" />
    </target>
    <target name="loadTestNG" depends="setClassPath">
        <taskdef resource="testngtasks" classpath="${test.classpath}"/>
    </target>
    <target name="init">
        <mkdir dir="${build.dir}"/>
        <tstamp>
            <format property="timestamp" pattern="dd-MM-yyyy_(HH-mm-ss)"/>
        </tstamp>
        <property name="build.log.dir" location="${basedir}/buildlogs"/>
        <mkdir dir="${build.log.dir}"/>
        <property name="build.log.filename" value="build_${timestamp}.log"/>
        <record name="${build.log.dir}/${build.log.filename}" loglevel="verbose" append="false"/>
        <echo message="build logged to ${build.log.filename}"/>
    </target>
    <target name="clean">
        <echo message="deleting existing build directory"/>
        <delete dir="${build.dir}"/>
    </target>
    <target name="compile" depends="clean,init,setClassPath,loadTestNG">
        <echo message="classpath:${test.classpath}"/>
        <echo message="compiling.........."/>
        <javac destdir="${build.dir}" srcdir="${src.dir}" classpath="${test.classpath}"/>
    </target>
    <target name="runTests" depends="compile">
        <testng classpath="${test.classpath}:${build.dir}">
            <xmlfileset dir="${basedir}" includes="Debug.xml"/>
        </testng>
    </target>
    <target name="report" depends="runTests">
        <delete dir="${basedir}/testng-xslt"/>
        <mkdir dir="${basedir}/testng-xslt"/>
        <xslt in="${basedir}/test-output/testng-results.xml"
        style="${basedir}/src/xslt/testng-results.xsl" out="${basedir}/testng-xslt/index.html" processor="SaxonLiaison">
            <param expression="${basedir}/testng-xslt/" name="testNgXslt.outputDir"/>
            <param expression="true" name="testNGXslt.sortTestCaseLinks"/>
            <param expression="FAIL,SKIP,PASS,BY_CLASS" name="testNgXslt.testDetailsFilter"/>
            <param expression="true" name="testNgXslt.showRuntimeTotals"/>
            <classpath refid="classpath_jars"/>
        </xslt>
    </target>
    <target name="RunAndViewReport" depends="report">
        <exec executable="${browser}" spawn="yes">
       <arg line="'${basedir}/testng-xslt/index.html'" />
      </exec>
     </target>
    <target name="sendMail" depends="RunAndViewReport">
        <zip destfile="${basedir}/testng-xslt/Report.zip" basedir="${basedir}/testng-xslt"/>
        <mail mailhost="smtp.gmail.com" mailport="465" subject="Notification of TESTNG build" ssl="false" user="tester@gmail.com" password="password">
            <from address="tester@gmail.com"/>
            <to address="tester@gmail.com"/>
            <message>The build has finished. A details report of this build is aatched</message>
            <attachments>
                <fileset dir="testng-xslt">
                    <include name="**/*.zip"/>
                </fileset>
            </attachments>
        </mail>
    </target>
</project>