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 is from the expected result. In Selenium, webdriver screenshot is mainly used for bug analysis. In this tutorial, we are going to learn how to capture screenshots in Selenium and the importance of screenshots in Selenium web driver.

How to take screenshots in Selenium webdriver using Java?
To capture screenshots in Selenium, Selenium has provided a TakeScreenShot interface .and in this interface, there is a method called as getScreenshotAs which will take screenshots in the form of files.

getScreenshotAs()- In the Selenium web driver to capture screenshots 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 screenshots as following are the types 
  • Screenshot of full page 
  • Screenshot of a particular element 
  • Screenshot of viewable part of the page.

How to take a screenshot of a full page in Selenium web driver using java?
Selenium does not provide the inherent capability to take screenshots of full pages. But using a third-party library named Ashot we can capture screenshots of the full page 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