
👉How to Take Screenshots in Selenium WebDriver
Introduction
How to Take Screenshots in Selenium WebDriver: Taking screenshots in Selenium WebDriver is a crucial step in automated testing and bug reporting. Screenshots help debug test failures, capture test evidence, and verify UI elements.
In this guide, you will learn:
✅ How to take screenshots using Selenium WebDriver
✅ How to capture full-page screenshots with Ashot API
✅ How to save and store screenshots in a desired location
✅ How to integrate screenshots into your test reports
👉How to Take a Screenshot in Selenium WebDriver?
Step 1: Convert WebDriver Object to TakeScreenshot
How to Take Screenshots in Selenium WebDriver: Selenium provides the TakeScreenshot
interface, which allows you to capture screenshots.
javaTakesScreenshot screenshot = ((TakesScreenshot) driver);
Step 2: Capture and Save the Screenshot
Use the getScreenshotAs()
method to capture the screenshot and store it as a file.
javaFile srcFile = screenshot.getScreenshotAs(OutputType.FILE);
Step 3: Copy the Screenshot to a Desired Location
Use the Apache Commons IO library to move the screenshot to a specific folder.
javaFile destFile = new File("C:\\Screenshots\\testScreenshot.png");
FileUtils.copyFile(srcFile, destFile);
👉Full Example: Selenium WebDriver Screenshot Code
javaimport java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenshotExample {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Open Website
driver.get("https://www.example.com");
// Capture Screenshot
TakesScreenshot screenshot = ((TakesScreenshot) driver);
File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
// Save Screenshot to a specific location
File destFile = new File("C:\\Screenshots\\testScreenshot.png");
FileUtils.copyFile(srcFile, destFile);
System.out.println("Screenshot saved successfully!");
// Close the browser
driver.quit();
}
}
👉What is AShot API?
How to Take Screenshots in Selenium WebDriver: AShot is a third-party Selenium utility that allows you to:
✅ Take full-page screenshots (beyond viewport)
✅ Capture specific WebElements
✅ Handle zooming, scaling, and blurring
How to Install AShot API?
You can install AShot in two ways:
1️⃣ Using Maven (Recommended)
Add the following dependency to your pom.xml
file:
xml<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>
2️⃣ Manually Download the AShot JAR
- Download from Maven Repository
- Add it to your project’s Build Path
👉How to Capture a Full-Page Screenshot Using AShot API?
How to Take Screenshots in Selenium WebDriver: By default, TakeScreenshot
only captures the visible portion of the page. To take a full-page screenshot, use AShot API.
Example: Take Full-Page Screenshot with AShot
javaimport java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class FullPageScreenshot {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Open the webpage
driver.get("https://www.example.com");
// Capture full-page screenshot
Screenshot screenshot = new AShot()
.shootingStrategy(ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
// Save the screenshot
ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\Screenshots\\fullPageScreenshot.png"));
System.out.println("Full-page screenshot saved successfully!");
// Close the browser
driver.quit();
}
}
👉How to Capture a WebElement Screenshot in Selenium?
How to Take Screenshots in Selenium WebDriver: AShot also allows you to capture a screenshot of a specific WebElement.
Example: Take Screenshot of a WebElement
javaimport java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
public class ElementScreenshot {
public static void main(String[] args) throws IOException {
// Set up WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
// Open the webpage
driver.get("https://www.example.com");
// Locate the WebElement
WebElement element = driver.findElement(By.id("logo"));
// Capture WebElement Screenshot
Screenshot screenshot = new AShot().takeScreenshot(driver, element);
// Save the screenshot
ImageIO.write(screenshot.getImage(), "PNG", new File("C:\\Screenshots\\elementScreenshot.png"));
System.out.println("Element screenshot saved successfully!");
// Close the browser
driver.quit();
}
}
👉Comparison: TakeScreenshot vs. AShot API
Feature | TakeScreenshot (Selenium) | AShot API (Third-Party) |
---|---|---|
Captures full-page screenshot | ❌ No | ✅ Yes |
Captures WebElement screenshot | ❌ No | ✅ Yes |
Supports zooming & scaling | ❌ No | ✅ Yes |
Requires external dependency | ❌ No | ✅ Yes |
Works on all browsers | ✅ Yes | ✅ Yes |
👉Common Issues & Solutions in Selenium Screenshots
Issue | Solution |
---|---|
Screenshot not saving | Ensure the file path is correct. Use C:\\Screenshots\\ for Windows. |
FileNotFoundException | Add Apache Commons IO JAR for FileUtils.copyFile() . |
Full-page screenshot not working | Use AShot API instead of TakeScreenshot . |
Element screenshot cropped | Ensure the WebElement is fully visible before capturing. |
👉Conclusion
How to Take Screenshots in Selenium WebDriver: Taking screenshots in Selenium WebDriver is essential for debugging test failures and creating test reports.
👉Key Takeaways:
✔ How to Take Screenshots in Selenium WebDriver: Use TakeScreenshot
for basic screenshots
✔ Use AShot API for full-page and element-specific screenshots
✔ Store screenshots in a structured directory for better test reporting
✔ Integrate screenshots into TestNG Reports for better analysis
Start capturing Selenium screenshots today for better test debugging!