【问题标题】:Automating Jenkins pages with selenium使用 selenium 自动化 Jenkins 页面
【发布时间】:2016-04-26 10:14:18
【问题描述】:

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

附: - 使用的框架 - Java with Selenium

【问题讨论】:

  • 正如您所说的xpath,我假设您已经看过Jenkins REST API?你试过什么了? xpath 有什么问题?
  • driver.findElement(By.xpath(xpathExpression)) 不适用于 Jenkins HTML 报告页面的任何 xpath。我使用了以下 ---> URL url = new URL("jenkins.companyname.com/job/"+jobName + "/" + build + "/api/xml");

标签: java selenium xpath jenkins jenkins-api


【解决方案1】:

由于您提供的信息有点模糊,请在下面找到两个有效的 sn-ps。

获取整个文档

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/

仅将 sn-ps 视为 PoC,以证明 Jenkins XML API 上的 XPath 正常工作。

【讨论】:

  • 这不是有意的。我需要一些可以帮助我找到 host:port/job/JOB_NAME/1234 指定的构建通过百分比的东西
  • @KshipraDwivedi 我不确定您在寻找什么。如果您正在寻找最新版本的测试结果。 XML API 的 URL 是 http://host:port/job/JOB_NAME//lastCompletedBuild/testReport/api/xml。相应地修改来自 sn-p 的 URL 和 xpath。
猜你喜欢
  • 2019-05-13
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 2017-09-25
  • 1970-01-01
  • 2021-05-08
  • 2020-11-24
  • 1970-01-01
相关资源
最近更新 更多