📸 Taking Unique Screenshots in selenium with java

When designing an automation testing framework in Selenium, capturing screenshots for test cases is a common and essential practice—especially when debugging test failures. One challenge often faced is handling duplicate screenshots that can overwrite each other. This issue can be effectively solved by generating unique screenshots based on the current date and time.

By formatting timestamps using DateTimeFormatter and LocalDateTime, we can create distinct filenames for each screenshot. This ensures every test run generates a unique screenshot file, making debugging easier and more traceable.

In the example below, I’ve used Selenium’s TakesScreenshot interface along with Java’s date-time API to capture and save screenshots with unique, time-stamped filenames. Here i am providing the script to take screenshot below:

âś…Example Script to handle duplicate screenshots:

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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.io.FileUtils;

public class ScreenshotWithTimestamp {

public static void main(String[] args) {
    // Set path to your ChromeDriver
    System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

    WebDriver driver = new ChromeDriver();

    try {
        // Open the desired website
        driver.get("https://example.com");

        // Create screenshot with date-time
        takeScreenshotWithTimestamp(driver);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        driver.quit();
    }
}

public static void takeScreenshotWithTimestamp(WebDriver driver) throws IOException {
    // Format current date and time
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
    String timestamp = LocalDateTime.now().format(formatter);

    // Create destination file path
    String fileName = "screenshot_" + timestamp + ".png";
    String filePath = "screenshots/" + fileName;

    // Create the screenshots directory if it doesn't exist
    Files.createDirectories(Paths.get("screenshots"));

    // Take screenshot and save to file
    File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    File destFile = new File(filePath);
    FileUtils.copyFile(srcFile, destFile);

    System.out.println("Screenshot saved: " + destFile.getAbsolutePath());
}

}

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , CodesToolbox
Scroll to Top
0
Would love your thoughts, please comment.x
()
x