You must Sign In to post a response.
  • Category: JavaScript

    Screenshot capture in Java Selenium

    While attempting to take a screenshot with Selenium, I keep getting this error? Since fileUtils isn't working for me, I'm using the FileHandler function. Do I need to make any changes?

    My Code :
    ts.getScreenshotAs(OutputType.FILE); ///---> line 54 File src=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); File n = new File ("D:\\Failed_Test_Cases\\accessdeniedlogin.png"); FileHandler.copy(src, n); My Result :
    java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.TakesScreenshot.getScreenshotAs(org.openqa.selenium.OutputType)" because "this.ts" is null at StepDefinitions.loginStep.user_clicks_login_button( at ✽.User clicks login button(file:///D:/coach-outlet/src/test/resources/Feature/login.feature:8)
  • #26657
    It looks like the error is occurring on line 54 of your code. Based on the error message, it seems that the "ts" object is null, which is causing the "getScreenshotAs" method to throw a null pointer exception.

    One possible cause for this issue is that the "ts" object is not being properly initialized. In order to take a screenshot using Selenium, you need to first cast the driver object to a TakesScreenshot object. Here's an example of how you might initialize the "ts" object correctly:

    TakesScreenshot ts = (TakesScreenshot)driver;

    Another possible reason for the error is that the driver object is not properly instantiated. Make sure that you have instantiated the driver object and that you are able to navigate to the desired webpage before trying to take a screenshot.

    It is also good to check whether the directory "D:\Failed_Test_Cases" exist or not before trying to save the screenshot.

    Try these steps and let us know if that helps. If you're still running into issues, please provide more information about the environment you're working in (e.g. version of Selenium, version of the browser, etc.) and we'll try to help you out further.


    I find that the harder I work, the more luck I seem to have.

  • Sign In to post your comments