代码之家  ›  专栏  ›  技术社区  ›  Steerpike

在ExtentReport中打印屏幕截图

  •  1
  • Steerpike  · 技术社区  · 7 年前

    我不明白为什么我的屏幕截图没有出现在我的本地扩展端口中(我还没有从构建服务器上尝试过)。下面是TestNG@AfterMethod,其中的takeScreenShot方法是电话:-

    @AfterMethod
    public void afterMethod(ITestResult result) throws IOException {
        APPLICATION_LOGS.debug("Running @AfterMethod");
        switch (result.getStatus()) {
            case ITestResult.FAILURE:
                childTest.fail(result.getThrowable())
                        .addScreenCaptureFromPath((takeScreenShot(result.getMethod().getMethodName())));
                try {
                    childTest.info("<a href='https://mywebsite.com/fabrix/logs/s/" + webSessionId + "'" + "target=_'blank'>Data control logs</a>");
                } catch (Exception e) {
                    childTest.info("Unable to get Session ID");
                }//.....more code
    

    这就是所谓的方法:

    private String takeScreenShot(String methodName) {
        String path = System.getProperty("user.dir") + "\\target\\reports\\screenshots\\" + methodName + ".png";
        try {
            File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotFile, new File(path));
        } catch (Exception e) {
            APPLICATION_LOGS.error("Could not write screenshot" + e);
        }
        return path;
    }
    

    报告说:

    我使用Chrome开发工具检查图像,路径是完美的,图像保存在与报告相同的目录中。那为什么不展示呢?

    来自IntelliJ:

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  2
  •   Bill Hileman    7 年前

    这是因为指定的屏幕截图路径需要相对于报表路径。我自己也发现了这一点。虽然截图确实是在您认为的位置拍摄和存储的,但由于它与报告的位置完全相同,所以只需离开指向文件名的路径。

    在我的输出中,我将报表文件放在项目文件夹之外的target\reports文件夹中,但是我有一个target\reports\screenshots文件夹,所以当我将屏幕截图附加到测试对象时,我只指定“screenshots”\文件名.png“当我尝试完整路径时,它不会起作用。