使用selenium自动化Jenkins页面

时间:2016-04-26 10:14:18

标签: java selenium xpath jenkins jenkins-api

我想构建一个自动系统来查找我的测试脚本中的碎片,为此我需要获得给定作业的Pass百分比,比如n个构建。通过Xpaths查找数据并不起作用。是否有任何API可以获取相同或任何特定方式来处理Xpaths

P.S。 - 使用的框架 - Java with Selenium

1 个答案:

答案 0 :(得分: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一般正在运行。