Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configurable page source in AllureSelenide #394

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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 @@ -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