【问题标题】:screen shot is not displayed in extent report屏幕截图未显示在范围报告中
【发布时间】:2021-08-22 11:21:32
【问题描述】:

截取屏幕截图并存储在文件夹中。但它没有显示失败的测试。 显示为损坏的图像。

Java 代码:

    public synchronized void onTestFailure(ITestResult result) {
                System.out.println((result.getMethod().getMethodName() + " failed!"));
                test.get().fail(MarkupHelper.createLabel(result.getName()+ " - Test failed due to below issue/error: ", ExtentColor.RED));
                test.get().fail(result.getThrowable());
                
                //Take screenshot and allow automatic saving of media files relative to the report
                //Extentreports log and screenshot operations for failed tests.
                try {
                    File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    //          String base64Screenshot = "data:image/png;base64,"+((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
                    String path=prop.getProperty("Screenshot_Folder")+System.currentTimeMillis()+".png";
                    File destination=new File(path);
                    FileUtils.copyFile(src, destination);
                    
                    test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());
                    //test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build());
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Screen-capture has been taken but not attached to Extent report");
                }
    
            }

下面是属性文件。

AutomationReport_Folder = D://Shared//V1core_automation
ExtentReport_Folder = D://Shared//V1core_automation//ExtentReports//
Screenshot_Folder = D://Shared//V1core_automation//ExtentReports//Screenshots//

截屏方法

public static String getScreenshot(WebDriver驱动) {

        TakesScreenshot ts=(TakesScreenshot) driver;
        File src=ts.getScreenshotAs(OutputType.FILE);

        String path=System.getProperty("user.dir")+"/Screenshots/"+System.currentTimeMillis()+".png";

        File destination=new File(path);

        try 
        {
            FileUtils.copyFile(src, destination);
        } catch (IOException e) 
        {
            System.out.println("Capture Failed "+e.getMessage());
        }

        return path;

    }

【问题讨论】:

  • 报告和截图是否存放在同一个目录下?
  • 是的,我在同一个目录中
  • 如何定义您的“Screenshot_Folder”属性?
  • 我已经创建了属性文件,我在其中定义了屏幕截图和报告的文件夹路径。我附上了同样的问题以供参考。

标签: java selenium selenium-webdriver extentreports


【解决方案1】:

替换代码中的这一行:

test.get().fail("Below is Screen Shot of Error Page/Pop-up: ", MediaEntityBuilder.createScreenCaptureFromPath(path).build());

MediaEntityBuilder.addScreenCaptureFromPath(path, result.getMethod().getMethodName());

看看效果如何。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多