Monday, 21 January 2013

XSLT- Generate Interactive reports on TestNG+ANT

XSLT stands for XML (Extensible Markup Language) Stylesheet Language for Transformations.
XSLT gives interactive(user friendly) reports with "Pie Chart"; but only on TestNG framework. It is better compared to ReportNG and ordinary TestNG reports. It uses the pure XSL for report generation and Saxon as an XSL2.0 implementation.

XSLT = XSL Transformations

Steps to Generate testng-xslt report:

1. Download testng-xslt.zip
2. Unzip and copy the testng-results.xsl from the testng-xslt folder(testng-xslt-1.1\src\main\resources) to your own project folder.
3. Now copy the saxon library from (testng-xslt-1.1\lib\saxon-8.7.jar) to your project lib folder.
4. Make sure Guice-x.x.jar is available in the build path. [As shown in my GIT https://github.com/prashanth-sams/Xslt_Ant_Testng_Example/tree/master/lib]
5. Modify your build.xml of ant and add the following target to it.


<?xml version="1.0" encoding="UTF-8"?>
        <project name="automation" default="Run" basedir=".">
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/build"/>
<property name="testng_output.dir" value="${basedir}/testng_output"/>

<!-- get class path, used for build -->
<property name="lib.dir" value="${basedir}/lib"/>
<path id="classpath">
        <fileset dir="${lib.dir}" includes="*.jar"/>
<pathelement location="build"/>
       </path>

<!-- create build folder, testng_output folder and remove old log file -->
<target name="init">
<delete dir="${build.dir}"/>
<delete dir="${testng_output}"/>
<delete dir="${basedir}/testng-xslt"/>
<mkdir dir="${build.dir}"/>
<mkdir dir="${basedir}/testng-xslt"/>
                <mkdir dir="${testng_output.dir}"/>
   
</target>

<!-- build all java file to class binary -->
<target name="compile" depends="init">
   <javac srcdir="${basedir}/src" destdir="${basedir}/build" classpathref="classpath" includeantruntime="false">
   <compilerarg value="-Xlint:unchecked"/>
   </javac> 
</target>

<!-- Config for testng -->
<taskdef name="testng" classname="org.testng.TestNGAntTask">
   <classpath>
    <pathelement location="lib/testng-6.8beta.jar"/>
   </classpath>
  </taskdef>
 

<!-- execute testng after compile, then generate report -->
<target name ="Run" depends="compile">

<!-- config testng -->
   <testng outputdir="${testng_output.dir}" classpathref="classpath" useDefaultListeners="true">

    <!-- execute testcase follow testng.xml -->
    <xmlfileset dir="." includes="testng.xml"/>
   
    <!-- generate report title -->
    <sysproperty key="org.uncommons.reportng.title" value="Test Automation"/>
    <sysproperty key="org.uncommons.reportng.escape-output" value="false"/>
   </testng>
</target>
  
<target name="xslt" depends="Run">
                 
   <xslt in="${basedir}/testng_output/testng-results.xml" style="${basedir}/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,CONF,BY_CLASS" name="testNgXslt.testDetailsFilter" />

            <param expression="true" name="testNgXslt.showRuntimeTotals" />

            <classpath refid="master-classpath">
            </classpath>
        </xslt>
    </target>

<property name="LIB" value="${basedir}/lib" />
<property name="BIN" value="${basedir}/bin" />
<path id="master-classpath">
<pathelement location="${BIN}" />
      <fileset dir="${LIB}">
         <include name="**/*.jar" />
      </fileset>
</path>

</project>


5. Create TestNG.xml file in your project with the following script for TestNG execution.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="package.classname"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

6. Now, try to run the command.

ant xslt



71 comments:

  1. Hi Prashanth,

    I am running the same script that you have given here.
    It is running and giving message as build successful.
    But I am not able to find the report generated folder testng-xslt any where.

    Please help on this.
    Thanks in advance.

    ReplyDelete
  2. Hi Prashanth,

    I fixed the issue. Your code previously seem to be pointing to wrong directory and also you are trying to run Report NG as well as Test NG.
    XSLT seem to be formatting only Test NG out put.

    Your blog is really nice for the beginners to work with reports.

    Thank you.

    ReplyDelete
  3. Thank you Suresh! I fixed the code; seems it is working for you right now.

    Enquire me for any issues

    Thank you!
    Prashanth Sams

    ReplyDelete
  4. Hi Prashanth,

    Thanks for such a informative blog..
    When i am using the above code, i am getting following error:

    java.lang.NoSuchMethodError: org.apache.tools.ant.types.Resource.getMagicNumber([B)I

    Please let me know how to resolve it.

    ReplyDelete
  5. Shankar, This might have happened due to version capatibility sickness; Please check your ANT version and update it.

    Environment Varibles
    Under System variables:

    variable name: ANT_HOME
    variable value: C:\Program Files\Apache Software Foundation\apache-ant-1.8.4

    Add ur Path
    C:\Program Files\Apache Software Foundation\apache-ant-1.8.4\bin;

    Check ANT version

    cmd > ant -version

    Please lemme know on any issues ;)

    Thank you!
    Prashanth Sams

    ReplyDelete
  6. hi Prashanth,

    Please let me know from where i can download Xslt code ?

    Thanks
    jagadeesh

    ReplyDelete
    Replies
    1. Jagadeesh, Try to use the above link for downloading xslt code now.

      Cheers!

      Delete
  7. Hi Prasanth sams

    very nice article.
    my build.xml is executing only the HtmlUnit driver.It is not even opening other browsers like firefox for chrome.

    can you please tell me what would be the reason?

    Thanks
    Aruna

    ReplyDelete
  8. Aruna, Can you able to launch test through Firefox on normal eclipse IDE using JUnit/TestNG ?

    If yes, Please send me your xml file to seleniumworks@gmail.com

    Cheers!

    ReplyDelete
  9. Hi Prashanth,

    I am getting unusual error, "[xslt] Unknown file:23:146: Fatal Error! Could not find function: if", however i am just using same xslt as on github site, but i am stuck , will be obliged if you could help me to get out of this confusion. Let me know if you need any more detail.
    Any help would be appreciated.

    Thanks,
    nitin
    nitinl@outlinesys.com

    ReplyDelete
    Replies
    1. Try to run the testng.xml first to make sure everything works fine.
      Then go with ant build.xml
      Checkout my example on GIT for newbies: https://github.com/prashanth-sams/Xslt_Ant_Testng_Example

      _
      Sams

      Delete
  10. Hi Prashanth,

    I have created an ant script with targets for my project automation. I want to generate report for it using xslt. Please help me how can I inculde this xslt code in my test xml file. I am running ant script in alinux server with the help of a shell script.

    ReplyDelete
  11. Hi,

    I am getting an error message (BUILD FAILED) when i run the build.xml using the code i got from the internet.

    Can someone help me

    ReplyDelete
    Replies
    1. Please try to run your testng.xml file first

      Delete
  12. am using selenium webdriver java build [ latest one]

    Framework : TestNg

    Editor Eclipse

    Firefox browser version : 22.0

    Chrome Browser version : 28.0.1500.72 m

    OS: Windows 7 Ultimate

    From Eclipse run option my test cases are running properly but When ever i am trying to run my webdriver test cases from command prompt using ANT

    For the chrome browser it showing alert related to manifest version and for Firefox browser its showing error related to Unable to bind to locking port 7054.

    Can any one give some suggestion what changes are required so that i can run my test cases properly from ant

    ReplyDelete
  13. I am getting the below error
    BUILD FAILED
    C:\Users\amodak\Vecna_Sample\Vecna_Test\build.xml:28: C:\Users\amodak\Vecna_Samp
    le\Vecna_Test\LIB does not exist.

    In eclipse I have created 3 Library namely
    1. JRE System Library - Pointing to JRE jars in C:\Program Files\Java\jre7\lib
    2. TestNG - pointing to D:\Softwares\eclipse\plugins\org.testng.eclipse_6.8.6.20130607_0745\lib\testng.jar
    3. LIB - D:\Selenium LIB\selenium-java-2.33.0\selenium-2.33.0\selenium-2.33.0\selenium-2.33.0\libs\Selenium jars

    Please suggest the needful

    ReplyDelete
    Replies
    1. Ayan, please make sure all the lib files are there in your classpath.
      Please check my git https://github.com/prashanth-sams/Xslt_Ant_Testng_Example
      Download, import in Eclipse and run them

      _
      Sams

      Delete
  14. Team ,

    if you have idea please let me know

    Run:
    [testng] Exception in thread "main" com.google.inject.ProvisionException: Guice provision errors:
    [testng] 1) null returned by binding at org.testng.internal.TestNGGuiceModule.provideObjectFactory()
    [testng] but org.testng.internal.Configuration.m_objectFactory is not @Nullable
    [testng] while locating org.testng.IObjectFactory
    [testng] for field at org.testng.internal.Configuration.m_objectFactory(Configuration.java:9)
    [testng] while locating org.testng.internal.Configuration
    [testng] at org.testng.internal.TestNGGuiceModule.configure(TestNGGuiceModule.java:26)
    [testng] while locating org.testng.internal.IConfiguration
    [testng] 1 error
    [testng] at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:987)
    [testng] at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1013)
    [testng] at org.testng.TestNG.getConfiguration(TestNG.java:1128)
    [testng] at org.testng.TestNG.createSuiteRunner(TestNG.java:1031)
    [testng] at org.testng.TestNG.createSuiteRunners(TestNG.java:1017)
    [testng] at org.testng.TestNG.runSuitesLocally(TestNG.java:949)
    [testng] at org.testng.TestNG.run(TestNG.java:874)
    [testng] at org.testng.TestNG.privateMain(TestNG.java:1178)
    [testng] at org.testng.TestNG.main(TestNG.java:1137)
    [testng] The tests failed.
    BUILD SUCCESSFUL

    ReplyDelete
    Replies
    1. This issue is due to GUICE jar file not there in location..please download Guice jar and place in exact location..Try..

      Delete
  15. Hi Prashant,
    please can you tell me how to delete Old output folder under the TestNG Report.

    ReplyDelete
    Replies
    1. It will automatically delete all the old files generated in previous run; I have set it in build.xml.
      Hope you have gone through that.
      _
      Sams

      Delete
  16. Sir (lib/testng-6.8beta.jar) this file path i am not understand what is this path its fix ya change my pc path seting

    ReplyDelete
    Replies
    1. You don't need to worry about this jar file; Cos, the latest selenium servers comes with in-built TestNG libraries.

      _
      Sams

      Delete
    2. Hi Prashanth,

      I am getting taskdef class org.testng.TestNGAntTask can not be found error while compilation.
      Can you please help me out

      Delete
  17. HI prashant,
    what is the use of configuring testng and passing className as





    where is the this TestNGAntTask class present?
    after removing the code, everything working fine,build was successfull.but i am not getting xslt report? what would be the reason?

    ReplyDelete
    Replies
    1. Khamer, It allows to configure TestNG when you try to execute the tests using ANT.
      May be you're missing one of the above mentioned steps..I've explained everything in a sample project on github
      https://github.com/prashanth-sams/Xslt_Ant_Testng_Example

      _
      Prashanth Sams

      Delete
  18. Hello Prashanth,
    Very nice tutorial. Is it possible to print user defined messages in XSLT reoprt. For example If i am executing Login test, I want to print What all steps performed inside that test. Whether that step was successful or not.

    Reporter.Log shows logs for Complete Test suite. But I want to have cusomised for each testcase

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Hi
    Prashanath
    very use full for every selenium people
    please can u tell where I can store testng-results.xsl file in my project
    I am using java 1.7 and selenium Web driver 2.37 this build.xml provided by u it will work or not please replay me I am waiting ur answer

    ReplyDelete
    Replies
    1. Ranjan, you can store the .xsl file inside your project folder.

      Delete
  21. Hi Prashant,
    Thanks for the blog. I used this in my project to generate xslt report, but now I want to send this report in the email body. Since the index.html file generated here has dependency on many other html files in output folder, is there any way to send this in email body?
    Your help is much appreciated.

    ReplyDelete
    Replies
    1. Hi Vijay, I will make sure "Emailing XSLT Test reports - Ant" ASAP in this post.

      Delete
  22. Hi Prashanth,Wonderful Post.Thanks a LOT for sharing this information with us.Please continue with your Great Work.You are sharing your knowledge and experience with us.May GOD BLESS YOU...

    ReplyDelete
  23. Hi Prashanth Sams, I have tried to configure the project that you shared with us in the following url(https://github.com/prashanth-sams/Xslt_Ant_Testng_Example) and it worked for me after initial glitches. We have to update the Selenium Jars to latest ones and try again.It will work.Thank You for your support for beginners like us.Please keep up the Good Work.May GOD BLESS You...

    ReplyDelete
  24. Hi Prashanth Sams, I ran the above script as per what you have given.After making some changes(like updating the jar files),it ran well.Thank You for guiding us.Please keep up the Good Work.May GOD BLESS YOU... :)

    ReplyDelete
  25. Am using selenium webdriver (ANT+TestNG+Java).How do I save results of TestNG with time-stamp and build no.(testng-results-Build no:DD-MM-YYYY_HH-MM-SS.xml),without overwriting previous results. Could you please share the build.xml code?

    ReplyDelete
  26. Hi Prashanth,

    I really appreciate the effort you put in all your blogs.

    I am not using Ant , Eclipse+Webdriver+TestNg , is it possible to generate xslt reports ? if yes, could you please guide me.

    Regards,
    Nihir

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. Hi Prashant ,

    when I run the code, I am getting an error -"No srcDir specified" in the below line-
    testng outputdir="${testng_output.dir}" classpathref="classpath" useDefaultListeners="true"

    Please suggest me what could be the possible way to get rid of this error.

    Thanks,
    Sayantika

    ReplyDelete
  29. Hi,
    I am using XSLT reports, But facing an issue
    1) When I open XSLt reporst with Firefox it works fine
    2) When I open it from Chrome the checkbox like Failed passed, Skipped are not working.

    Please let me know the solution.

    ReplyDelete
  30. Hi Prashant,

    We have a small requirement asked by our client that is

    We are able to generate the high level testng report with the help of .xsl file and even we are mailing to stake holders. But now they are asking us to keep the pie diagram of the high level report in the mail body.

    Is there any way we can fetch and keep the pie diagram in the mail body.

    Thanks in advance,
    Yatheesh

    ReplyDelete
    Replies
    1. Yatheesh,

      There is no existing reporting system for such requirements. Currently, I am creating a new report for Selenium called SRF, Selenium Reporting Framework. I will try to implement them in it. Stay Tuned!!

      _
      Sams

      Delete
  31. Hi Prashanth,

    Very nice blog,it makes me to create a XSLT Report very easy, Thanks a lot

    ReplyDelete
    Replies
    1. Thank you :) Stay Tuned for SRF, Selenium Reporting Framework.

      Delete
  32. Hi I am Completey New to Automation and trying to my way to figure things around.

    I have few tests written in Test NG and looking out for generating a xlst report, i have ant installed in my computer

    Now what happens can i generate a Ant Build .xml from eclipse or do i have to code it, please help me out on how to proceed further.

    ReplyDelete
  33. Hi Prashanth,
    I trie your blog. It is very informative. I do same .
    But my build is nOT successful.
    It gives error:


    BUILD FAILED
    C:\Eclipse\SBC Regression\SBC\build.xml:33: taskdef class org.testng.TestNGAntTask cannot be found
    using the classloader AntClassLoader[]

    Can you please tell me what may be the cause

    ReplyDelete
  34. Hi Prasanth,

    I am getting below error.

    I added all jars in testng.xml its working absolutely fine. but build.xml i am getting below error...
    [javac] Compiling 2 source files to D:\Selenium_Automation\Selenium_Practice\TestNG_XSLT\build
    [javac] D:\Selenium_Automation\Selenium_Practice\TestNG_XSLT\src\report\Google.java:5: error: package org.openqa.selenium does not exist
    [javac] import org.openqa.selenium.WebDriver;
    [javac] ^
    [javac] D:\Selenium_Automation\Selenium_Practice\TestNG_XSLT\src\report\Google.java:6: error: package org.openqa.selenium.firefox does not exist
    [javac] import org.openqa.selenium.firefox.FirefoxDriver;
    [javac] ^


    BUILD FAILED.

    Please help me in this....

    ReplyDelete
  35. Hi Prashanth,

    Thanks for the blog just joined here.
    What exactly in Reporter output contains on above Graph?

    ReplyDelete
  36. Hi,

    It was really helpful. But my query is how can I view browser or operating system used in this xslt report?

    My client wants to see parameters like os, browser details in the report.

    Looking forward for your reply!

    Thanks,

    ReplyDelete
  37. Hi I am copying your complete code to build.xml file. and running through RUN TEST (windows batch file ), its not running. Please help me for this.

    ReplyDelete
  38. i am also getting the same error when i am running my suite from build.xml and suite.xml working exaclty fine

    ReplyDelete
  39. Unknown file:23:146: Fatal Error! Could not find function: if--- getting this error when i am running my suite from build.xml and suite.xml working exaclty fine

    ReplyDelete
    Replies
    1. hi neha,
      if you found the solution then can you please post it here?

      Delete
  40. nice tutorial, to generate pdf report use pdfngreport its free,open source plugin library http://uttesh.github.io/pdfngreport/

    ReplyDelete
  41. Did anyone found the solution for Fatal Error

    [xslt] Unknown file:22:146: Fatal Error! Could not find function: if
    [xslt] : Fatal Error! Fatal error during transformation Cause: Fatal error during transformation


    I am able to run test successfully and testng reports are also generated. but when I am generating xslt report. I am getting fatal error.

    ReplyDelete
  42. This comment has been removed by the author.

    ReplyDelete
  43. Hi Prashanth,

    I am able to run my application through testng.xml successfully but not able to run using ant build. It is showing below mentioned text :
    [testng] Total tests run: 1, Failures: 0, Skips: 1
    [testng] Configuration Failures: 1, Skips: 1
    [testng] ===============================================
    [testng] The tests failed.
    BUILD SUCCESSFUL
    Total time: 6 seconds
    Actually it is showing skipped status. Please suggest

    ReplyDelete
  44. I am able to run target(init,compile) successfully independently but not able to run Run target.

    ReplyDelete
  45. Hi Prashanth,

    Thanks for this post. I would like to know how I will generate a XSLT report in testng without ANT or Maven?

    Thanks and Regards,
    Kuladip
    kdipsahu@gmail.com

    ReplyDelete
  46. Hi prashanth,i created build and it runs successfully and the folder which i wanted to store the xslt reports is also created but the report of xslt is not there in that folder..can u help me where it will be and how i can see it???

    ReplyDelete
  47. I have successfully implemented using the steps mentioned here. However i need help to make some changes. I would like to add logo of our client top left corner of the generated report.

    ReplyDelete
  48. I have successfully implemented using the steps mentioned here. However i need help to make some changes. I would like to add logo of our client top left corner of the generated report.

    ReplyDelete
  49. shashikant sabnekar22 December 2015 at 21:50

    I have successfully implemented using the steps mentioned here. However i need help to make some changes. I would like to add logo of our client top left corner of the generated report.

    ReplyDelete
  50. Interactive Property Solution iBrouchure with numerous exceptional capacity to make your undertaking intriguing. It tells the story and interactive.

    ReplyDelete
  51. Sometimes xslt reports are not generating proper results like even if any failures are there its showing as pass, did anyone faced this issue.

    ReplyDelete
  52. Thanks for the great information in your blog Selenium Training in Chennai

    ReplyDelete
  53. Hi its great blog and nice. I have one question - I have developed a keyword driven framework. I have one @test method and all my logic sits there for excel. How do I utilize XLST report because it shows only one test in report

    ReplyDelete