Search⌘ K
AI Features

ScreenshotWriter Class

Explore how to implement the ScreenshotWriter class to capture and save screenshots during Selenium tests in Java. Understand how it generates unique file names using test details and timestamps, enabling organized storage of screenshots within an automation framework.

We'll cover the following...

The ScreenshotWriter class is similar to the LogWriter one.

The code

Java
package framework;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
public class ScreenshotWriter {
private String folderPath = "./target/screenshots/";
public ScreenshotWriter()
{
}
public void writeToFile(byte[] screenshotInfo, String testName)
{
File screenshotsFolder = new File(folderPath);
if (!screenshotsFolder.exists())
screenshotsFolder.mkdir();
try {
FileOutputStream file = new FileOutputStream(
screenshotName(testName));
file.write(screenshotInfo);
file.close();
}
catch (Exception ex) {
throw new IllegalStateException("cannot create screenshot!", ex);
}
}
private String screenshotName(String testName)
{
String now = LocalDateTime.now().toString();
now = now.replace(":", "_")
.replace(";", "_")
.replace(".", "_");
return folderPath + testName + now + ".png";
}
}

What does the code do?

...