Capture screenshot in Selenium for failing conditions using Java
Tags: java, selenium, automation
Published: 2022-07-17 01:25:38

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](https://commons.apache.org/). 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. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bscoegxjvbatr1suq5v2.png)

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. :)