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

Browser console logs #3092

Merged
merged 3 commits into from
Aug 24, 2022
Merged
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
59 changes: 59 additions & 0 deletions docs/modules/plugins/pages/plugin-web-app.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,62 @@ xhr.onreadystatechange = function() {
xhr.send();` and save result to scenario variable `response`
Then `${response}` matcher `.+`
----

=== Browser logs steps

This set of steps allows to validate the https://developer.mozilla.org/en-US/docs/Web/API/console[browser console logging messages].

:log-levels: List of the comma-separated messages levels. The supported levels are: ERRORS, WARNINGS.

==== Validate log entries absence

Validates the absence of log entries of the desired level in the browser console.

[source,gherkin]
----
Then there are no browser console $logLevels
----
* `$logLevels` - {log-levels}

.Validate absence of JS errors
[source,gherkin]
----
Given I am on a page with the URL 'https://vividus-test-site.herokuapp.com/'
Then there are no browser console ERRORS
----

==== Validate specific log entries absence

Validates the absence of specific log entries of the desired level in the browser console.

[source,gherkin]
----
Then there are no browser console $logLevels by regex '$pattern'
----
* `$logLevels` - {log-levels}
* `$pattern` - The regular expression to match log entry messages.

.Validate absence of JS error referencing user
[source,gherkin]
----
Given I am on a page with the URL 'https://vividus-test-site.herokuapp.com/'
Then there are no browser console ERRORS by regex '.*user.*'
----

==== Validate specific log entries presence

Validates the presence of specific log entries of the desired level in the browser console.

[source,gherkin]
----
Then there are browser console $logLevels by regex '$pattern'
----
* `$logLevels` - {log-levels}
* `$pattern` - The regular expression to match log entry messages.

.Validate presence of JS errors referencing user
[source,gherkin]
----
Given I am on a page with the URL 'https://vividus-test-site.herokuapp.com/'
Then there are browser console ERRORS by regex '.*user.*'
----
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -59,24 +59,21 @@ public static void resetBuffer(WebDriver driver, boolean ignoreUnsupportedDriver
private static Optional<LogEntries> getLog(WebDriver driver, boolean ignoreUnsupportedDrivers)
{
// The Selenium log API isn't supported: https://github.com/w3c/webdriver/issues/406
if (WebDriverManager.isBrowserAnyOf(driver, Browser.FIREFOX, Browser.IE))
// Safari: https://developer.apple.com/documentation/webkit/macos_webdriver_commands_for_safari_11_1_and_earlier
if (WebDriverManager.isBrowserAnyOf(driver, Browser.FIREFOX, Browser.IE, Browser.SAFARI))
{
if (ignoreUnsupportedDrivers)
{
return Optional.empty();
}
throw new IllegalStateException("Firefox does not support retrieval of browser logs");
throw new IllegalStateException("Browser does not support retrieval of browser logs");
ikalinin1 marked this conversation as resolved.
Show resolved Hide resolved
}
return Optional.of(driver.manage().logs().get(LogType.BROWSER));
}

private static Stream<LogEntry> filter(LogEntries log, Level level)
{
int levelValue = level.intValue();
return log.getAll().stream().filter(logEntry ->
{
int logEntryLevel = logEntry.getLevel().intValue();
return logEntryLevel >= levelValue && logEntryLevel <= levelValue;
});
return log.getAll().stream().filter(logEntry -> levelValue == logEntry.getLevel().intValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.inject.Inject;
Expand Down Expand Up @@ -52,14 +53,9 @@ public class JsValidationSteps
* @param regex Regular expression to filter log entries
*/
@Then("there are browser console $logEntries by regex `$regex`")
public void checkThereAreLogEntriesOnOpenedPageFiltredByRegExp(List<BrowserLogLevel> logEntries, String regex)
public void checkThereAreLogEntriesOnOpenedPageFilteredByRegExp(List<BrowserLogLevel> logEntries, Pattern regex)
{
WebDriver webDriver = webDriverProvider.get();
Set<LogEntry> filteredLogEntries = BrowserLogManager.getFilteredLog(webDriver, logEntries).stream()
.filter(logEntry -> logEntry.getMessage().matches(regex))
.collect(Collectors.toSet());

publishAttachment(Map.of(webDriver.getCurrentUrl(), filteredLogEntries));
Set<LogEntry> filteredLogEntries = getLogEntries(logEntries, regex);
softAssert.assertFalse(String.format("Current page contains JavaScript %s by regex '%s'",
toString(logEntries), regex), filteredLogEntries.isEmpty());
}
Expand All @@ -77,8 +73,8 @@ public void checkThereAreLogEntriesOnOpenedPageFiltredByRegExp(List<BrowserLogLe
@Then("there are no browser console $logEntries")
public void checkJsLogEntriesOnOpenedPage(List<BrowserLogLevel> logEntries)
{
checkFilteredJsEntries(logEntries,
e -> includeBrowserExtensionLogEntries || !e.getMessage().contains("extension"));
checkLogMessagesAbsence(getLogEntries(logEntries,
e -> includeBrowserExtensionLogEntries || !e.getMessage().contains("extension")), logEntries);
}

/**
Expand All @@ -93,21 +89,31 @@ public void checkJsLogEntriesOnOpenedPage(List<BrowserLogLevel> logEntries)
* @param regex Regular expression to filter log entries
*/
@Then(value = "there are no browser console $logEntries by regex '$regex'", priority = 1)
public void checkJsLogEntriesOnOpenedPageFiltredByRegExp(List<BrowserLogLevel> logEntries, String regex)
public void checkJsLogEntriesOnOpenedPageFilteredByRegExp(List<BrowserLogLevel> logEntries, Pattern regex)
{
checkLogMessagesAbsence(getLogEntries(logEntries, regex), logEntries);
}

private void checkLogMessagesAbsence(Set<LogEntry> logEntries, List<BrowserLogLevel> logLevels)
{
checkFilteredJsEntries(logEntries, logEntry -> logEntry.getMessage().matches(regex));
softAssert.assertEquals("Current page contains no JavaScript " + toString(logLevels), 0,
logEntries.size());
}

private void checkFilteredJsEntries(List<BrowserLogLevel> logLevels, Predicate<? super LogEntry> filter)
private Set<LogEntry> getLogEntries(List<BrowserLogLevel> logEntries, Predicate<? super LogEntry> filter)
{
WebDriver webDriver = webDriverProvider.get();
Set<LogEntry> filteredLogEntries = BrowserLogManager.getFilteredLog(webDriver, logLevels).stream()
.filter(filter)
Set<LogEntry> filteredLogEntries = BrowserLogManager.getFilteredLog(webDriver, logEntries).stream()
.filter(filter::test)
.collect(Collectors.toSet());

publishAttachment(Map.of(webDriver.getCurrentUrl(), filteredLogEntries));
softAssert.assertEquals("Current page contains no JavaScript " + toString(logLevels), 0,
filteredLogEntries.size());
return filteredLogEntries;
}

private Set<LogEntry> getLogEntries(List<BrowserLogLevel> logEntries, Pattern regex)
{
return getLogEntries(logEntries, message -> regex.matcher(message.getMessage()).matches());
}

private void publishAttachment(Map<String, Set<LogEntry>> results)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,8 +29,12 @@
import java.util.Arrays;
import java.util.Set;
import java.util.logging.Level;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.WebDriver;
Expand Down Expand Up @@ -71,13 +75,21 @@ void shouldReturnFilteredLogContainingNoEntries()
assertEquals(emptySet(), filteredLog);
}

@Test
void shouldNotReturnLogsForFirefox()
static Stream<Arguments> notSupportedBrowsers()
{
WebDriver webDriver = mockBrowserName(Browser.FIREFOX.browserName());
return Stream.of(Arguments.of(Browser.IE),
Arguments.of(Browser.SAFARI),
Arguments.of(Browser.FIREFOX));
}

@ParameterizedTest
@MethodSource("notSupportedBrowsers")
void shouldFailWhenBrowserDoesntSupportsLogs(Browser browser)
{
WebDriver webDriver = mockBrowserName(browser.browserName());
IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> BrowserLogManager.getLog(webDriver));
assertEquals("Firefox does not support retrieval of browser logs", exception.getMessage());
assertEquals("Browser does not support retrieval of browser logs", exception.getMessage());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -52,7 +53,9 @@ class JsValidationStepsTests
{
private static final String URL = "url";
private static final String ERROR_MESSAGE = "error message";
private static final Pattern ERROR_MESSAGE_PATTERN = Pattern.compile(ERROR_MESSAGE);
private static final String EXTENSION = "extension ";
private static final Pattern EXTENSION_PATTERN = Pattern.compile(EXTENSION);
private static final String ERRORS_ASSERTION_DESCRIPTION = "Current page contains no JavaScript errors";

@Mock
Expand All @@ -68,11 +71,11 @@ class JsValidationStepsTests
private JsValidationSteps jsValidationSteps;

@Test
void testCheckThereAreLogEntriesOnOpenedPageFiltredByRegExp()
void testCheckThereAreLogEntriesOnOpenedPageFilteredByRegExp()
{
Map<String, Set<LogEntry>> expectedResults = testCheckJsErrors(ERROR_MESSAGE, () -> jsValidationSteps
.checkThereAreLogEntriesOnOpenedPageFiltredByRegExp(singletonList(BrowserLogLevel.ERRORS),
ERROR_MESSAGE));
.checkThereAreLogEntriesOnOpenedPageFilteredByRegExp(singletonList(BrowserLogLevel.ERRORS),
ERROR_MESSAGE_PATTERN));
InOrder inOrder = inOrder(attachmentPublisher, softAssert);
verifyAttachmentPublisher(expectedResults, inOrder);
inOrder.verify(softAssert).assertFalse(String.format("Current page contains JavaScript %s by regex '%s'",
Expand All @@ -91,7 +94,8 @@ void testCheckJsErrorsOnPage()
void testCheckJsErrorsOnPageByRegExpNoMatch()
{
testCheckJsErrors(ERROR_MESSAGE, () -> jsValidationSteps
.checkJsLogEntriesOnOpenedPageFiltredByRegExp(singletonList(BrowserLogLevel.ERRORS), EXTENSION));
.checkJsLogEntriesOnOpenedPageFilteredByRegExp(singletonList(BrowserLogLevel.ERRORS),
EXTENSION_PATTERN));
verifyTestActions(Map.of(URL, Collections.emptySet()), ERRORS_ASSERTION_DESCRIPTION);
}

Expand Down