Skip to content

Commit 7100361

Browse files
committed
appended timestamp in the filename to get unique screenshot name every time
1 parent a21c941 commit 7100361

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/test/java/io/github/mfaisalkhatri/utils/ScreenshotListener.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.IOException;
55
import java.nio.file.Files;
66
import java.nio.file.Paths;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
79

810
import org.openqa.selenium.OutputType;
911
import org.openqa.selenium.TakesScreenshot;
@@ -16,14 +18,17 @@ public class ScreenshotListener implements ITestListener {
1618
public void onTestFailure (final ITestResult result) {
1719
Object testClass = result.getInstance ();
1820
WebDriver driver = ((WebDriverProvider) testClass).getDriver ();
21+
String timestamp = new SimpleDateFormat ("yyyyMMdd_HHmmss_SSS").format (new Date ());
22+
1923

2024
if (driver != null) {
2125
String testName = result.getMethod ()
2226
.getMethodName ();
2327
File screenshot = ((TakesScreenshot) driver).getScreenshotAs (OutputType.FILE);
28+
String filename = timestamp + ".png";
2429
try {
2530
Files.createDirectories (Paths.get ("screenshots"));
26-
Files.copy (screenshot.toPath (), Paths.get ("screenshots", testName+ ".png"));
31+
Files.copy (screenshot.toPath (), Paths.get (testName, filename));
2732
} catch (IOException e) {
2833
throw new RuntimeException (e);
2934
}

0 commit comments

Comments
 (0)