Emailing XSLT Report in WebDriver/Java Using ant build.xml automatically

Emailing XSLT Report in WebDriver/Java Using ant build.xml automatically
1- Read installation of Ant on your system from here




2- Configuration for XSLT Report

1- Download testng-xslt-1.1.2-master.zip file from this location
2
– Unzip testng-xslt-1.1.2-master.zip file
3- Copy all jar files and place them in jar library folder in your project.
4- Copy the testng-results.xsl from the testng-xslt folder(path: XSLT jartestng-xslt-1.1.2-mastertestng-xslt-1.1.2-mastersrcmainresources) and place in your project folder.
5- Create build.xml in your project under eclipse. To read how to create build.xml read here
6
– Modify xml code in build.xml
Here is the build.xml to create xslt report

===================================build.xml=======================
<project name=”Automation” default=”makexsltreports” 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>

<!– adding XSLT report target to produce XSLT report –>
<target name=”makexsltreports” depends=”run”>
<delete dir=”${project.dir}/XSLT_Reports/output”>
</delete>
<mkdir dir=”${project.dir}/XSLT_Reports/output”/>

<xslt in=”${ng.result}/testng-results.xml” style=”src/dummy/testng-results.xsl”
out=”${project.dir}/XSLT_Reports/output/index.html” classpathref=”classpath_jars” processor=”SaxonLiaison”>
<param name=”testNgXslt.outputDir” expression=”${project.dir}/XSLT_Reports/output/”/>
<param name=”testNgXslt.showRuntimeTotals” expression=”true”/>
<param expression=”true” name=”testNgXslt.sortTestCaseLinks” />
<param expression=”FAIL,SKIP,PASS,CONF,BY_CLASS” name=”testNgXslt.testDetailsFilter” />
</xslt>
</target>
</project>

===========End of build.xml======================





Since in above build.xml we have used testng it means we need to add testng.xml(you may put nay name of you xml)
===============testng.xml=====================

<?xml version=”1.0″ encoding=”UTF-8″?>
<suite name=”Ant Suite”>
       <test name=”Generictest”>
       <packages>
<package name=”dummy” />
</packages>                                                             
       </test>
 </suite>
======================testng.xml==============
7- Launch Command prompt, Go to your Project Location, for Instance  type “ant makexsltreports”
8. Find the TestNG-XSLT report (index.html) in the target Location specified in the Build.XML.
3- Sending mail through ant
1- Download javax.mail.jar.zip from here  and javax.activation.jar.zip from here
2- Unzip both file
3- Copy javax.mail.jar and javax.activation.jar and go to you Ant home folder(Where you have unziped your Ant zip file in your directory), search lib folder and paste both jar files.
4- Come to build.xml file and add line
Here I am adding makexsltreports as dependencies since we are trying to send the zipped report of XSLT in to mail
======================Javamail api integration in ant=================

<!– using javax.mail.jar and javax.activation.jar trying to send report as zip file –>

<target name=”sendMail” depends=”makexsltreports”>

<!– Compressing all the output file of XSLT report –>
<zip destfile=”${project.dir}/XSLT_Reports/output.zip” basedir=”${project.dir}/XSLT_Reports/output” />

<mail
tolist=”List of mail id separated with comma”
from=”[email protected]
subject=”Email subject”
mailhost=”smtp.gmail.com”
mailport=”465″
ssl=”true”
user=”[email protected]
password=”password”>
<attachments>
<fileset dir=”${project.dir}/XSLT_Reports/”>
<include name=”**/*.zip”/>
</fileset>
</attachments>

</mail>
</target>

===========================================================

So here is complete build.xml through which we can generate xslt report and can also send zipped report on some maild ids.

<project name=”Automation” default=”sendMail” 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>
<!– adding XSLT report target to produce XSLT report –>
<target name=”makexsltreports” depends=”run”>
<delete dir=”${project.dir}/XSLT_Reports/output”>
</delete>
<mkdir dir=”${project.dir}/XSLT_Reports/output”/>
<xslt in=”${ng.result}/testng-results.xml” style=”src/dummy/testng-results.xsl”out=”${project.dir}/XSLT_Reports/output/index.html” classpathref=”classpath_jars” processor=”SaxonLiaison”>
<param name=”testNgXslt.outputDir” expression=”${project.dir}/XSLT_Reports/output/”/><param name=”testNgXslt.showRuntimeTotals” expression=”true”/>
<param expression=”true” name=”testNgXslt.sortTestCaseLinks” />
<param expression=”FAIL,SKIP,PASS,CONF,BY_CLASS” name=”testNgXslt.testDetailsFilter” />
</xslt>
</target>
<!– using javax.mail.jar and javax.activation.jar trying to send report as zip file –>
<target name=”sendMail” depends=”makexsltreports”>
<zip destfile=”${project.dir}/XSLT_Reports/output.zip” basedir=”${project.dir}/XSLT_Reports/output” />
<mail
tolist=”[email protected]
from=”[email protected]
subject=”Email subject”
mailhost=”smtp.gmail.com”
mailport=”465″
ssl=”true”
user=”[email protected]
password=”password”>
<attachments>
<fileset dir=”${project.dir}/XSLT_Reports/”>
<include name=”**/*.zip”/>
</fileset>
</attachments>
</mail>
</target>
</project>

here in this example I have taken gmail.com but you may take some other SMTP server and port according to your need.

29 comments

  • Madiraju Chaitanya

    Hi Dwarika Dhish Mishra Ji, Thanks a Lot for this post.I was struggling with the above concept for the last couple of days(I could not find the proper build.xml,testng.xml and *.xsl files).I saw your post today and it was very helpful. I Thank You for that. One more small favor I need. Do you have any sample projects w.r.t Keyword Driven,Data Driven and Hybrid Driven Projects(each one,I need them) which can be used to understand the project structure and practice(I guess I have asked you…in the past.I tried to find these projects in Google,but could not find the proper ones,with explanation) the same. If you have them,kindly share it with me,or,If you know a location(URL…like Github) where I can find these sample projects with these framworks,Please kindly let me know. I am already working on some automation projects,but I need them to practice more to get a greater understanding of the same. Kindly do the needful.

    Thanks and Regards, Chaitanya

  • madirajukrishnachaitanya

    Hi Mishra Ji,Nice Article.Thanks for sharing this content with us.

  • Hi Hi Mishra , Its been in reading through your article. Much informative. Keep writing

  • superb ji thanks for the established 🙂

  • In the above build.xml ,how do i store the results of Testng in sepereate Time -stamped folders so that the previous results are not overwritten by new ones .

  • My build.xml has executed successully and generated XSLT reports.I am not getting XSLT reports email.


  • <!–

    –>

    ant run will execute the test

    hi i am able generate xslt reports but iam not able to send mail.. i am getting an error
    sendMail:
    [mail] Sending email: test mail
    [mail] Failed to send email: Could not connect to SMTP host: smtp.gmail.com
    , port: 465

    BUILD FAILED
    C:\Users\Raghu\workspace\Selenium\build.xml:108: Problem while sending mime mail
    :

    Total time: 1 second

    • Then please try this code with some other prover like smtp.live.com by using some of your hotmail email id or outlook id…if it is working with these, then its not the problem of ant build.xml it might be of gmail. because i am getting mail for the same.

  • Hi,
    how to generate customized pie chart and bar chart from xslt report.

  • How to create custom pie chart and bar chart and present in html.

  • Hi Mishra,

    I have added mail.jar and activation.jar into my project folder.When i run build.xml file i am getting the below. Kindly,suggest how to resolve this issue.

    [mail] Failed to send email: javax.mail.internet.MimeMessage

    BUILD FAILED
    E:\GIT_Projects\GooruAutomation\GooruAutomation\build.xml:122: java.lang.ClassNotFoundException: javax.mail.internet.MimeMessage

    • Please add Javax.mail and Javax.activate jar in class path of Ant .

      This can be achieved in two ways
      1- Just go to ANT_HOME location navigate to lib folder and put above mentioned Jar and open the command prompt and run the program by writing ant sendMail

      2- This is used to set classpath in Eclipse

      Go to Window–> Preferences –> Ant (Click on triangle icon to expend Ant options) –> Click on Runtime –> Select Classpath tab –> select Global Entries –> Click on Add external jar and add above mentioned jar and click ok and then select the build.xml and run as you are trying to run your program and it will work..

      Hopefully i have answered you query and if still have problem then please write to me at [email protected]

      • Hello Dwarika Dhish Mishra,

        Thank you for ur suggestion.

        I was also unable to send mail bcos of the above mentioned error. After reading ur post , I did the above setting in eclipse and also in Ant, but still I am facing the same problem. The error is :
        Failed to send email: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1

        Please help me resolve this.

        Thanks in advance.

        Regards
        Prabahar

      • Hi Dwarikadhish,
        I have followed all steps exactly and now there are no error messages but still no mail is sent by the code. Can you please help?

        • Have you added your jars related to mail in your ant classpath

          • Hi,
            Yes you are correct. jars were placed at wrong path. Now I am able to send mails. Thanks.
            One more concern….Mails are send while script is executed from command prompt but if when same is executed from eclipse then IOException occurs.

            Thanks,
            MaheshP

          • Place ur jar in ant build path and it will work from here as well

          • Hi Mishra,

            I have placed the jars in lib folder of ant directory where i unzipped.

            can u tell me what is ant build path.

            But im still getting “The error is :
            Failed to send email: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1”

      • Hi Mr.Mishra,
        Thanks for the solution. It worked.

  • Hi Dwarika ,

    Thanks for sharing this content with us. Observed one issue, the report folders generated under the project with the date and time showing the previous report but received correct email via mail, this might be happens because we have not refreshed the project. Please help me.

    • Found the solution.

      Just cut the following lines

      and past it in between mail closing tag and target tag of mail .i.e.

  • Hi Divakar,
    Thanks for sharing sharing the content,I have successfully executed but the mail is not or creating any zip file for the attachment?

  • Hi Dwarika,

    I want to send only testng-xslt.html not full folder but unable to send. Only I am able to send testgn-xslt.zip folder…. Please comment.

    Thanks

  • Hi There

    I currently face an issue that the reports are not getting updated. Even after compliling and running tests again and then rI run the xslt report target to generate the report but the report is of the first run and is not getting updated. I do delete and create the directories again. Does anyone have any idea on this

  • Pingback: Error: cannot find symbol in XSLT + TestNG Report | DL-UAT

  • i am getting a error “problem while sending mime mail”…Please help me regarding this.I have followed the above steps exactly as told by you but then also i am unable to send mail because of this error.

  • i am getting a error “problem while sending mime mail”…Please help me regarding this.I have followed the above steps exactly as told by you but then also i am unable to send mail because of this error.Please help me fix this

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.