Capture Screenshot in selenium:

 

  • A screenshot, or screen capture, is a picture taken from your computer’s desktop.
  • It may include desktop backgrounds, icons of files and folders, and open windows.
  • It is mandatory to take the screenshot for verification so that can prove also that your test case has covered certain functionality or not.
  • When we execute huge number of test scripts, and if some test fails, we need to check why the test has failed.
  • Selenium can automatically take screenshots during execution.
  • You need to type cast WebDriver instance to TakesScreenshot.

 

When / Why Need to Capture ScreenShot in Selenium WebDriver:

Application issue:

  • Sometimes Applications not run properly due to Network Issue, Internet Speed, Cache Issue that Selenium script chances to Fail so, we capture screenshot.

 

Assertion Failure:

  • Assertion’s used to Verify case Pass or Fail.
  • In Both Pass or Fail cases we capture screenshot.

 

Timeout to find WebElements on the web page:

  • Sometimes Selenium script throw exception Due to not able to find WebElement that time we capture screenshot to know where bug is found.

 

To Verify Bug is Valid or Not?

  • Sometimes bug is not valid Or need to compare existing project UI / Functionality with Expected one.

 

Capture the screenshot:

  • Using TakesScreenshot and getScreenshotAs capture ScreenShot and Store into File.

 

Store the screenshot:

  • Using the help of FileUtils.copyFile we store ScreenShot into Local Device path.

 

Scenario’s to Capture ScreenShot:

  • setProperty of Browser path to Launch Browser.
  • Create driver Instance / Object of WebDriver.
  • Wait until Full page load.
  • Open URL.
  • Maximize Browser Screen.
  • TakeScreenShot using getScreenshotAs in-Built option.
  • Store captured ScreenShot internal path.

 

Example: Capture Screenshot:

How to Take Screenshot in Selenium WebDriver and Take a screenshot with Selenium WebDriver and selenium take screenshot on failure