From eb1d5cbb34262792f46b1e0b5570a3c68d8bd23b Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Thu, 7 Nov 2024 22:27:55 +0100 Subject: [PATCH 1/4] refactor: Leverage Reflections in SpectrumEntity.getSharedFields() --- .../io/github/giulong/spectrum/SpectrumEntity.java | 11 +---------- .../io/github/giulong/spectrum/SpectrumTest.java | 3 ++- .../github/giulong/spectrum/utils/Reflections.java | 10 +++++++--- .../github/giulong/spectrum/SpectrumEntityTest.java | 6 ++++-- .../giulong/spectrum/utils/ReflectionsTest.java | 12 ++++++++++++ 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumEntity.java b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumEntity.java index cfbba30c..82dc55ae 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumEntity.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumEntity.java @@ -5,8 +5,8 @@ import com.aventstack.extentreports.Status; import com.aventstack.extentreports.model.Media; import io.github.giulong.spectrum.interfaces.Shared; -import io.github.giulong.spectrum.utils.*; import io.github.giulong.spectrum.types.TestData; +import io.github.giulong.spectrum.utils.*; import io.github.giulong.spectrum.utils.events.EventsDispatcher; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -17,12 +17,10 @@ import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.WebDriverWait; -import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; import java.security.MessageDigest; import java.util.Arrays; -import java.util.List; import static com.aventstack.extentreports.MediaEntityBuilder.createScreenCaptureFromPath; import static com.aventstack.extentreports.Status.*; @@ -85,13 +83,6 @@ public abstract class SpectrumEntity, Data> { @Shared JsWebElementProxyBuilder jsWebElementProxyBuilder; - List getSharedFields() { - return Arrays - .stream(SpectrumEntity.class.getDeclaredFields()) - .filter(f -> f.isAnnotationPresent(Shared.class)) - .toList(); - } - /** * Hovers on the provided WebElement, leveraging the {@code actions} field * diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumTest.java b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumTest.java index 67b9fadb..b01905f6 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumTest.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumTest.java @@ -4,6 +4,7 @@ import io.github.giulong.spectrum.extensions.interceptors.SpectrumInterceptor; import io.github.giulong.spectrum.extensions.resolvers.*; import io.github.giulong.spectrum.extensions.watchers.EventsWatcher; +import io.github.giulong.spectrum.interfaces.Shared; import io.github.giulong.spectrum.types.*; import io.github.giulong.spectrum.utils.*; import io.github.giulong.spectrum.utils.events.EventsDispatcher; @@ -106,7 +107,7 @@ void beforeEach(final TestContext testContext, final Configuration configuration final Class clazz = this.getClass(); log.debug("Initializing pages of test '{}'", clazz.getSimpleName()); - final List sharedFields = getSharedFields(); + final List sharedFields = Reflections.getAnnotatedFields(SpectrumEntity.class, Shared.class); return Reflections .getFieldsOf(clazz, SpectrumTest.class) diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java index e72036e7..5dcba0e5 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Reflections.java @@ -105,18 +105,22 @@ public static void copyField(final Field field, final Object source, final Objec field.set(dest, field.get(source)); } - public static List getAnnotatedFields(final Object object, final Class annotation) { - final String className = object.getClass().getTypeName(); + public static List getAnnotatedFields(final Class clazz, final Class annotation) { + final String className = clazz.getTypeName(); final String annotationName = annotation.getTypeName(); return Arrays - .stream(object.getClass().getDeclaredFields()) + .stream(clazz.getDeclaredFields()) .filter(f -> f.isAnnotationPresent(annotation)) .peek(f -> log.debug("Field {}.{} is annotated with {}", className, f.getName(), annotationName)) .peek(f -> f.setAccessible(true)) .toList(); } + public static List getAnnotatedFields(final Object object, final Class annotation) { + return getAnnotatedFields(object.getClass(), annotation); + } + public static List getAnnotatedFieldsValues(final Object object, final Class annotation, final Class clazz) { return getAnnotatedFields(object, annotation) .stream() diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumEntityTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumEntityTest.java index cd9060e1..b5a6665f 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumEntityTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumEntityTest.java @@ -3,8 +3,10 @@ import com.aventstack.extentreports.ExtentTest; import com.aventstack.extentreports.Status; import com.aventstack.extentreports.model.Media; +import io.github.giulong.spectrum.interfaces.Shared; import io.github.giulong.spectrum.types.TestData; import io.github.giulong.spectrum.utils.Configuration; +import io.github.giulong.spectrum.utils.Reflections; import io.github.giulong.spectrum.utils.StatefulExtentTest; import lombok.SneakyThrows; import org.junit.jupiter.api.AfterAll; @@ -115,9 +117,9 @@ private void addScreenshotToReportStubs() { } @Test - @DisplayName("getSharedFields should return the list of fields of SpectrumEntity.class that are annotated with @Shared") + @DisplayName("checking shared fields") void getSharedFields() { - final List actual = spectrumEntity.getSharedFields(); + final List actual = Reflections.getAnnotatedFields(SpectrumEntity.class, Shared.class); final List sharedFieldsNames = actual .stream() .map(Field::getName) diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java index 52072107..1f21d7fe 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/utils/ReflectionsTest.java @@ -165,6 +165,18 @@ void copyFieldSame() { assertEquals(parentField, Reflections.getFieldValue(parentField, dummyThird)); } + @Test + @DisplayName("getAnnotatedFields should return the list of fields on the provided class which are annotated with the provided annotation") + void getAnnotatedFieldsClass() throws IllegalAccessException { + final String value = "value"; + final Dummy dummy = new Dummy(null, value, null); + + final List actual = Reflections.getAnnotatedFields(Dummy.class, Secured.class); + + assertEquals(1, actual.size()); + assertEquals(value, actual.getFirst().get(dummy)); + } + @Test @DisplayName("getAnnotatedFields should return the list of fields on the provided object which are annotated with the provided annotation") void getAnnotatedFields() throws IllegalAccessException { From 4b7a8f9f903755c35feeff045e0466f51c048ebd Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Sun, 10 Nov 2024 16:59:14 +0100 Subject: [PATCH 2/4] fix: SpectrumPage.isLoaded doesn't append the slash between the baseUrl and the endpoint --- .../io/github/giulong/spectrum/SpectrumPage.java | 2 +- .../giulong/spectrum/SpectrumPageTest.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumPage.java b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumPage.java index 3c891d62..e8d67294 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumPage.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/SpectrumPage.java @@ -60,7 +60,7 @@ public T waitForPageLoading() { */ public boolean isLoaded() { final String currentUrl = driver.getCurrentUrl(); - final String pageUrl = configuration.getApplication().getBaseUrl() + endpoint; + final String pageUrl = String.format("%s/%s", configuration.getApplication().getBaseUrl(), endpoint.replaceFirst("/", "")); log.debug("Current url: {}", currentUrl); log.debug("Page url: {}", pageUrl); diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumPageTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumPageTest.java index 38165e29..693353a1 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumPageTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/SpectrumPageTest.java @@ -25,8 +25,6 @@ class SpectrumPageTest { - private final String endpoint = "/endpoint"; - private MockedStatic jsWebElementListInvocationHandlerMockedStatic; @Mock @@ -85,6 +83,7 @@ void afterEach() { @DisplayName("open should get the configured base url and wait for the page to be loaded") void open() { final String url = "url"; + final String endpoint = "/endpoint"; Reflections.setField("endpoint", spectrumPage, endpoint); when(configuration.getApplication()).thenReturn(application); @@ -101,23 +100,24 @@ void waitForPageLoading() { } @DisplayName("isLoaded should check if the current page url matches the endpoint") - @ParameterizedTest(name = "with page url {0} and current url {1} we expect {2}") + @ParameterizedTest(name = "with base url {0}, endpoint {1}, and current url {2} we expect {3}") @MethodSource("valuesProvider") - void isLoaded(final String pageUrl, final String currentUrl, final boolean expected) { + void isLoaded(final String baseUrl, final String endpoint, final String currentUrl, final boolean expected) { Reflections.setField("endpoint", spectrumPage, endpoint); //noinspection DataFlowIssue - when(webDriver.getCurrentUrl()).thenReturn(currentUrl + endpoint); + when(webDriver.getCurrentUrl()).thenReturn(currentUrl); when(configuration.getApplication()).thenReturn(application); - when(application.getBaseUrl()).thenReturn(pageUrl); + when(application.getBaseUrl()).thenReturn(baseUrl); assertEquals(expected, spectrumPage.isLoaded()); } static Stream valuesProvider() { return Stream.of( - arguments("current", "current", true), - arguments("page", "current", false) + arguments("baseUrl", "/endpoint", "baseUrl/endpoint", true), + arguments("baseUrl", "endpoint", "baseUrl/endpoint", true), + arguments("baseUrl", "/endpoint", "nope", false) ); } From 4ad5230e556eb5370645f4ccf2253e0b6edfd12a Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Sun, 10 Nov 2024 17:01:24 +0100 Subject: [PATCH 3/4] docs: replacing outdated openjdk 21 url --- README.md | 2 +- docs/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 74c8192b..7fd329d6 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ All you need to do is take the three steps listed below, as shown in this video: https://github.com/giulong/spectrum/assets/27963644/fecee8f5-f6dc-4b47-81a3-514e66b3907d > ⚠️ JDK
-> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/21/) to be able to run your tests. +> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/archive/) to be able to run your tests. 1. Generate a new project leveraging the [Spectrum Archetype](https://mvnrepository.com/artifact/io.github.giulong/spectrum-archetype) via your IDE or by running this in a terminal and providing values when prompted: diff --git a/docs/README.md b/docs/README.md index 5251d9c9..158f6932 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,7 @@ and **mobile and desktop applications** via [Appium](http://appium.io/docs/en/la ## Setup > ⚠️ **JDK**
-> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/21/){:target="_blank"} to be able to run your tests. +> Since Spectrum is compiled with a jdk 21, you need a [jdk 21+](https://jdk.java.net/archive/){:target="_blank"} to be able to run your tests. > If you get an `Unsupported major.minor version` exception, the reason is that you're using an incompatible java version. ### Spectrum Archetype From f473651b0ff97ea8fa7788a401d60fa5d76f4717 Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Sun, 10 Nov 2024 18:22:50 +0100 Subject: [PATCH 4/4] fix: web driver's events are not logged by default. Event.level is now of type org.slf4j.event.Level --- docs/README.md | 12 +-- .../internals/SpectrumWebDriverListener.java | 7 +- .../giulong/spectrum/utils/Configuration.java | 5 +- .../resources/yaml/configuration.default.yaml | 1 - .../SpectrumWebDriverListenerTest.java | 74 +++++++------------ 5 files changed, 38 insertions(+), 61 deletions(-) diff --git a/docs/README.md b/docs/README.md index 158f6932..71fefde2 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1232,7 +1232,7 @@ Given [Spectrum intercepts web driver's events](#webdriver-events-listener), it such as `beforeSendKeys` and `afterSendKeys`, send sensitive data to logs and html report in plain text. To avoid this, you just need to decorate the sensitive web elements with `@Secured`, and the sensitive data will be redacted -with `[***]`. The replacement will only affect events' consumers such as logs and html report, +with `[***]`. The replacement will only affect events' consumers such as logs and html report, of course the actual value will still be sent to or read from the web element. {% include copyCode.html %} @@ -1313,11 +1313,11 @@ Spectrum with an events listener used to automatically take actions such as logging and generating reports. You can tweak each event in your `configuration.yaml`, by providing these: -| Key | Type | Default | Description | -|---------|---------------------------------------------------------------------------------------------|---------|-----------------------------------------------------| -| level | [Level](https://logback.qos.ch/apidocs/ch/qos/logback/classic/Level.html){:target="_blank"} | `OFF` | Level at which this event will be logged | -| message | String | `null` | Message to be logged upon receiving this event | -| wait | long | `0` | Milliseconds to wait before listening to this event | +| Key | Type | Default | Description | +|---------|---------------------------------------------------------------------------------|---------|-----------------------------------------------------| +| level | [Level](https://www.slf4j.org/api/org/slf4j/event/Level.html){:target="_blank"} | `null` | Level at which this event will be logged | +| message | String | `null` | Message to be logged upon receiving this event | +| wait | long | `0` | Milliseconds to wait before listening to this event | For example, you can set these: diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListener.java b/spectrum/src/main/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListener.java index 58bc44c0..64e31098 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListener.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListener.java @@ -25,14 +25,11 @@ import static io.github.giulong.spectrum.enums.Frame.AUTO_AFTER; import static io.github.giulong.spectrum.enums.Frame.AUTO_BEFORE; -import static org.slf4j.event.Level.*; @Slf4j @Builder public class SpectrumWebDriverListener implements WebDriverListener { - private static final Map LEVELS = Map.of("TRACE", TRACE, "INFO", INFO, "WARN", WARN); - private Pattern locatorPattern; private Events events; private List> consumers; @@ -59,9 +56,9 @@ protected List parse(final Object[] args) { @SneakyThrows protected void listenTo(final Frame frame, final Configuration.Drivers.Event event, final Object... args) { - final Level level = LEVELS.getOrDefault(event.getLevel().levelStr, DEBUG); + final Level level = event.getLevel(); - if (!log.isEnabledForLevel(level)) { + if (level == null || !log.isEnabledForLevel(level)) { return; } diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Configuration.java b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Configuration.java index 7434435c..caa22c20 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/utils/Configuration.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/utils/Configuration.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.logging.Level; -import static ch.qos.logback.classic.Level.OFF; import static lombok.AccessLevel.PRIVATE; @SuppressWarnings("unused") @@ -499,8 +498,8 @@ public static class Events { public static class Event { @JsonPropertyDescription("Level at which this event will be logged") - @JsonSchemaTypes(value = String.class, valueList = {"OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"}) - private final ch.qos.logback.classic.Level level = OFF; + @JsonSchemaTypes(value = String.class, valueList = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"}) + private org.slf4j.event.Level level; @JsonPropertyDescription("Message to be logged upon receiving this event") private String message; diff --git a/spectrum/src/main/resources/yaml/configuration.default.yaml b/spectrum/src/main/resources/yaml/configuration.default.yaml index 50f07844..9fb4ec31 100644 --- a/spectrum/src/main/resources/yaml/configuration.default.yaml +++ b/spectrum/src/main/resources/yaml/configuration.default.yaml @@ -320,7 +320,6 @@ drivers: beforeGetSize: message: Getting size of %1$s afterGetSize: - level: OFF message: Size of %1$s is %2$s beforeGetCssValue: message: Getting css property %2$s of element %1$s diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListenerTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListenerTest.java index f0a0c8f8..a64dec50 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListenerTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/internals/SpectrumWebDriverListenerTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockedStatic; @@ -27,9 +28,6 @@ import java.util.regex.Pattern; import java.util.stream.Stream; -import static ch.qos.logback.classic.Level.ALL; -import static ch.qos.logback.classic.Level.OFF; -import static ch.qos.logback.classic.Level.WARN; import static io.github.giulong.spectrum.enums.Frame.AUTO_AFTER; import static io.github.giulong.spectrum.enums.Frame.AUTO_BEFORE; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -237,7 +235,7 @@ void listenTrace() { ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.TRACE); when(event.getMessage()).thenReturn(message); - when(event.getLevel()).thenReturn(Level.TRACE); + when(event.getLevel()).thenReturn(TRACE); when(event.getWait()).thenReturn(0L); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -249,8 +247,8 @@ void listenTrace() { @Test @DisplayName("TRACE level off: listen should not log the provided event") void listenTraceOff() { - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(OFF); - when(event.getLevel()).thenReturn(Level.TRACE); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.OFF); + when(event.getLevel()).thenReturn(TRACE); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -263,9 +261,9 @@ void listenTraceOff() { void listenDebug() { webDriverEventStubsAtLevel(DEBUG); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.DEBUG); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.DEBUG); when(event.getMessage()).thenReturn(message); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.DEBUG); + when(event.getLevel()).thenReturn(DEBUG); when(event.getWait()).thenReturn(wait); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -277,8 +275,8 @@ void listenDebug() { @Test @DisplayName("DEBUG level off: listen should not log the provided event") void listenDebugOff() { - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(OFF); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.DEBUG); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.OFF); + when(event.getLevel()).thenReturn(DEBUG); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -291,9 +289,9 @@ void listenDebugOff() { void listenInfo() { webDriverEventStubsAtLevel(INFO); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.INFO); when(event.getMessage()).thenReturn(message); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + when(event.getLevel()).thenReturn(INFO); when(event.getWait()).thenReturn(wait); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -305,8 +303,8 @@ void listenInfo() { @Test @DisplayName("INFO level off: listen should not log the provided event") void listenInfoOff() { - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(OFF); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.OFF); + when(event.getLevel()).thenReturn(INFO); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -319,7 +317,7 @@ void listenInfoOff() { void listenWarn() { webDriverEventStubsAtLevel(org.slf4j.event.Level.WARN); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(WARN); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.WARN); when(event.getMessage()).thenReturn(message); when(event.getLevel()).thenReturn(WARN); when(event.getWait()).thenReturn(wait); @@ -333,7 +331,7 @@ void listenWarn() { @Test @DisplayName("WARN level off: listen should not log the provided event") void listenWarnOff() { - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(OFF); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.OFF); when(event.getLevel()).thenReturn(WARN); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -342,28 +340,12 @@ void listenWarnOff() { verifyNoInteractions(consumers); } - @SuppressWarnings("deprecation") - @Test - @DisplayName("Default level: listen should log at DEBUG level as per logback default") - void listenDefault() { - webDriverEventStubsAtLevel(DEBUG); - - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ALL); - when(event.getMessage()).thenReturn(message); - when(event.getLevel()).thenReturn(ALL); - - spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); - - verify(consumer1).accept(webDriverEvent); - verify(consumer2).accept(webDriverEvent); - } - - @SuppressWarnings("deprecation") - @Test - @DisplayName("Default level: listen should log at DEBUG level as per logback default") - void listenDefaultOFF() { - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(OFF); - when(event.getLevel()).thenReturn(ALL); + @DisplayName("Default level (null): listen should not log regardless of the log's level") + @ParameterizedTest(name = "with log level {0}") + @ValueSource(strings = {"ERROR", "WARN", "INFO", "DEBUG", "TRACE"}) + void listenDefault(final String level) { + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.toLevel(level)); + when(event.getLevel()).thenReturn(null); spectrumWebDriverListener.listenTo(AUTO_BEFORE, event, arg); @@ -381,10 +363,10 @@ void beforeSendKeys() { when(testContext.isSecuredWebElement(webElement1)).thenReturn(false); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.INFO); when(events.getBeforeSendKeys()).thenReturn(event); when(event.getMessage()).thenReturn(localMessage); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + when(event.getLevel()).thenReturn(INFO); when(event.getWait()).thenReturn(wait); when(WebDriverEvent.builder()).thenReturn(webDriverEventBuilder); @@ -420,10 +402,10 @@ void beforeSendKeysSecured() { when(testContext.isSecuredWebElement(webElement1)).thenReturn(true); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.INFO); when(events.getBeforeSendKeys()).thenReturn(event); when(event.getMessage()).thenReturn(localMessage); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + when(event.getLevel()).thenReturn(INFO); when(event.getWait()).thenReturn(wait); when(WebDriverEvent.builder()).thenReturn(webDriverEventBuilder); @@ -459,10 +441,10 @@ void afterSendKeys() { when(testContext.isSecuredWebElement(webElement1)).thenReturn(false); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.INFO); when(events.getAfterSendKeys()).thenReturn(event); when(event.getMessage()).thenReturn(localMessage); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + when(event.getLevel()).thenReturn(INFO); when(event.getWait()).thenReturn(wait); when(WebDriverEvent.builder()).thenReturn(webDriverEventBuilder); @@ -498,10 +480,10 @@ void afterSendKeysSecured() { when(testContext.isSecuredWebElement(webElement1)).thenReturn(true); - ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(ch.qos.logback.classic.Level.INFO); + ((Logger) LoggerFactory.getLogger(SpectrumWebDriverListener.class)).setLevel(Level.INFO); when(events.getAfterSendKeys()).thenReturn(event); when(event.getMessage()).thenReturn(localMessage); - when(event.getLevel()).thenReturn(ch.qos.logback.classic.Level.INFO); + when(event.getLevel()).thenReturn(INFO); when(event.getWait()).thenReturn(wait); when(WebDriverEvent.builder()).thenReturn(webDriverEventBuilder);