How to capture screenshot in selenium Webdriver using Java

                      Capturing Screenshot in selenium

While testing of application whenever we encounter any bug it is a common behaviour to take screenshot where the deviation form expected result .In Selenium webdriver screenshot mainly used for bug analysis .In this tutorial we are going to learn how to take capture screenshot in selenium and importance of screenshot 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();
driver.get("http://www.google.com/");
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 
  • Screen shot of particular element 
  • Screen shot of viewable part of page .

  • How to take screenshot of full page in selenium webdriver using java?
Selenium does not provide the inherent capability to take screenshot of fullpage . But using third-party library named Ashot we can capture screen shot 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

0 Comments