Skip to content

Instantly share code, notes, and snippets.

@adiralashiva8
Last active February 12, 2019 18:42
Show Gist options
  • Save adiralashiva8/99e0d6bcaf38542b3e80fc151021aaf5 to your computer and use it in GitHub Desktop.
Save adiralashiva8/99e0d6bcaf38542b3e80fc151021aaf5 to your computer and use it in GitHub Desktop.
Parse TestNG-result.xml to get method and test metrics
import org.w3c.dom.* ;
import org.xml.sax.SAXException;
import javax.xml.parsers.* ;
import java.io.* ;
public class TestngMetrics {
public static void main(String[] args) {
String path = System.getProperty("user.dir") + "/testng-result.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"));
// Test Metrics
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;
try {
if (!eElement.getAttribute("is-config").contains("true")) {
// Get parent element to capture suite name
Element suiteElement = (Element) eElement.getParentNode();
System.out.println("Class Name: " + suiteElement.getAttribute("name"));
// Get test case name
System.out.println("Test 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: ");
}
} else {
continue;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
// Method Metrics
NodeList mMethods = document.getElementsByTagName("test-method");
for (int temp = 0; temp < mMethods.getLength(); temp++) {
Node node = mMethods.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) node;
try {
if (eElement.getAttribute("is-config").contains("true")) {
// Get parent element to capture suite name
Element suiteElement = (Element) eElement.getParentNode();
System.out.println("Class Name: " + suiteElement.getAttribute("name"));
// Get test case name
System.out.println("Method 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: ");
}
} else {
continue;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment