From 8683699e1f8f000cee251e0d9a9f669c537d3a48 Mon Sep 17 00:00:00 2001 From: Dmytro_Stekanov Date: Sat, 25 Apr 2020 08:53:49 +0300 Subject: [PATCH 1/2] reuse Selenide screenshots --- .../allure/selenide/AllureSelenide.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java b/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java index 53aceea47..e5713e158 100644 --- a/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java +++ b/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java @@ -17,6 +17,7 @@ import com.codeborne.selenide.Selenide; import com.codeborne.selenide.WebDriverRunner; +import com.codeborne.selenide.impl.ScreenShotLaboratory; import com.codeborne.selenide.logevents.LogEvent; import com.codeborne.selenide.logevents.LogEventListener; import com.codeborne.selenide.logevents.SelenideLog; @@ -31,6 +32,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -91,10 +95,24 @@ public AllureSelenide disableLogs(final LogType logType) { } private static Optional getScreenshotBytes() { + return ScreenShotLaboratory.getInstance().getLastThreadScreenshot() + .map(AllureSelenide::convertScreenshotFileToByteArray) + .orElseGet(AllureSelenide::takeNewScreenshot); + } + + private static Optional convertScreenshotFileToByteArray(File file) { + try { + return Optional.of(Files.readAllBytes(file.toPath())); + } catch (IOException e) { + return Optional.empty(); + } + } + + private static Optional takeNewScreenshot() { try { return WebDriverRunner.hasWebDriverStarted() - ? Optional.of(((TakesScreenshot) WebDriverRunner.getWebDriver()).getScreenshotAs(OutputType.BYTES)) - : Optional.empty(); + ? Optional.of(((TakesScreenshot) WebDriverRunner.getWebDriver()).getScreenshotAs(OutputType.BYTES)) + : Optional.empty(); } catch (WebDriverException e) { LOGGER.warn("Could not get screen shot", e); return Optional.empty(); From 341f5aa7ba4404087be220425e6c6f4d75699014 Mon Sep 17 00:00:00 2001 From: Dmytro_Stekanov Date: Sat, 25 Apr 2020 23:14:59 +0300 Subject: [PATCH 2/2] fix code style issue --- .../src/main/java/io/qameta/allure/selenide/AllureSelenide.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java b/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java index e5713e158..1fe8b8af4 100644 --- a/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java +++ b/allure-selenide/src/main/java/io/qameta/allure/selenide/AllureSelenide.java @@ -100,7 +100,7 @@ private static Optional getScreenshotBytes() { .orElseGet(AllureSelenide::takeNewScreenshot); } - private static Optional convertScreenshotFileToByteArray(File file) { + private static Optional convertScreenshotFileToByteArray(final File file) { try { return Optional.of(Files.readAllBytes(file.toPath())); } catch (IOException e) {