From 7bd0cc0d84cb0e166753b3036338fb1e46ff6bf2 Mon Sep 17 00:00:00 2001 From: USSQA Date: Wed, 16 Sep 2026 15:10:50 -0600 Subject: [PATCH 1/2] use Selenide.screenshot to get screenshot bytes --- .../main/java/io/qameta/allure/selenide/AllureSelenide.java | 5 +---- 1 file changed, 1 insertion(+), 4 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 48b36fc87..ef45f1a78 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 @@ -27,7 +27,6 @@ import io.qameta.allure.model.StatusDetails; import io.qameta.allure.model.StepResult; import org.openqa.selenium.OutputType; -import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriverException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,9 +133,7 @@ public AllureSelenide disableLogs(final LogType logType) { private static Optional getScreenshotBytes() { try { - return WebDriverRunner.hasWebDriverStarted() - ? Optional.of(((TakesScreenshot) WebDriverRunner.getWebDriver()).getScreenshotAs(OutputType.BYTES)) - : Optional.empty(); + return Optional.ofNullable(Selenide.screenshot(OutputType.BYTES)); } catch (WebDriverException e) { LOGGER.warn("Could not get screen shot", e); return Optional.empty(); From cc72568e0725b61780ce66c0115e6e7e8ec48b11 Mon Sep 17 00:00:00 2001 From: USSQA Date: Thu, 17 Sep 2026 10:23:03 -0600 Subject: [PATCH 2/2] test(selenide): verify screenshots are taken via Selenide Photographer plugin - add test with a custom Photographer registered via SPI, asserting its bytes end up in the Allure attachment - add test that a driver without TakesScreenshot support does not break step reporting - add TestPhotographer fixture and META-INF/services registration --- .../allure/selenide/AllureSelenideTest.java | 63 +++++++++++++++++++ .../allure/selenide/TestPhotographer.java | 46 ++++++++++++++ .../com.codeborne.selenide.impl.Photographer | 1 + 3 files changed, 110 insertions(+) create mode 100644 allure-selenide/src/test/java/io/qameta/allure/selenide/TestPhotographer.java create mode 100644 allure-selenide/src/test/resources/META-INF/services/com.codeborne.selenide.impl.Photographer diff --git a/allure-selenide/src/test/java/io/qameta/allure/selenide/AllureSelenideTest.java b/allure-selenide/src/test/java/io/qameta/allure/selenide/AllureSelenideTest.java index d742f0644..69cfbf82c 100644 --- a/allure-selenide/src/test/java/io/qameta/allure/selenide/AllureSelenideTest.java +++ b/allure-selenide/src/test/java/io/qameta/allure/selenide/AllureSelenideTest.java @@ -57,6 +57,7 @@ class AllureSelenideTest { @AfterEach void closeBrowser() { WebDriverRunner.closeWebDriver(); + TestPhotographer.screenshot = null; } @AllureFeatures.Steps @@ -174,6 +175,41 @@ void shouldSaveScreenshotsOnFail() { .isEqualTo("hello"); } + @AllureFeatures.Attachments + @Test + void shouldTakeScreenshotsViaSelenidePhotographerPlugin() { + final WebDriver wdMock = mock(WebDriver.class); + WebDriverRunner.setWebDriver(wdMock); + TestPhotographer.screenshot = "photographer-screenshot".getBytes(StandardCharsets.UTF_8); + + final AllureResults results = runSelenideTestContext(() -> { + final AllureSelenide selenide = new AllureSelenide() + .savePageSource(false) + .screenshots(true); + SelenideLogger.addListener(UUID.randomUUID().toString(), selenide); + final SelenideLog log = SelenideLogger.beginStep( + "dummy source", + "dummyMethod()", + "param1", + "param2" + ); + SelenideLogger.commitStep(log, new Exception("something went wrong")); + }); + + final StepResult selenideStep = extractStepFromResults(results); + assertThat(selenideStep.getAttachments()) + .hasSize(1); + + final Attachment attachment = selenideStep.getAttachments().iterator().next(); + assertThat(results.getAttachments()) + .containsKey(attachment.getSource()); + + final String attachmentContent = results.getAttachmentContentAsString(attachment); + + assertThat(attachmentContent) + .isEqualTo("photographer-screenshot"); + } + @AllureFeatures.Attachments @Test void shouldSavePageSourceOnFail() { @@ -233,6 +269,33 @@ void shouldNotFailIfBrowserIsNotOpened() { assertThat(selenideStep.getAttachments()).hasSize(0); } + @AllureFeatures.Attachments + @Test + void shouldNotFailIfDriverDoesNotSupportScreenshots() { + final WebDriver wdMock = mock(WebDriver.class); + WebDriverRunner.setWebDriver(wdMock); + + final AllureResults results = runSelenideTestContext(() -> { + final AllureSelenide selenide = new AllureSelenide() + .savePageSource(false) + .screenshots(true); + SelenideLogger.addListener(UUID.randomUUID().toString(), selenide); + final SelenideLog log = SelenideLogger.beginStep( + "dummy source", + "dummyMethod()", + "param1", + "param2" + ); + SelenideLogger.commitStep(log, new Exception("something went wrong")); + }); + + final StepResult selenideStep = extractStepFromResults(results); + assertThat(selenideStep.getStatus()).isEqualTo(Status.BROKEN); + assertThat(selenideStep.getStatusDetails().getMessage()).isEqualTo("something went wrong"); + assertThat(selenideStep.getStage()).isEqualTo(Stage.FINISHED); + assertThat(selenideStep.getAttachments()).hasSize(0); + } + @AllureFeatures.Attachments @Test void shouldSaveLogs() { diff --git a/allure-selenide/src/test/java/io/qameta/allure/selenide/TestPhotographer.java b/allure-selenide/src/test/java/io/qameta/allure/selenide/TestPhotographer.java new file mode 100644 index 000000000..e9c83753d --- /dev/null +++ b/allure-selenide/src/test/java/io/qameta/allure/selenide/TestPhotographer.java @@ -0,0 +1,46 @@ +/* + * Copyright 2016-2026 Qameta Software Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.qameta.allure.selenide; + +import com.codeborne.selenide.impl.Photographer; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriver; + +import java.util.Optional; + +/** + * Test fixture for {@link AllureSelenideTest}, not a test class itself. + * + *

Registered globally via {@code META-INF/services/com.codeborne.selenide.impl.Photographer}, + * so it delegates to the driver (same as Selenide's default photographer) unless + * {@link #screenshot} is set by a test.

+ */ +public class TestPhotographer implements Photographer { + + static byte[] screenshot; + + @Override + public Optional takeScreenshot(final WebDriver webDriver, final OutputType outputType) { + if (screenshot != null) { + return Optional.of(outputType.convertFromPngBytes(screenshot)); + } + return webDriver instanceof TakesScreenshot + ? Optional.ofNullable(((TakesScreenshot) webDriver).getScreenshotAs(outputType)) + : Optional.empty(); + } +} + diff --git a/allure-selenide/src/test/resources/META-INF/services/com.codeborne.selenide.impl.Photographer b/allure-selenide/src/test/resources/META-INF/services/com.codeborne.selenide.impl.Photographer new file mode 100644 index 000000000..065cfb663 --- /dev/null +++ b/allure-selenide/src/test/resources/META-INF/services/com.codeborne.selenide.impl.Photographer @@ -0,0 +1 @@ +io.qameta.allure.selenide.TestPhotographer \ No newline at end of file