首页 > 解决方案 > 我无法将屏幕截图附加到范围报告。解决办法是什么?

问题描述

@AfterMethod
    public void getResult(ITestResult result) throws Exception{
     if(result.getStatus() == ITestResult.FAILURE)
    {
       extentlogger.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable() + 
       " - Test Case Failed", ExtentColor.RED));
   
       try {
       // get path of captured screenshot using custom failedTCTakeScreenshot method
       String screenshotPath = takeSnapShot(result);
       extentlogger.fail("Test Case Failed Snapshot is below " + 
     extentlogger.addScreenCaptureFromPath(screenshotPath));
      } catch (InterruptedException e) {
       e.printStackTrace();
       }
     }
    }

上面的代码是我在TestNG中使用after方法的代码。我做错什么了吗?在此处输入图像描述

标签: seleniumextentreportsselenium-extent-report

解决方案


您没有任何名为 takeSnapShot 或未声明为公共的方法:

在基类中创建一个具有以下内容的方法,或者通过替换 takeSnapShot 步骤直接在 aftermethod 挂钩中添加以下代码:

    //Convert web driver object to TakeScreenshot

    TakesScreenshot scrShot =((TakesScreenshot)webdriver);

    //Call getScreenshotAs method to create image file

            File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

        //Move image file to new destination

            File DestFile=new File(fileWithPath);

            //Copy file at destination

            FileUtils.copyFile(SrcFile, DestFile);

推荐阅读