Saturday, August 10, 2019

XML to HTML report using xsl

XML to HTML report using xsl



Sample junit xml report

<testsuite errors="0" failures="2" hostname="Deepaks-MacBook-Air.local" name="xxx.scalaTests.common.DefaultColumnAdditionSpec" tests="4" time="7.66" timestamp="2019-08-10T09:58:56">

<testcase name="1To validate resultant df, when input datatype is of type string. 1To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="7.625"> </testcase>

<testcase name="3To validate resultant df, when input datatype is of type string. 3To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.003"> </testcase>

<testcase name="4To validate resultant df, when input datatype is of type string. 4To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.008">
<failure message="org.scalatest.exceptions.TestFailedException was thrown." type="class org.scalatest.exceptions.TestFailedException">...</failure>
</testcase>

<testcase name="5To validate resultant df, when input datatype is of type string. 5To validate resultant df, when input datatype is of type string." classname="xxx.scalaTests.common.DefaultColumnAdditionSpec" time="0.0">
<failure message="org.scalatest.exceptions.TestFailedException was thrown." type="class org.scalatest.exceptions.TestFailedException">...</failure>
</testcase>

</testsuite>

----save as test.xsl--------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>Result</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Test Name</th>
      <th style="text-align:left">Result</th>
    </tr>
    <xsl:for-each select="testsuites /testsuite/testcase">
    <tr>
      <td><xsl:value-of select="@name"/></td>
<xsl:choose>
<xsl:when test="failure"><td>FAIL</td></xsl:when>
<xsl:otherwise><td>PASS</td></xsl:otherwise>
</xsl:choose>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------------

STEPS:

  1. Save the above xsl code as test.xsl
  2. Open terminal
  3. cd to juit report file.
  4. Run below code 
    1. MAC : xsltproc -o result.html test.xsl junit_result.xml 
    2. Win :  msxsl sourcefile.xml test.xsl -o junit_result.html 









No comments:

Post a Comment