Taking screenshots during test execution is essential in automation
testing. Selenium provided the getScreenshotAs() method to capture
the current webpage and save it as an image file. This is useful for
debugging, reporting, and identifying issues in test cases.
In this post, we'll cover:
- What is getScreenshotAs() ?
- How to use it in Selenium WebDriver
- Syntax and different ways to implement it
- A complete example code
What is getScreenshotAs()?
The getScreenshotAs() method is part of the TakeScreenshot
interface in Selenium. It allows us to capture a screenshot of the current
webpage and store it as a file.
Why is it useful?
- Helps in debugging test failures
- Useful for generating reports with screenshots
- Captures visual proof of test execution
How to Use getScreenshotAs() in Selenium
To take a screenshot in Selenium, follow these steps:
- Cast the WebDriver instance to TakesScreenshot
- Use getScreenshotAs() method to capture the screenshot
- Save the captured screenshot to a specified location using FileHandler.copy()
Syntax:
Method 1: Using an explicit
TakesScreenshot reference
TakesScreenshot tshot = (TakesScreenshot)driver;
File src = tshot.getScreenshotAs(OutputType.FILE);
File dest = new File("destination path\\filename.jpg");
FileHandler.copy(src, dest);
Method 2: Using a one-liner approach
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File dest = new File("destination path\\filename.jpg");
FileHandler.copy(src, dest);
Both methods achieve the same result. The second method is more
compact.
Complete Example: Taking a Screenshot Using EdgeDriver
Below is a complete Java program that launches Microsoft Edge, navigates
to a sample website, captures a screenshot, and saves it to a specified
location.
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.TakeScreenshot;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.io.FileHandler;
public class TScreenshot {
public static void main(String[] args) throws IOException {
// Set path for Edge WebDriver
System.setProperty("webdriver.edge.driver",".//edgedriver.exe");
// Initialize Edge
WebDriver
WebDriver driver = new EdgeDriver();
// Open a Webpage
String url =
"https://testingcolleges.blogspot.com";
driver.get(url);
// Take a screenshot
TakesScreenshot screenshot =
(TakesScreenshot)driver;
File source =
screenshot.getScreenshotAs(OutputType.FILE);
// Define destination file path
File destination = new
File("E:\\123.jpg");
// Copy the screenshot to the
destination
FileHandler.copy(source,
destination);
// Close the browser
driver.quit();
System.out.println("Screenshot
saved successfully at: " +
destination.getAbsolutePath());
}
}
Explanation of the Code
1. Setting Up WebDriver
System.setProperty("webdriver.edge.driver", ".//edgedriver.exe");
WebDriver driver = new EdgeDriver();
- Sets the system property to specify the Edge WebDriver location.
- Creates a new instance of the EdgeDriver.
2. Opening a Webpage
String url = "https://testingcolleges.blogspot.com";
driver.get(url);
- Loads the given URL in the browser.
3. Taking the Screenshot
TakesScreenshot screenshot = (TakesScreenshot) driver;
File source = screenshot.getScreenshotAs(OutputType.FILE);
- Casts the driver object to TakesScreenshot.
- Captures a screenshot and stores it in a temporary file.
4. Saving the Screenshot
File destination = new File("E:\\123.jpg");
FileHandler.copy(source, destination);
- Define the location where the screenshot will be saved.
- Copies the captured screenshot to the specified file path.
5. Closing the Browser
driver.quit();
- Closes the browser session after execution.
6. Displaying Success Message
System.out.println("Screenshot saved successfully at: " +
desination.getAbsolutePath());
- Print a message with the exact file path of the saved screenshot.
Common Errors and Fixes
1. IllegalStateException: The path to the driver executable must be
set
Cause: The WebDriver path is incorrect or not set
properly.
Solution: Ensure the correct WebDriver executable path
is provided:
System.setProperty("webdriver.edge.driver",
"C:\\Path\\To\\edgedriver.exe");
2. IOException: The system cannot find the file specified
Cause: The destination folder does not
exist.
Solution: Ensure the directory exists before
saving the file.
3. UnhandledAlertException: Modal dialog present.
Cause: A popup or alert is blocking the
screenshot.
Solution: Handle alerts before taking a
screenshot:
Alert alert = driver.switchTo().alert();
alert.accept(); // or alert.dismiss();
Conclusion
Taking screenshots in Selenium is simple and highly useful for debugging
and reporting. Using getScreenshotAs(), you can easily capture
and store screenshots during test execution.
✅ Key Takeaways:
- getScreenshotAs() is used to capture screenshots in Selenium.
- You can use either explicit casting or a one-liner approach.
- Always handle errors like missing WebDriver or invalid file path.
Now, try implementing this in your Selenium tests and enhance your
automation reporting with screenshots! 🚀
No comments:
Post a Comment