首页 > 解决方案 > 屏幕截图未显示在范围报告中

问题描述

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

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//

截屏方法

公共静态字符串 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;

    }

标签: javaseleniumselenium-webdriverextentreports

解决方案


替换代码中的这一行:

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

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

并看到它的工作原理。


推荐阅读