Skip to content

Commit

Permalink
Allow configurable page source
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-orel committed Sep 18, 2019
1 parent f50f4b9 commit c5d9c55
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogType, Level> logTypesToSave = new HashMap<>();
private final AllureLifecycle lifecycle;

Expand All @@ -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;
}

Expand All @@ -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<byte[]> getScreenshotBytes() {
try {
return WebDriverRunner.hasWebDriverStarted()
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit c5d9c55

Please sign in to comment.