Skip to content

Commit

Permalink
[plugin-web-app] Refactor console validation steps
Browse files Browse the repository at this point in the history
  • Loading branch information
ikalinin1 committed Aug 22, 2022
1 parent 5f5433a commit 7eeb518
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
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 @@ -73,10 +73,6 @@ private static Optional<LogEntries> getLog(WebDriver driver, boolean ignoreUnsup
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 @@ -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

0 comments on commit 7eeb518

Please sign in to comment.