Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Last active January 1, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h4ck4life/791b9e506d16b2e80e2f52271401c8b9 to your computer and use it in GitHub Desktop.
Save h4ck4life/791b9e506d16b2e80e2f52271401c8b9 to your computer and use it in GitHub Desktop.
Read TRX result file and output with test summary into plain csv
import java.io.FileInputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Steps to build executable jar:
* - javac TrxSummary.java
* - jar cfe TrxSummary.jar TrxSummary *.class
* - java -jar TrxSummary.jar results-example-mstest.trx
*/
public class TrxSummary {
public static void main(String[] args) {
if (args.length > 0) {
try {
Path absolutePath = Paths.get(args[0]);
FileInputStream fileInputStream = new FileInputStream(absolutePath.toAbsolutePath().toString());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fileInputStream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("Counters");
if (nodeList.getLength() > 0) {
Node nNode = nodeList.item(0);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
StringBuilder resultOuput = new StringBuilder();
resultOuput.append("Total:" + eElement.getAttribute("total"));
resultOuput.append(",");
resultOuput.append("Executed:" + eElement.getAttribute("executed"));
resultOuput.append(",");
resultOuput.append("Passed:" + eElement.getAttribute("passed"));
resultOuput.append(",");
resultOuput.append("Error:" + eElement.getAttribute("error"));
resultOuput.append(",");
resultOuput.append("Failed:" + eElement.getAttribute("failed"));
resultOuput.append(",");
resultOuput.append("Timeout:" + eElement.getAttribute("timeout"));
System.out.println(resultOuput);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("Please input TRX file path");
System.exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment