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 ec3c081ef..b85cb133b 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 @@ -49,7 +49,9 @@ public class AllureSelenide implements LogEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(AllureSelenide.class); private boolean saveScreenshots = true; - private boolean savePageHtml = true; + private boolean savePageSource = true; + private String sourceType = "text/html"; + private String sourceExtension = "html"; private final Map logTypesToSave = new HashMap<>(); private final AllureLifecycle lifecycle; @@ -66,8 +68,8 @@ public AllureSelenide screenshots(final boolean saveScreenshots) { return this; } - public AllureSelenide savePageSource(final boolean savePageHtml) { - this.savePageHtml = savePageHtml; + public AllureSelenide savePageSource(final boolean savePageSource) { + this.savePageSource = savePageSource; return this; } @@ -83,6 +85,13 @@ public AllureSelenide disableLogs(final LogType logType) { return this; } + public AllureSelenide configureSource(String type, String extension){ + this.sourceType = type; + this.sourceExtension = extension; + + return this; + } + private static Optional getScreenshotBytes() { try { return WebDriverRunner.hasWebDriverStarted() @@ -129,9 +138,9 @@ public void afterEvent(final LogEvent event) { getScreenshotBytes() .ifPresent(bytes -> lifecycle.addAttachment("Screenshot", "image/png", "png", bytes)); } - if (savePageHtml) { + if (savePageSource) { getPageSourceBytes() - .ifPresent(bytes -> lifecycle.addAttachment("Page source", "text/html", "html", bytes)); + .ifPresent(bytes -> lifecycle.addAttachment("Page source", sourceType, sourceExtension, bytes)); } if (!logTypesToSave.isEmpty()) { logTypesToSave 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 bd77fa701..d4298e487 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 @@ -30,8 +30,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openqa.selenium.OutputType; -import org.openqa.selenium.WebDriverException; import org.openqa.selenium.WebDriver.Options; +import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.logging.LogEntries; import org.openqa.selenium.logging.LogEntry; @@ -191,6 +191,68 @@ void shouldSavePageSourceOnFail() { .isEqualTo("dummy-page-source"); } + @AllureFeatures.Attachments + @Test + void shouldHaveHTMLPageSourceAsDefault() { + final ChromeDriver wdMock = mock(ChromeDriver.class); + WebDriverRunner.setWebDriver(wdMock); + doReturn("html-page-source") + .when(wdMock).getPageSource(); + + final AllureResults results = runWithinTestContext(() -> { + final AllureSelenide selenide = new AllureSelenide() + .screenshots(false) + .savePageSource(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(attachment.getType()).isEqualTo("text/html"); + } + + @AllureFeatures.Attachments + @Test + void shouldAllowToConfigurePageSource() { + final String sourceType = "text/xml"; + final ChromeDriver wdMock = mock(ChromeDriver.class); + WebDriverRunner.setWebDriver(wdMock); + doReturn("xml-page-source") + .when(wdMock).getPageSource(); + + final AllureResults results = runWithinTestContext(() -> { + final AllureSelenide selenide = new AllureSelenide() + .screenshots(false) + .savePageSource(true) + .configureSource(sourceType, "xml"); + 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(attachment.getType()).isEqualTo(sourceType); + } + @AllureFeatures.Attachments @Test void shouldNotFailIfBrowserIsNotOpened() {