Last active
February 13, 2019 11:59
-
-
Save adiralashiva8/29ba02bbb8b7984d8adeb37fd0837d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.w3c.dom.* ; | |
import org.xml.sax.SAXException; | |
import javax.xml.parsers.* ; | |
import java.io.* ; | |
public class TestNGMetricsReport { | |
public static void main(String[] args) { | |
String path = System.getProperty("user.dir") + "/result6.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("==================== Test Case Stats ===================="); | |
System.out.println(""); | |
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")); | |
System.out.println(""); | |
System.out.println("==================== Class Metrics ===================="); | |
System.out.println(""); | |
// Class Metrics | |
NodeList cClass = document.getElementsByTagName("class"); | |
for (int temp = 0; temp < cClass.getLength(); temp++) { | |
int cTotalSteps = 0; | |
int cPassSteps = 0; | |
int cFailSteps = 0; | |
int cSkipSetps = 0; | |
Node node = cClass.item(temp); | |
if (node.getNodeType() == Node.ELEMENT_NODE) { | |
Element eElement = (Element) node; | |
// Get class name | |
System.out.println("Class Name: " + eElement.getAttribute("name")); | |
NodeList cChildNodes = eElement.getElementsByTagName("test-method"); | |
for (int ctemp = 0; ctemp < cChildNodes.getLength(); ctemp++) { | |
Node cnode = cChildNodes.item(ctemp); | |
if (cnode.getNodeType() == Node.ELEMENT_NODE) { | |
Element cElement = (Element) cnode; | |
cTotalSteps ++; | |
if (cElement.getAttribute("status").equalsIgnoreCase("PASS")) { | |
cPassSteps ++ ; | |
} else if (cElement.getAttribute("status").equalsIgnoreCase("FAIL")) { | |
cFailSteps ++ ; | |
} else { | |
cSkipSetps ++ ; | |
} | |
} | |
} | |
System.out.println("Total Steps: " + cTotalSteps); | |
System.out.println("Pass Steps: " + cPassSteps); | |
System.out.println("Fail Steps: " + cFailSteps); | |
System.out.println("Skip Steps: " + cSkipSetps); | |
System.out.println(""); | |
} | |
} | |
System.out.println("==================== Test Metrics ===================="); | |
System.out.println(""); | |
// 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; | |
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: " + exceptionNode.getAttribute("class")); | |
} else { | |
System.out.println("Error: "); | |
} | |
} else { | |
continue; | |
} | |
System.out.println(""); | |
} | |
} | |
System.out.println("==================== Method Metrics ===================="); | |
System.out.println(""); | |
// Test 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; | |
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: " + exceptionNode.getAttribute("class")); | |
} else { | |
System.out.println("Error: "); | |
} | |
} else { | |
continue; | |
} | |
System.out.println(""); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment