How to get screenshot of full webpage using Selenium and Java?


Whenever we encounter a failure during testing, it is a common nature to capture the screenshots wherever there is a deviation from the expected result. Thus it is considered a mandatory step to attach a screenshot for creating a bug.

While automating a bunch of test cases of a considerable number, capturing screenshot is critical to infer why a test case has failed for both the development and testing team. As they debug the failures, going through the screenshot and conclude if the failure is due to script issue or defect in the application.

Sometimes we may need to capture the screenshots of the full page and not only the viewable part of the screen. The latest versions of the common browsers mostly capture the area which is visible.

To solve this problem, we have to use AShot() method. It is a method provided by webdriver to get a full screen image and available from versions 3.x of Selenium. It comes with below usages −

  • Full screen image capture.

  • Enhance screenshot.

  • Comparison between screenshots.

We have to download and add the following jar to our project before using AShot() −

https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot

Syntax −

Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("<< file path>>"));

Example

Code Implementation.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.io.File;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import javax.imageio.ImageIO;
public class ScreenshotFull{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // capture screenshot and store the image
      Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).take       Screenshot(driver);
      ImageIO.write(s.getImage(),"PNG",new File("tutorialspoint.png"));
      driver.quit();
   }
}

Output

Updated on: 28-Aug-2020

573 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements