Selenium – Creating XML Reports



http://selftechy.com/2011/07/07/selenium-creating-xml-reports


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,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.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念