Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,9 +133,7 @@ public AllureSelenide disableLogs(final LogType logType) {

private static Optional<byte[]> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AllureSelenideTest {
@AfterEach
void closeBrowser() {
WebDriverRunner.closeWebDriver();
TestPhotographer.screenshot = null;
}

@AllureFeatures.Steps
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>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.</p>
*/
public class TestPhotographer implements Photographer {

static byte[] screenshot;

@Override
public <T> Optional<T> takeScreenshot(final WebDriver webDriver, final OutputType<T> outputType) {
if (screenshot != null) {
return Optional.of(outputType.convertFromPngBytes(screenshot));
}
return webDriver instanceof TakesScreenshot
? Optional.ofNullable(((TakesScreenshot) webDriver).getScreenshotAs(outputType))
: Optional.empty();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.qameta.allure.selenide.TestPhotographer