0
votes

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. 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. enter image description here

1
May be because it will not able to find snapshot image from your source file. - Dhru 'soni
But the srcFile has the TakeScreenshot method.. - Dinu
yes it will create on src file when it will failed, but it will not copied to your destination file as it will not able to get data from src file. put a break point and debug does it contains any data in your destination file. - Dhru 'soni
the source file is not getting created. It is throwing null pointer at this line.scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); - Dinu
how can we remove the null pointer? - Dinu

1 Answers

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