-1

I want to build an automated system to find the flakiness in my test scripts, for which I need to get Pass percentage of, say n builds, of a given job. Finding the data through Xpaths doesn't work. Is there any API which can fetch me the same, or any particular way to deal with the Xpaths.

P.S. - Framework used - Java with Selenium

KR29
  • 393
  • 1
  • 4
  • 19
  • As you are speaking about `xpath` I assume you had already a look at the `Jenkins REST API`? What have you tried so far? What's your problem with `xpath`? – SubOptimal Apr 26 '16 at 10:30
  • driver.findElement(By.xpath(xpathExpression)) doesn't work for any xpath of Jenkins HTML report page. I have used the following ---> URL url = new URL("http://jenkins.companyname.com/job/"+ jobName + "/" + build + "/api/xml"); – Kshipra Dwivedi Apr 27 '16 at 10:36

1 Answers1

0

As your provided information a bit vague find below two working snippets.

get the whole document

URI uri = new URI("http://host:port/job/JOB_NAME/api/xml");
HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();

DocumentBuilder builder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();
Document document = builder.parse(con.getInputStream());

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile("//lastSuccessfulBuild/url")
    .evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
    System.out.println("last successful: " + nodeList.item(i).getTextContent());
}
con.disconnect();

get only the interesting part using the Jenkins XPath API

URI uri = new URI("http://host:port/job/JOB_NAME/api/xml"
    + "?xpath=//lastSuccessfulBuild/url");

HttpURLConnection con = (HttpURLConnection) uri.toURL().openConnection();

DocumentBuilder builder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();
Document document = builder.parse(con.getInputStream());

XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.compile("/url")
    .evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
    System.out.println("last successful: " + nodeList.item(i).getTextContent());
}
con.disconnect();

example output for both

last successful: http://host:port/job/JOB_NAME/1234/

See the snippets only as a PoC to demonstrate that XPath on the Jenkins XML API is working in general.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • This is not intended. I need something which can help me find the passed percentage of build specified by http://host:port/job/JOB_NAME/1234/ – Kshipra Dwivedi Apr 29 '16 at 12:52
  • @KshipraDwivedi I'm not sure about what you are looking for.If you are looking for the test result of the latest build. The URL to the XML API is `http://host:port/job/JOB_NAME//lastCompletedBuild/testReport/api/xml`. Amend the URL and the xpath from the snippet accordingly. – SubOptimal Apr 29 '16 at 13:40