【问题标题】:Null pointer exception while taking screenshot in selenium POM with extent report使用范围报告在 selenium POM 中截屏时出现空指针异常
【发布时间】:2019-04-24 08:30:48
【问题描述】:

场景失败时尝试截屏时抛出空指针异常。我有一个动作类,我在其中污染了捕获屏幕截图方法。

public static String capture(WebDriver driver) throws NullPointerException, IOException {
    File scrFile;
    scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    File Dest = new File("D:\\Dinu\\SeleniumReports\\Test" + System.currentTimeMillis() + ".jpeg");
    String filepath = Dest.getAbsolutePath();
    org.openqa.selenium.io.FileHandler.copy(scrFile, Dest);
    return filepath;
}

范围报告是使用 Itestlisterner 接口实现的。下面给出的代码实现了给定的屏幕截图方法:

public synchronized void onTestFailure(ITestResult result) {
    System.out.println((result.getMethod().getMethodName() + " failed!"));
    test.get().fail(result.getThrowable());
    try {
        String screenshotPath = actions.capture(driver);
        test.get().addScreenCaptureFromPath(screenshotPath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我收到以下错误:请帮助解决同样的问题。

【问题讨论】:

  • 可能是因为它无法从您的源文件中找到快照图像。
  • 但是 srcFile 有 TakeScreenshot 方法..
  • 是的,它会在失败时在 src 文件上创建,但它不会复制到您的目标文件,因为它无法从 src 文件中获取数据。放置一个断点并调试它是否包含目标文件中的任何数据。
  • 源文件没有被创建。它在这一行抛出空指针。scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  • 如何去掉空指针?

标签: selenium selenium-webdriver extentreports selenium-extent-report


【解决方案1】:
public static String getScreenhot(WebDriver driver, String screenshotName) throws Exception {
 String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
 TakesScreenshot ts = (TakesScreenshot) driver;
 File source = ts.getScreenshotAs(OutputType.FILE);
                //after execution, you could see a folder "FailedTestsScreenshots" under src folder
 String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/"+screenshotName+dateName+".png";
 File finalDestination = new File(destination);
 FileUtils.copyFile(source, finalDestination);
 return destination;
 }


@AfterMethod
     public void getResult(ITestResult result) throws IOException{
     if(result.getStatus() == ITestResult.FAILURE){
     logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
     logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
     //To capture screenshot path and store the path of the screenshot in the string "screenshotPath"
                            //We do pass the path captured by this mehtod in to the extent reports using "logger.addScreenCapture" method. 
                            String screenshotPath = ExtentReportsClass.getScreenshot(driver, result.getName());
     //To add it in the extent report 
     logger.log(LogStatus.FAIL, logger.addScreenCapture(screenshotPath));
     }else if(result.getStatus() == ITestResult.SKIP){
     logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
     }
     // ending test
     //endTest(logger) : It ends the current test and prepares to create HTML report
     extent.endTest(logger);
     }

【讨论】:

  • 这也给出了空指针异常。我认为空指针被抛出,因为我正在对 webdriver 驱动程序进行新的引用。但不知道如何解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-21
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多