Selenium 4: Full page screenshots

22-10-2019 door Roy de Kleijn

In some cases it can be useful to take a screenshot of the entire page, rather than only what is visible in the viewport. Selenium 4 provides this feature for the FireFoxDriver.

The code below demonstrates how to do that.

@Test
public void takeScreenshotTest() {
   driver.get("https://www.amazon.com");

   this.takeScreenshot(driver, new File("target/screenshot/screenshot.png"));
}

private void takeScreenshot(WebDriver driver, File destination) {
   File file = null;
   if (driver instanceof FirefoxDriver) {
      file = ((FirefoxDriver) driver).getFullPageScreenshotAs(OutputType.FILE);
   } else if (driver instanceof ChromeDriver) {
      file = ((ChromeDriver) driver).getScreenshotAs(OutputType.FILE);
   }

   try {
      FileUtils.copyFile(file, destination);
   } catch (IOException e) {
      e.printStackTrace();
   }
}

As you can see, you need to determine first which driver you are using.

Delen: