Skip to content

Instantly share code, notes, and snippets.

@adiralashiva8
Created February 9, 2019 03:53
Show Gist options
  • Save adiralashiva8/f9491d7d31859eaf24c28250ab7b632f to your computer and use it in GitHub Desktop.
Save adiralashiva8/f9491d7d31859eaf24c28250ab7b632f to your computer and use it in GitHub Desktop.
Code to parse testng-result.xml and print results
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.*;
public class Metrics{
public static void main(String[] args) {
String path = System.getProperty("user.dir")+"/test-output/testng-results.xml";
File testNgResultXmlFile = new File(path);
//Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
//Build Document
Document document = null;
try {
document = builder.parse(testNgResultXmlFile);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Normalize the XML Structure;
document.getDocumentElement().normalize();
System.out.println("Total : " + document.getDocumentElement().getAttribute("total"));
System.out.println("Passed : " + document.getDocumentElement().getAttribute("passed"));
System.out.println("Failed : " + document.getDocumentElement().getAttribute("failed"));
System.out.println("Skipped : " + document.getDocumentElement().getAttribute("skipped"));
NodeList tMethods = document.getElementsByTagName("test-method");
for (int temp = 0; temp < tMethods.getLength(); temp++){
Node node = tMethods.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) node;
// Get parent element to capture suite name
Element suiteElement = (Element) eElement.getParentNode();
System.out.println("Suite: " + suiteElement.getAttribute("name"));
// Get test case name
System.out.println("Name: " + eElement.getAttribute("name"));
// Get test case status
System.out.println("Status: " + eElement.getAttribute("status"));
// Get test case duration
System.out.println("Duration: " + eElement.getAttribute("duration-ms"));
// Get exception message if exist
Node eNode = eElement.getElementsByTagName("exception").item(0);
Element exceptionNode = (Element) eNode;
if (exceptionNode != null) {
System.out.println("Error Message: " + exceptionNode.getAttribute("class"));
} else {
System.out.println("Error Message: ");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment