diff --git a/java/src/org/openqa/selenium/OutputType.java b/java/src/org/openqa/selenium/OutputType.java index 1d0e013e41266..3a3997cfd6cb1 100644 --- a/java/src/org/openqa/selenium/OutputType.java +++ b/java/src/org/openqa/selenium/OutputType.java @@ -83,14 +83,29 @@ public File convertFromPngBytes(byte[] data) { } private File save(byte[] data) { + Path tmpFilePath = createScreenshotFile(); try { - Path tmpFilePath = Files.createTempFile("screenshot", ".png"); - File tmpFile = tmpFilePath.toFile(); - tmpFile.deleteOnExit(); Files.write(tmpFilePath, data); - return tmpFile; } catch (IOException e) { - throw new WebDriverException(e); + throw new WebDriverException( + "Failed to create or write screenshot to temporary file: " + + tmpFilePath.toAbsolutePath().toString(), + e); + } + + File tmpFile = tmpFilePath.toFile(); + tmpFile.deleteOnExit(); + return tmpFile; + } + + private Path createScreenshotFile() { + try { + return Files.createTempFile("screenshot", ".png"); + } catch (IOException e) { + throw new WebDriverException( + "Failed to create or write screenshot to temporary file: " + + "temporary file could not be created", + e); } }