How to take screenshots?
You need Apache Commons IO for FileUtils.copyFile().
Add this to your Maven pom.xml if you use Maven:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
1. Take a Screenshot of the Entire Page
Sample Program:
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import org.apache.commons.io.FileUtils; // Add Apache Commons IO dependency
public class ScreenshotExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
driver.get("https://example.com");
// Take screenshot and store as a file format
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now copy the screenshot to desired location
FileUtils.copyFile(src, new File("./screenshot.png"));
System.out.println("Screenshot captured!");
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
2. Take a Screenshot of a Specific Element
import org.openqa.selenium.WebElement;
// Assume 'element' is already located:
WebElement element = driver.findElement(By.id("myElement"));
File src = element.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("./element-screenshot.png"));
3. Take a Screenshot in case of failure using TestNG
Using TestNG’s ITestListener:
Implement ITestListener to automatically capture screenshots on test failure:
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.openqa.selenium.WebDriver;
public class ScreenshotListener implements ITestListener {
@Override
public void onTestFailure(ITestResult result) {
Object testClass = result.getInstance();
WebDriver driver = ((YourBaseTestClass) testClass).getDriver(); // get your driver instance
String methodName = result.getName();
takeScreenshot(driver, methodName + "_FAILED");
}
//... other overridden methods
}
4. Manual Screenshot Capture on Catch Block
If you’re not using any test framework hooks, you can take screenshots in your catch blocks:
try {
// test steps
} catch (Exception e) {
takeScreenshot(driver, "TestStep_Failed");
throw e;
}
Best Practices
- Include timestamps in the screenshot file name for uniqueness.
- Store screenshots in a structured folder, e.g., ./screenshots/.
- Attach screenshots to your test reports (many reporting tools support this, e.g., Allure, ExtentReports).
Sample Method with Timestamp
import java.text.SimpleDateFormat;
import java.util.Date;
public static void takeScreenshot(WebDriver driver, String fileName) {
try {
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("./screenshots/" + fileName + "_" + timestamp + ".png"));
} catch (Exception e) {
e.printStackTrace();
}
}