Capturing screenshots in Selenium WebDriver is crucial for debugging and reporting test results. Selenium provides multiple ways to take screenshots of web pages, elements, or even specific areas of the screen.
π οΈ Why Take Screenshots in Selenium?
Screenshots help in:
βοΈ Debugging failed test cases
βοΈ Generating visual test reports
βοΈ Tracking UI changes over time
βοΈ Capturing error messages for analysis
π― Different Ways to Take Screenshots in Selenium
β
1. Taking a Full Page Screenshot Using TakesScreenshot
Selenium provides the TakesScreenshot
interface, which allows capturing the entire visible area of a webpage.
πΉ Example: Capturing Full Web Page Screenshot
import org.openqa.selenium.*;
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) throws IOException {
// Setup WebDriver
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Capture screenshot
TakesScreenshot ts = (TakesScreenshot) driver;
File src = ts.getScreenshotAs(OutputType.FILE);
// Save the screenshot
FileUtils.copyFile(src, new File("screenshot.png"));
// Close browser
driver.quit();
}
}
πΉ Explanation:
- Convert
WebDriver
instance toTakesScreenshot
. - Capture the screenshot using
getScreenshotAs(OutputType.FILE)
. - Save the screenshot to a desired location.
β 2. Taking a Screenshot of a Specific WebElement
Instead of capturing the full page, you may need a screenshot of a particular element (e.g., a button, form, or image).
πΉ Example: Capturing a Specific WebElement
WebElement element = driver.findElement(By.id("logo"));
// Capture screenshot of the element
File src = element.getScreenshotAs(OutputType.FILE);
// Save the screenshot
FileUtils.copyFile(src, new File("element_screenshot.png"));
πΉ Key Benefits:
βοΈ Helps in verifying specific UI elements
βοΈ Useful for capturing error messages or pop-ups
β 3. Taking a Full-Page Screenshot (Beyond Visible Area)
Seleniumβs default TakesScreenshot
only captures the visible part of a webpage. For capturing the entire page (including scrollable areas), you can use third-party libraries like Ashot.
πΉ Example: Using Ashot for Full-Page Screenshot
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class FullPageScreenshot {
public static void main(String[] args) throws IOException {
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
Screenshot screenshot = new AShot()
.shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
ImageIO.write(screenshot.getImage(), "PNG", new File("fullpage_screenshot.png"));
driver.quit();
}
}
πΉ Why Use AShot?
βοΈ Captures entire page, including scrollable content
βοΈ Supports high-resolution images
βοΈ Can be used for image comparison
β 4. Capturing Screenshots on Test Failure (TestNG)
To capture screenshots automatically when a test case fails, use TestNGβs @AfterMethod
.
πΉ Example: Take Screenshot on Failure
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class ScreenshotOnFailure {
WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.get("https://example.com");
}
@Test
public void testFailureScreenshot() {
// Intentionally failing test
driver.findElement(By.id("wrongElement")).click();
}
@AfterMethod
public void takeScreenshotOnFailure(ITestResult result) throws IOException {
if (ITestResult.FAILURE == result.getStatus()) {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("failed_test_screenshot.png"));
}
driver.quit();
}
}
πΉ Key Benefits:
βοΈ Automatically captures a screenshot when a test fails
βοΈ Saves debugging time
βοΈ Useful for generating reports
π Best Practices for Taking Screenshots
βοΈ Always use try-catch
to handle exceptions
βοΈ Store screenshots with timestamps to avoid overwriting
βοΈ Use meaningful names for screenshots (e.g., Login_Failure.png
)
βοΈ Ensure WebDriver waits for elements before capturing
π Conclusion
Taking screenshots in Selenium WebDriver is an essential technique for debugging and reporting. Whether capturing the full page, specific elements, or handling failures, you can use built-in and third-party tools like Ashot for enhanced functionality. π Happy Testing! π―