Testing / Selenium Interview questions
How do you take a screenshot in Selenium WebDriver?
WebDriver instances that implement the TakesScreenshot interface can capture the current browser view as an image file.
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(src, new File("screenshot.png"));
This captures only the visible viewport by default, not the full scrollable page - full-page screenshots require either a browser-specific workaround (like Chrome DevTools Protocol commands) or a third-party library, since WebDriver's standard API has no built-in full-page capture the way some newer frameworks do.
It's common to wrap this call in a TestNG or JUnit listener that automatically captures a screenshot whenever a test fails, attaching it to the report without needing to add the call manually inside every test method.
More Related questions...