0

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

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();
    }
}

并收到以下错误:请帮助解决相同的问题。 在此处输入图像描述

4

1 回答 1

0
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);
     }
于 2019-04-24T09:20:44.517 回答