【问题标题】:Unable to view screenshots in Jenkins reports无法在 Jenkins 报告中查看屏幕截图
【发布时间】:2019-01-14 22:39:34
【问题描述】:

我的 jenkins 报告未显示运行中捕获的屏幕截图。 (它们在 MAC 上运行)。但是,当我在本地(Windows 机器)运行相同的作业时,报告有屏幕截图。

我在 Jenkins 报告中看到如下错误。

当我尝试打开图像时,它显示如下。它实际上无法找到图像的路径。

Error : http://*****-mac:8080/job/***Test/job/******/ws/target/screenshots/2019-01-14/Sign%20In20190114154253.png

Actual: http://*****-mac:8080/job/***Test/job/******/HTML_20Report/target/screenshots/2019-01-14/Sign%20In20190114154253.png

我正在使用下面的代码来捕获屏幕截图。它确实捕获了屏幕截图,我可以在实际位置看到它们。 我不知道我什么时候做错了,这些图片在报告中没有被视为链接/不可用:

public void captureScreenshot() throws IOException {

    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String dateForScreenshots = dateFormat.format(date);
    File localScreenshots = new File(new File("target/screenshots"), dateForScreenshots);
    if (!localScreenshots.exists() || !localScreenshots.isDirectory()) {
        localScreenshots.mkdirs();
    }
    System.setProperty("org.uncommons.reportng.escape-output", "false");// this is to create a link in ReportNG
    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    String destination = System.getProperty("user.dir") + "/target/screenshots/" + dateForScreenshots + "/" + getDriver().getTitle() + Utils.generateRandomString() + ".png";
    File screenshotName = new File(destination);
    //Now add screenshot to results by copying the file
    FileUtils.copyFile(scrFile, screenshotName);
    Reporter.log("<br>  <img src='" + destination + "' height='90' width='160' /><br>");
    Reporter.log("<a href=" + destination + "></a>");
}

【问题讨论】:

    标签: jenkins selenium-webdriver screenshot


    【解决方案1】:

    从此行

    Error : http://*****-mac:8080/job/***Test/job/******/ws/target/screenshots/2019-01-14/Sign%20In20190114154253.png
    

    我认为 Jenkins 在工作区文件夹 (ws) 中找不到目标分支。可能您需要在 Jenkins 作业中指定工作区文件夹(如this 答案),或者只是将destination 变量以某种方式更改为类似(添加/HTML_20Report):

        String destination = System.getProperty("user.dir") + "/HTML_20Report/target/screenshots/" + dateForScreenshots + "/" + getDriver().getTitle() + Utils.generateRandomString() + ".png";
    

    【讨论】:

    • 谢谢你.. 我更改了报告日志中使用的最终 img src 位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多