简体   繁体   English

在带有范围报告的selenium POM中截取屏幕时出现空指针异常

[英]Null pointer exception while taking screenshot in selenium POM with extent report

A null pointer exception is thrown While trying to take screenshot when the scenario fails. 抛出空指针异常尝试在方案失败时截取屏幕截图。 I have an actions class in which i have defiled the capture screenshot method. 我有一个动作类,我已经玷污了捕获截图方法。

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

Extent reports are implemented using Itestlisterner interface. 范围报告使用Itestlisterner接口实现。 Below given code for which implements the screenshot method given: 下面给出的代码实现了给出的截图方法:

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

And am getting the below error: Please help in resolving the same. 我得到以下错误:请帮助解决相同的问题。 在此输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM