We testers often do this task frequently, capturing evidence for failing test cases or conditions as screenshots or video. Accomplishing this task manually is a quite hectic process. Here comes automation handy. I am going to write about the process how we can capture screenshots in Selenium using Java. We will try avoiding hard coding and reduce redundancy so that it goes well with POM(Page object model).
First things first, let's create a project called XYZ. It's better if it's a Maven project. Inside of it, we create a package (suppose I name it) com.screenshot and inside of the package, we create a class ScreenShot.java. Inside, we are adding this following code.
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
This will take the screenshot for us. But if we want to save it somewhere in our project or to view the captured file, we are going to need a utility called commons.io. We can get this file from this link. For Maven project, we just need to edit the pom.xml file and this following code in the dependencies section.
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
This should be enough. To save the screenshot that we captured, we just need to add the following code right after we added the earlier. Before that, let's assume we created a directory/folder called screenshot in our class path, so that we can save our captured screenshots there. So in total the code looks like this-
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File(".//screenshot//"your_file_name".png"));
If it's done correctly, we shall have a screenshot with .png extension in our screenshot folder. We can keep it using any format we want by modifying the format.
It's done!! But wait, still some left. In most cases, we have more than one failing cases in our project and we need to keep multiple screenshots. But since we have defined the name statically, every time the capture will be replaced by the new one. To work on this, we can name the file dynamically. As an example, we can save the files with dates or time. One way of doing this,
Date dt = new Date();
String st = dt.toString().replace(" ", "-").replace(":", "-");
The Date class will have the time and to convert the date object to string, we can use the toString() method. We can also change the format using the replace() method according to our choice. Lastly, we are going to add the string as the name of the file. It looks like this-
FileUtils.copyFile(ss, new File(".//screenshot//" + st + ".png"));
After this, we shall have a screenshot file in our folder.
Sometimes we may need to change the directory of our screenshots, so it's better to handle the whole process in a separate package/class/method so that we can change it efficiently. It may look like this-
public class ScreenShot {
public static File screenShot(WebDriver driver) throws IOException {
Date dt = new Date();
String st = dt.toString().replace(" ", "-").replace(":", "-");
File ss = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ss, new File(".//screenshot//" + st + ".png"));
return ss;
}
Since it's a static method, we can use this anywhere in our testcases just by using ScreenShot.screenShot(driver);
.
That's it!!
Thanks for reading this out. Pardon me for the mistakes I may have made as I am still a learner and exploring everyday. I would be glad if it helps anyone. :)
Top comments (0)