-1

我想构建一个自动化系统来查找我的测试脚本中的脆弱性,为此我需要获得给定工作的通过百分比,比如 n 次构建。通过查找数据Xpaths不起作用。是否有任何 API 可以让我获得相同的信息,或者任何特定的方式来处理Xpaths.

PS - 使用的框架 -Java with Selenium

4

1 回答 1

0

由于您提供的信息有点模糊,请在以下两个工作片段中找到。

获取整个文档

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();

使用 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();

两者的示例输出

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

仅将这些片段视为 PoC,以证明 Jenkins XML API 上的 XPath 正常工作。

于 2016-04-28T07:45:22.787 回答