Screenshotting is particularly useful when it comes to automating web applications. It helps you to analyze the bugs that you find while testing the applications. Taking screenshots might help you in capturing the testing evidence of your test cases, which will be a tedious job while running the test cases.
In this blog, you will find the ways of capturing the screenshot using selenium.
Why Capture Screenshots?
Capturing screenshots during software testing is important for a few reasons:
- Documentation:
Screenshots provide visual evidence of problems discovered during testing. They help improve communication between testers and developers by presenting solid evidence of defects and problems. - Debugging:
Screenshots can be useful while debugging an application. These can help testers in pinpointing the exact time an error occurred and provide ideas as to what might be the reason for the issue. - Verification:
Screenshots can be used to confirm that a test was properly run. They can provide as evidence that a test was performed and help in verifying that the desired outcomes were accomplished.
Methods for Capturing Screenshot using Selenium
There are having different methods that can be used to capture screenshot using selenium, that includes:
1. Capture Screenshots using TakeScreenshot Method
The TakeScreenshot method is part of the WebDriver interface in Selenium and is used to capture screenshots of the current page. It allows you to capture the entire page or a specific element on the page. The captured screenshot can be saved in various image formats such as PNG and JPG.
Following are the steps that can use to capture screenshot using the TakeScreenshot method:
I. Create an instance of the WebDriver interface:
For using any browser, you have to create an instance of webdriver.
WebDriver driver = new ChromeDriver();
II. Navigate to the website:
You have to enter the URL of the webpage where you want to capture the screenshot.
driver.get("https://testingmint.com");
III. Cast WebDriver instance to the TakesScreenshot interface:
As the WebDriver interface does not directly provide methods for capturing screenshots. However, the TakesScreenshot interface extends the WebDriver interface and provides additional methods for capturing screenshots. For that, you have to cast WebDriver instance to the TakesScreenshot interface.
TakesScreenshot Scr= (TakesScreenshot) driver;
IV. Call the TakeScreenshot method to capture a screenshot:
For capturing the screenshot and storing the screenshot as a file you have to use the following command.
File srcFile = Scr.getScreenshotAs(OutputType.FILE);
V. Save the captured screenshot to a specified directory:
For storing the screenshot in a particular directory, you have to use copyFile method of FileUtils. This will use to copy the file from source to destination.
File SaveDir = new File("D:\\screenshots\\screenshot.png");
FileUtils.copyFile(srcFile, SaveDir);
Practical Demonstration of Capture Screenshot using TakeScreenshot Method
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class TakeScreenshotExample{
public static void main(String[] args) throws Exception {
// Create an instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
// Navigate to testingmint.com
driver.get("https://www.testingmint.com/");
Thread.sleep(3000);
// Cast the driver instance to TakesScreenshot
TakesScreenshot Scr= (TakesScreenshot) driver;
// Capture the screenshot and save it to a file
File srcFile = Scr.getScreenshotAs(OutputType.FILE);
File SaveDir = new File("D:\\screenshots\\screenshot.png");
FileUtils.copyFile(srcFile, SaveDir);
// Quit the browser
driver.quit();
}
}
Output:
2. Capture Screenshot using Ashot Method
Ashot is a Java-based library that provides a simple and powerful API for taking screenshots in Selenium WebDriver tests. It offers a wide range of features and customization options, allowing you to capture screenshots of full web pages, individual elements, or custom dimensions.
Following are the steps that can use to capture screenshot using the Ashot method:
I. Create an instance of the Ashot class:
For using different methods of Ashot Class you have to create an instance.
AShot ashot = new AShot();
II. Capture a screenshot of the web page using the takeScreenshot method:
For capturing the screenshot, you have to use takeScreenshot method of Ashot().
Screenshot screenshot = ashot.takeScreenshot(driver);
III. Save the screenshot to a file:
For storing screenshot in different image formats you have to use the following command.
ImageIO.write(screenshot.getImage(), "PNG", new File("D:\\screenshots\\screenshot.png"));
The above method takes three arguments, here is the explanation of that.
- The first argument is the BufferedImage instance of the screenshot you want to save. You can access this instance by calling the getImage method on the Screenshot object returned by the ashot.takeScreenshot().
- The second argument is used to specify the image format you want to save. In this case, we are specifying “PNG“.
- The third argument is a File object representing the file you want to save the image to. In this case, we are storing the screenshot in the specified directory with the name “screenshot.png“.
Practical Demonstration of Capture Screenshot using Ashot Method
import java.io.File;
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;
public class AshotExample {
public static void main(String[] args) throws Exception {
// Create a new instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Navigate to the testingmint.com homepage
driver.get("https://www.testingmint.com/");
Thread.sleep(3000);
// Create a new instance of the Ashot class
AShot ashot = new AShot();
// Capture a screenshot of the web page
Screenshot screenshot = ashot.takeScreenshot(driver);
File SaveDir = new File("D:\\screenshots\\screenshot.png");
// Save the screenshot to a PNG file
ImageIO.write(screenshot.getImage(), "PNG", SaveDir);
// Close the browser
driver.quit();
}
}
Output:
Types of Screenshots
There will be having different types of screenshots that can be taken in manual and automation testing.
1. Current Window Screenshot
A current window screenshot is a type of screenshot that captures the content of the active or currently selected window on the computer screen. In the above examples, you have seen, how to Capture screenshot of current window using TakeScreenshot() and Ashot().
2. Full-Page Screenshot
A Full-page screenshot, is a screenshot that captures the entire visible content of a web page, including the parts that are not currently visible on the screen without scrolling.
This type of screenshot is useful for capturing the complete layout and content of a web page, especially if the page is long or contains important information beyond the initial viewable area.
Practical Demonstration of Capture Full-Page Screenshot
import java.io.File;
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 Exception {
// Create a new instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Navigate to the testingmint.com homepage
driver.get("https://www.testingmint.com/");
Thread.sleep(3000);
// Create a new instance of the Ashot class
AShot ashot = new AShot();
// Set the shooting strategy to capture the entire page
ashot.shootingStrategy(ShootingStrategies.viewportPasting(ShootingStrategies.scaling(1.125f), 1000));
// Capture a screenshot of the web page
Screenshot screenshot = ashot.takeScreenshot(driver);
File SaveDir = new File("D:\\screenshots\\screenshot.png");
// Save the screenshot to a PNG file
ImageIO.write(screenshot.getImage(), "PNG", SaveDir);
// Close the browser
driver.quit();
}
}
Output:
3. Element Screenshot
An element screenshot refers to a specific type of screenshot that captures a screenshot of a specific element on a web page, rather than the entire page. This type of screenshot can be useful when you want to focus on a particular element, such as a button, an image, or a block of text, and exclude everything else on the page.
Practical Demonstration of Capture Element Screenshot
import java.io.File;
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;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ElementScreenshotExample {
public static void main(String[] args) throws Exception {
// Create a new instance of the ChromeDriver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Navigate to the testingmint.com homepage
driver.get("https://www.testingmint.com/");
Thread.sleep(3000);
//Locate the element
WebElement element = driver.findElement(By.xpath("//div[@class='inside-site-info grid-container']"));
// Create a new instance of the Ashot class
AShot ashot = new AShot();
// Provide Shooting stratergy and coordinates
ashot.coordsProvider(new WebDriverCoordsProvider());
ashot.shootingStrategy(ShootingStrategies.viewportPasting(100));
// Capture a screenshot of the web page
Screenshot screenshot = ashot.takeScreenshot(driver, element);
File SaveDir = new File("D:\\screenshots\\screenshot.png");
// Save the screenshot to a PNG file
ImageIO.write(screenshot.getImage(), "PNG", SaveDir);
// Close the browser
driver.quit();
}
}
Output:
Conclusion
In this blog, we have explored why to automate screenshot capturing flow, what are the different methods to capture screeshot using selenium and what are the different types of screenshots.