Junior QASeleniumFree question
How do you take a screenshot in Selenium?
60-second answer
I cast the driver to the TakesScreenshot interface and call getScreenshotAs, with OutputType FILE, BYTES or BASE64, then save the file with a meaningful name like the test name and a timestamp. Selenium 4 can also take a screenshot of a single element using element.getScreenshotAs. In a framework I don't call this in every test; I put it in a TestNG listener's onTestFailure method or an AfterMethod that checks the result, and attach it to the report like Extent or Allure.
Explanation
OutputType.FILE→ save to disk (useFiles.copyor Apache CommonsFileUtils).OutputType.BASE64→ embed directly in HTML reports.OutputType.BYTES→ attach to Allure.- Full-page screenshots: Firefox supports
getFullPageScreenshotAs; for Chrome use CDP or a library.
Example
On failure, a listener saves screenshots/checkoutTest_2026-11-06_10-15-02.png and embeds it into the Extent report.
Sample code
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Files.copy(src.toPath(), Paths.get("screenshots", testName + ".png"),
StandardCopyOption.REPLACE_EXISTING);
// Element screenshot (Selenium 4)
File logo = driver.findElement(By.id("logo")).getScreenshotAs(OutputType.FILE);
// In a listener
@Override
public void onTestFailure(ITestResult result) {
String b64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
ExtentManager.getTest().fail("Failed").addScreenCaptureFromBase64String(b64);
}