Taking **screenshots** during test execution is a useful feature in Selenium. Screenshots help capture the state of the application at specific points in the test, such as when an error occurs or a test fails. Selenium provides the **`TakesScreenshot`** interface, which is used to capture and save screenshots as images.
Selenium uses the **`TakesScreenshot`** interface to capture screenshots. The WebDriver class implements this interface, which provides the method **`getScreenshotAs()`** for taking a screenshot. The screenshot can be saved in various formats such as PNG, JPEG, or BASE64 encoding.
Here is an example of how to take a screenshot and save it as a file:
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 java.io.IOException;
import org.apache.commons.io.FileUtils;
public class ScreenshotExample {
public static void main(String[] args) {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
// Navigate to a webpage
driver.get("https://shariqsp.com");
// Take a screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save the screenshot to a file
File destinationFile = new File("./screenshots/homepage.png");
FileUtils.copyFile(sourceFile, destinationFile);
System.out.println("Screenshot saved successfully!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Screenshots can be captured for a variety of scenarios during test execution. Here are some common use cases:
It’s common practice to capture screenshots when a test fails. This helps identify the state of the application when the failure occurred. You can integrate this into your test framework using a **`TestNG Listener`** or **`JUnit Rule`**.
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class ScreenshotOnFailureListener implements ITestListener {
WebDriver driver;
@Override
public void onTestFailure(ITestResult result) {
// Capture screenshot on test failure
TakesScreenshot screenshot = (TakesScreenshot) driver;
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
try {
File destinationFile = new File("./screenshots/failure.png");
FileUtils.copyFile(sourceFile, destinationFile);
System.out.println("Screenshot captured for failed test: " + result.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
// Other listener methods can be implemented as needed
}
You can capture screenshots at specific points during test execution, such as after filling a form or before submitting a transaction. This can help in debugging issues by recording the application state at various stages of the workflow.
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 java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FormScreenshotExample {
public static void main(String[] args) {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
try {
// Navigate to a webpage and fill a form (example)
driver.get("https://shariqsp.com/form");
// Assuming form filling steps here...
// Take a screenshot after form is filled
TakesScreenshot screenshot = (TakesScreenshot) driver;
File sourceFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save the screenshot
File destinationFile = new File("./screenshots/form_filled.png");
FileUtils.copyFile(sourceFile, destinationFile);
System.out.println("Screenshot saved after filling the form!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Capturing screenshots during test execution has several benefits, including:
The TakesScreenshot
interface in Selenium allows capturing screenshots of the entire webpage. However, to capture a screenshot of a specific web element, such as a button, you can use the getScreenshotAs
method directly on the WebElement
instance.
findElement
.
getScreenshotAs
method on the WebElement
to capture the screenshot.
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class ScreenshotExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
// Locate the web element (button in this case)
WebElement button = driver.findElement(By.id("buttonId"));
// Take a screenshot of the button
File srcFile = button.getScreenshotAs(OutputType.FILE);
try {
// Save the screenshot to a specified location
FileUtils.copyFile(srcFile, new File("button_screenshot.png"));
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.quit();
}
}
}
In this code, we initialize the ChromeDriver, navigate to a webpage, locate the button using its ID, and directly capture the screenshot of the button without cropping. Finally, we save the screenshot as "button_screenshot.png".