Italian Trulli

How To Capture Screenshot in Selenium Using Java

Test Tribe

                      Capturing Screenshot in selenium

While testing of application whenever we encounter any bug it is a common behaviour to take screenshots where the deviation from the expected result.In Selenium webdriver screenshot is mainly used for bug analysis. In this tutorial, we are going to learn how to take capture screenshots in selenium and the importance of screenshots in selenium webdriver.

  • How to take screenshot in selenium webdriver using java?
To capture screenshot in selenium , Selenium has provided a TakeScreenShot interface .and in this interface there is a method called as getScreenshotAs which will take screenshot in the form of file .

getScreenshotAs()- In selenium webdriver to capture screenshot we use getScreenshotAs method.

Selenium Code - 

WebDriver driver = new FirefoxDriver();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

In Selenium webdriver we can capture screenshot as
following are the types 
  • Screenshot of full page 
  • Screenshot of a particular element 
  • Screenshot of viewable part of page.

  • How to take screenshot of a full page in selenium webdriver using java?
Selenium does not provide the inherent capability to take screenshot of fullpage . But using a third-party library named Ashot we can capture screenshot of fullpage in selenium.
First download the jar file from -HERE
and then add the same as an external dependency .

Selenium Code-
 // capture screenshot and store the image
        Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
        ImageIO.write(s.getImage(),"PNG",new File("C:\\projectScreenshots\\fullPageScreenshot.png"));


Post a Comment