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
- Cast the driver to
TakesScreenshotand callgetScreenshotAs()withOutputType.FILE. - Print the file's
exists()result after the labelScreenshot taken:. - Print whether its
length()is greater than zero after the labelFile 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 toTakesScreenshotfirst.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.
Related lessons
- Take Element ScreenshotCapture only one element.
- Window Size and PositionWindow size decides how much of the page fits in the image.
- Scroll to Element with JavaScriptScroll to the part of the page you want in the shot.
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.
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();
}
}