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>



18 comments:

  1. Hi! Nice blog. I have a doubt, when i am using this build.xml for my project, i am getting a message that, couldn't find the library.

    Can you help me with that..Do i need to add any jar file apart i have added, the jars mentioned in xml file too.

    ReplyDelete
  2. Buildfile: C:\Users\Amar\workspace\SeleniumAssertion\Build.xml
    clean:
    [echo] deleting existing build directory
    clean:
    [echo] deleting existing build directory
    init:
    [mkdir] Created dir: C:\Users\Amar\workspace\SeleniumAssertion\build
    [echo] build logged to build_20-03-2013_(14-37-20).log
    setClassPath:

    BUILD FAILED
    C:\Users\Amar\workspace\SeleniumAssertion\Build.xml:12: C:\Users\Amar\workspace\SeleniumAssertion\lib does not exist.

    Total time: 373 milliseconds


    This is the error.

    ReplyDelete
  3. Hi,

    The ANT build file works fine for Selenium RC but

    I am having trouble in running the Webdriver(Selenium 2)
    using the ANT build.xml file.

    it works fine till compiling the java files and then when i run it simply skips all the test cases and not showing any error.

    it is not even opening any browsers

    so i want to know did the above build file work for Webdriver test case also?

    please help me

    ReplyDelete
    Replies
    1. HI Arun i am also facing the same issue ,if the issue is solved please let me know..

      Delete
    2. Raghu,

      Did you specify testng.xml name in build.xml file of yours?

      Delete
    3. Thanks for the reply.Yeah i did mentioned it.,

      Delete
    4. This comment has been removed by the author.

      Delete
  4. Hi,

    When iam using this Build.xml file in my webdriver Project.Iam getting only this message itself..

    Buildfile: C:\Users\Admin\workspace\TestNG\src\Build.xml
    clean:
    [echo] deleting existing build directory
    BUILD SUCCESSFUL
    Total time: 282 milliseconds


    I didn't get xslt reports and report mailing..

    can u help me..

    ReplyDelete
  5. Hi NX,

    i tried the above code and was able to run the test suite without any errors. But i am not able to see the zip file created and email sent. Kindly advise.

    Regards,
    Sachin

    ReplyDelete
  6. Hi,
    I am getting below error. Kindly support


    Buildfile: D:\Java Workspace\ExampleAnt\Build.xml
    setClassPath:
    setClassPath:
    loadTestNG:
    init:
    [mkdir] Created dir: D:\Java Workspace\ExampleAnt\build
    [echo] build logged to build_09-12-2014_(12-18-31).log
    clean:
    [echo] deleting existing build directory
    [delete] Deleting directory D:\Java Workspace\ExampleAnt\build
    clean:
    [echo] deleting existing build directory
    init:
    [mkdir] Created dir: D:\Java Workspace\ExampleAnt\build
    [echo] build logged to build_09-12-2014_(12-18-31).log
    setClassPath:
    loadTestNG:
    compile:
    [echo] classpath:D:\Java Workspace\ExampleAnt:D:\Java Workspace\ExampleAnt\lib\testng-6.8.5.jar
    [echo] compiling..........
    [javac] D:\Java Workspace\ExampleAnt\Build.xml:35: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

    BUILD FAILED
    D:\Java Workspace\ExampleAnt\Build.xml:35: Class not found: javac1.8

    Total time: 313 milliseconds

    ReplyDelete
    Replies
    1. Hi Neela,

      Can you please try renaming the folder 'Java Workspac' to 'JavaWorkspac' and try the build..

      Delete
  7. Hi NX,
    Thanks for reply.Error displayed on executing BUILD.xml in eclipse


    Error:

    Buildfile: D:\Java Workspace\ExampleAnt\Build.xml
    init:
    [echo] build logged to build_09-12-2014_(18-14-19).log
    setClassPath:
    loadTestNG:
    setClassPath:
    clean:
    [echo] deleting existing build directory
    [delete] Deleting directory D:\Java Workspace\ExampleAnt\build
    clean:
    [echo] deleting existing build directory
    init:
    [mkdir] Created dir: D:\Java Workspace\ExampleAnt\build
    [echo] build logged to build_09-12-2014_(18-14-19).log
    setClassPath:
    loadTestNG:
    compile:
    [echo] classpath:D:\Java Workspace\ExampleAnt:D:\Java Workspace\ExampleAnt\lib\testng-6.8.5.jar
    [echo] compiling..........
    [javac] D:\Java Workspace\ExampleAnt\Build.xml:35: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

    BUILD FAILED
    D:\Java Workspace\ExampleAnt\Build.xml:35: Unable to find a javac compiler;
    com.sun.tools.javac.Main is not on the classpath.
    Perhaps JAVA_HOME does not point to the JDK.
    It is currently set to "C:\Program Files\Java\jre8"

    Total time: 345 milliseconds

    ReplyDelete
  8. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    RPA courses in bangalore

    ReplyDelete
  9. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.
    I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Java training in Chennai
    Java training in Bangalore
    Java online training
    Java training in Pune






    ReplyDelete
  10. Really it was an awesome article about JAVA, very interesting to read.You have provided an nice article,Thanks for sharing.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete