Selenium – Creating XML Reports

by seetaram on July 7, 2011

XML is the abbreviation for Extensible Markup Language.  XML is used in many aspects of software development, data storage, to communicate between different applications, data sharing, etc.  In Test Automation if the reports are generated in XML format that can be utilized to generate customized HTML reports and also can be imported into Spreadsheet like Microsoft Excel for better visibility.  This forces the Automation Testers to generate XML reports.  In this post I have given Java code which generates the XML report in the below format:

<testsuite>
    <testcase>
        <ID>Test_01</ID>
        <Status>Pass</Status>
        <Error>Null</Error>
    </testcase>
    <testcase>
        <ID>Test_02</ID>
        <Status>Pass</Status>
        <Error>Null</Error>
    </testcase>
</testsuite>

The above xml contains testsuite tag, under that testcase tag.  Each of the testcase tag will have different attributes such as ID, status, and error.  If we generate the output of an execution of Test Automation Suite in this format that will help us in analyzing the results.  Hence, we can utilize the same format in test automation with Selenium.

In this example, I have not used Selenium but this can be easily integrated in the test automation suite just by implementing the class and inheriting the class in the Automation Suite.

Let us have a look at the below code:

package com.selftechy.xmlhelpers;

/*
 * 
 * Author - Seetaram Hegde
 */
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;

public class XMLdoc {
    public String getXML()
      throws ParserConfigurationException,
             TransformerException,
             TransformerConfigurationException
             {
		        DocumentBuilderFactory factory
		          = DocumentBuilderFactory.newInstance();
		        DocumentBuilder builder = factory.newDocumentBuilder();
		        DOMImplementation DMi = builder.getDOMImplementation();
		
		        Document dc = DMi.createDocument(null,null,null);
		        Element ts = dc.createElement("testsuite");
		        dc.appendChild(ts);
		
		        Element tc = dc.createElement("testcase");
		        ts.appendChild(tc);
		        
		        Element id = dc.createElement("ID");
		        tc.appendChild(id);
		        id.appendChild(dc.createTextNode("Test_01"));
		
		        Element sts = dc.createElement("Status");
		        tc.appendChild(sts);
		        sts.appendChild(dc.createTextNode("Pass"));

		        Element err = dc.createElement("Error");
		        tc.appendChild(err);
		        err.appendChild(dc.createTextNode("Null"));

		        Element tc2 = dc.createElement("testcase");
		        ts.appendChild(tc2);
		        
		        Element id2 = dc.createElement("ID");
		        tc2.appendChild(id2);
		        id2.appendChild(dc.createTextNode("Test_02"));
		
		        Element sts2 = dc.createElement("Status");
		        tc2.appendChild(sts2);
		        sts2.appendChild(dc.createTextNode("Pass"));

		        Element err2 = dc.createElement("Error");
		        tc2.appendChild(err2);
		        err2.appendChild(dc.createTextNode("Null"));
		        
		        DOMSource dmSrc = new DOMSource(dc);
		        TransformerFactory tfc = TransformerFactory.newInstance();
		        Transformer transformer = tfc.newTransformer();

		        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
		        transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
		        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		        java.io.StringWriter swriter = new java.io.StringWriter();
		        StreamResult sresult = new StreamResult(swriter);
		        transformer.transform(dmSrc, sresult);
		        String xml = swriter.toString();
		        return xml;
    }

    public static void main(String args[])
			    throws ParserConfigurationException,
			    TransformerException,
			    TransformerConfigurationException
     {
        System.out.println(new XMLdoc().getXML());
     }
}

There are different XML parsers / processors available but out of them JAXP is the one which is provided by the JDK.  Here, I have utilized JAXP for XML report creation.  This library provides several classes and methods to accomplish the XML document creation.

Execute the above Java program and the output should look like the one which resembles the above format.

Create a Java class and integrate with Selenium Test Automation Suite.

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email