Skip to content

Selenium 4: Full page screenshots

22-10-2019 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:
Roy de Kleijn

Test Automation Expert

Met meer dan 15 jaar ervaring in software testing en test automation helpt Roy teams om hun testprocessen te verbeteren.

Wil je meer leren?

Bekijk onze trainingen en breng je test automation skills naar het volgende niveau.