首页 > 解决方案 > 无法将屏幕截图保存在指定路径或将屏幕截图附加到 MAC 中的 extentreports

问题描述

我尝试了很多方法来截取测试用例失败的屏幕截图,但没有任何效果。使用 selenium 时无法截取屏幕截图并将其附加到 MAC os 中的 extentreport。

public void onTestFailure(ITestResult tr)
{
    logger=extent.createTest(tr.getName()); // create new entry in the report
    logger.log(Status.FAIL,MarkupHelper.createLabel(tr.getName(),ExtentColor.RED)); // send the passed information to the report with GREEN color highlighted
    String screenshotPath="./Stest-output/"+tr.getName()+".png";
    TakesScreenshot ts = (TakesScreenshot)driver;
    File img =ts.getScreenshotAs(OutputType.FILE);
    File destination =new File(screenshotPath);
    try {
        FileUtils.copyFile(img,destination);
        logger.addScreenCaptureFromPath(screenshotPath);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if(img != null)
    {
        System.out.println("Screenshot is below:"+tr.getName());
        try {
            logger.info("Screenshot is below:" + logger.addScreenCaptureFromPath(screenshotPath));
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

尝试将图像从源复制到目标时抛出空指针异常。

使堆栈溢出中的所有方法都可用。

标签: macosseleniumscreenshotextentreports

解决方案


您无法将屏幕截图附加到范围报告 html 中,因为您忘记调用flush()将报告 HTML 文件附加到所有测试结果/屏幕截图的方法。必须至少有一个结束的测试才能附加到报告中。

注意:如果flush()在任何结束的测试之前调用,则不会将任何信息附加到报告中。

@AfterTest
public void tearDown() {
extent.flush();

推荐阅读