From c5d9c55f4cb04a10a6cabecc1357a746918f4bee Mon Sep 17 00:00:00 2001 From: yaroslav-orel Date: Wed, 18 Sep 2019 15:16:04 +0300 Subject: [PATCH 1/2] Allow configurable page source --- .../allure/selenide/AllureSelenide.java | 19 ++++-- .../allure/selenide/AllureSelenideTest.java | 64 ++++++++++++++++++- 2 files changed, 77 insertions(+), 6 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 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() { From da91fe4d50b4a1b4d8339f7b6af927cc6919b20b Mon Sep 17 00:00:00 2001 From: yaroslav-orel Date: Wed, 18 Sep 2019 15:29:21 +0300 Subject: [PATCH 2/2] Fix checkstyle 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 b85cb133b..d7606b2c4 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 @@ -85,7 +85,7 @@ public AllureSelenide disableLogs(final LogType logType) { return this; } - public AllureSelenide configureSource(String type, String extension){ + public AllureSelenide configureSource(String type, String extension) { this.sourceType = type; this.sourceExtension = extension;