From 6a935bd4c1c162f78bdf64d6f0c932de4c2f7bad Mon Sep 17 00:00:00 2001 From: Giulio Longfils Date: Wed, 25 Dec 2024 22:41:21 +0100 Subject: [PATCH] feature: configuration key to keep the browser open at the end --- .../giulong/spectrum/utils/Configuration.java | 3 +++ .../spectrum/utils/events/DriverConsumer.java | 5 ++++- .../resources/yaml/configuration.default.yaml | 2 ++ .../utils/events/DriverConsumerTest.java | 22 ++++++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) 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 caa22c20..9ebb699e 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 @@ -163,6 +163,9 @@ public static class Extent { @Generated public static class Drivers { + @JsonPropertyDescription("Whether to keep the driver open after the execution") + private boolean keepOpen; + @JsonPropertyDescription("Driver's fluent waits") private Waits waits; diff --git a/spectrum/src/main/java/io/github/giulong/spectrum/utils/events/DriverConsumer.java b/spectrum/src/main/java/io/github/giulong/spectrum/utils/events/DriverConsumer.java index 53c65914..c694c43f 100644 --- a/spectrum/src/main/java/io/github/giulong/spectrum/utils/events/DriverConsumer.java +++ b/spectrum/src/main/java/io/github/giulong/spectrum/utils/events/DriverConsumer.java @@ -23,7 +23,10 @@ public void accept(final Event event) { final Configuration.Runtime runtime = configuration.getRuntime(); - runtime.getDriver().shutdown(); + if (!configuration.getDrivers().isKeepOpen()) { + runtime.getDriver().shutdown(); + } + runtime.getEnvironment().shutdown(); } } diff --git a/spectrum/src/main/resources/yaml/configuration.default.yaml b/spectrum/src/main/resources/yaml/configuration.default.yaml index 42b0bb70..4fcce9e3 100644 --- a/spectrum/src/main/resources/yaml/configuration.default.yaml +++ b/spectrum/src/main/resources/yaml/configuration.default.yaml @@ -75,6 +75,8 @@ environments: # Drivers configuration drivers: + keepOpen: false # Whether to keep the driver open after the execution + waits: implicit: 0 # Seconds Selenium waits before throwing a NoSuchElementException when an element isn't found pageLoadTimeout: 10 # Seconds that Selenium waits before throwing an exception because the page wasn't fully loaded yet diff --git a/spectrum/src/test/java/io/github/giulong/spectrum/utils/events/DriverConsumerTest.java b/spectrum/src/test/java/io/github/giulong/spectrum/utils/events/DriverConsumerTest.java index 69a259c3..cd6833b4 100644 --- a/spectrum/src/test/java/io/github/giulong/spectrum/utils/events/DriverConsumerTest.java +++ b/spectrum/src/test/java/io/github/giulong/spectrum/utils/events/DriverConsumerTest.java @@ -23,6 +23,9 @@ class DriverConsumerTest { @Mock private Configuration.Runtime runtime; + @Mock + private Configuration.Drivers drivers; + @Mock private Event event; @@ -52,10 +55,12 @@ void acceptSkipped() { } @Test - @DisplayName("accept should shutdown the driver") + @DisplayName("accept should shutdown the driver and the environment") void accept() { when(event.getResult()).thenReturn(SUCCESSFUL); when(configuration.getRuntime()).thenReturn(runtime); + when(configuration.getDrivers()).thenReturn(drivers); + when(drivers.isKeepOpen()).thenReturn(false); doReturn(driver).when(runtime).getDriver(); doReturn(environment).when(runtime).getEnvironment(); @@ -64,4 +69,19 @@ void accept() { verify(driver).shutdown(); verify(environment).shutdown(); } + + @Test + @DisplayName("accept should not shutdown the driver if drivers.keepOpen is true") + void acceptKeepOpen() { + when(event.getResult()).thenReturn(SUCCESSFUL); + when(configuration.getRuntime()).thenReturn(runtime); + when(configuration.getDrivers()).thenReturn(drivers); + when(drivers.isKeepOpen()).thenReturn(true); + doReturn(environment).when(runtime).getEnvironment(); + + driverConsumer.accept(event); + + verify(driver, never()).shutdown(); + verify(environment).shutdown(); + } }