Skip to content
CodeKruPro

Take Viewport Screenshot

62/70

Take Viewport Screenshot

Selenium can capture screenshots using the TakesScreenshot interface. getScreenshotAs(OutputType.FILE) saves the image as a PNG file. In Chrome it captures the visible viewport, the part of the page that fits in the window, not the whole scrolling page.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Take a screenshot of the viewport as a File
  3. Print whether the file exists, after the label Screenshot taken:
  4. Print whether its size in bytes is greater than 0, after the label File has content:

Expected output:

Screenshot taken: true
File has content: true

About this lesson

Take a screenshot in Selenium Java with TakesScreenshot and OutputType.FILE

Learn Selenium JavaAdvancedScreenshots

How it works

Drivers that can capture the page implement TakesScreenshot. Cast the driver and call getScreenshotAs() with an OutputType: OutputType.FILE gives a PNG file, OutputType.BYTES a byte array and OutputType.BASE64 a string, useful for embedding in HTML reports.

In Chrome the capture is the visible viewport, not the whole page. On the Buttons page in this runner the image is 780 by 437 pixels while the document is 1016 pixels tall. ChromeDriver has no full-page method; Firefox has one through HasFullPageScreenshot, and in Chrome you would resize the window or use DevTools to capture more.

The file is temporary. Selenium writes it to the system temp folder and marks it to be deleted when the JVM exits, so copy it somewhere permanent, for example with Files.copy(), if you want to keep it.

About the assignment

On the Buttons page you capture the browser as a file and check two facts about it: that the file exists and that it is not empty, which is how a test confirms a capture worked before attaching it to a report.

What you will do

  1. Cast the driver to TakesScreenshot and call getScreenshotAs() with OutputType.FILE.
  2. Print the file's exists() result after the label Screenshot taken:.
  3. Print whether its length() is greater than zero after the label File has content:.

Common mistakes

  • Calling getScreenshotAs() on the driver variable

    The variable is typed as WebDriver, which has no such method, so the code does not compile. Cast to TakesScreenshot first.

  • Expecting the whole page

    Content below the viewport is not in the image. Scroll, resize, or capture the element you care about.

  • Relying on the temp file later

    It is deleted when the program ends. Copy it inside the test run.

In a real test suite

Most suites take a screenshot automatically when a test fails, in a JUnit extension or a TestNG listener, and attach it to the report next to the error. Base64 output is popular there because it embeds straight into an HTML report.

Lesson 61 changed the DOM. Lesson 63 captures a single element instead of the whole viewport.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        // TODO: Take screenshot as FILE
        // TODO: Check if file exists and has content
        // TODO: Print results
        
        driver.quit();
    }
}