bug6135: storing screenshot in test case class name's folder

This commit is contained in:
Axel Uhl
2025-07-05 17:09:34 +02:00
parent 1bf1108c1c
commit 8afbe2645d
@@ -8,6 +8,8 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
@@ -20,6 +22,8 @@ import org.openqa.selenium.remote.RemoteWebDriver;
import com.sap.sailing.selenium.test.AbstractSeleniumTest;
public class ScreenShotRule implements TestExecutionExceptionHandler {
private static final Logger logger = Logger.getLogger(ScreenShotRule.class.getName());
private static final String NOT_SUPPORTED_IMAGE = "/com/sap/sailing/selenium/resources/not-supported.png"; //$NON-NLS-1$
private static final String ATTACHMENT_FORMAT = "[[ATTACHMENT|%s]]"; //$NON-NLS-1$
/**
@@ -57,27 +61,30 @@ public class ScreenShotRule implements TestExecutionExceptionHandler {
final File screenshotFolder = environment.getScreenshotFolder();
if (screenshotFolder != null) {
environment.getWindowManager().forEachOpenedWindow(window -> {
final String filename = UUID.randomUUID().toString();
// FIXME we only use one driver for multiple windows. How to select the right window for screenshot?
WebDriver driver = window.getWebDriver();
if (RemoteWebDriver.class.equals(driver.getClass())) {
driver = new Augmenter().augment(driver);
}
InputStream source = getScreenshotNotSupportedImage();
if (driver instanceof TakesScreenshot) {
source = new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
}
try {
// FIXME how could this ever have written to the correct folder? This always writes to ScreenShotRule...
final File destinationDir = new File(screenshotFolder, getClass().getName());
destinationDir.mkdirs();
final File destination = new File(destinationDir, filename + SCREENSHOT_FILE_EXTENSION); //$NON-NLS-1$
final Path path = destination.toPath();
Files.copy(source, path, StandardCopyOption.REPLACE_EXISTING);
// ATTENTION: Do not remove this line because it is needed for the JUnit Attachment Plugin!
System.out.println(String.format(ATTACHMENT_FORMAT, destination.getCanonicalFile().toURI()));
} catch (IOException exception) {
throw new RuntimeException(exception);
window.switchToWindow();
final String filename = UUID.randomUUID().toString();
WebDriver driver = window.getWebDriver();
if (RemoteWebDriver.class.equals(driver.getClass())) {
driver = new Augmenter().augment(driver);
}
InputStream source = getScreenshotNotSupportedImage();
if (driver instanceof TakesScreenshot) {
source = new ByteArrayInputStream(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
}
try {
final File destinationDir = new File(screenshotFolder, context.getRequiredTestClass().getName());
destinationDir.mkdirs();
final File destination = new File(destinationDir, filename + SCREENSHOT_FILE_EXTENSION); //$NON-NLS-1$
final Path path = destination.toPath();
Files.copy(source, path, StandardCopyOption.REPLACE_EXISTING);
// ATTENTION: Do not remove this line because it is needed for the JUnit Attachment Plugin!
System.out.println(String.format(ATTACHMENT_FORMAT, destination.getCanonicalFile().toURI()));
} catch (IOException exception) {
throw new RuntimeException(exception);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Could not capture screenshot for window: " + window.getWindowHandle(), e);
}
});
}