From 300998731fde6e258b56e537ea2ebefd704f51f3 Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Tue, 12 Nov 2024 10:24:07 +0100 Subject: [PATCH] refactor: Enhance readability by using state getters Replaced direct field access with state getters to improve readability and encapsulation. This change makes it clearer how state is accessed and managed within the class. --- .../grails/plugin/geb/ContainerGebSpec.groovy | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy b/src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy index 1abc77c..4fae793 100644 --- a/src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy +++ b/src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy @@ -61,25 +61,23 @@ class ContainerGebSpec extends GebSpec implements ContainerAwareDownloader { @PackageScope void initialize() { - if (!webDriverContainer) { - webDriverContainer = new BrowserWebDriverContainer() - Testcontainers.exposeHostPorts(port) - webDriverContainer.tap { - addExposedPort(this.port) - withAccessToHost(true) - start() - } - if (hostName != DEFAULT_HOSTNAME_FROM_CONTAINER) { - webDriverContainer.execInContainer('/bin/sh', '-c', "echo '$hostIp\t$hostName' | sudo tee -a /etc/hosts") - } - WebDriver driver = new RemoteWebDriver(webDriverContainer.seleniumAddress, new ChromeOptions()) - driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)) - browser.driver = driver + webDriverContainer = new BrowserWebDriverContainer() + Testcontainers.exposeHostPorts(port) + webDriverContainer.tap { + addExposedPort(this.port) + withAccessToHost(true) + start() } + if (hostNameChanged) { + webDriverContainer.execInContainer('/bin/sh', '-c', "echo '$hostIp\t$hostName' | sudo tee -a /etc/hosts") + } + WebDriver driver = new RemoteWebDriver(webDriverContainer.seleniumAddress, new ChromeOptions()) + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30)) + browser.driver = driver } void setup() { - initialize() + if (notInitialized) initialize() browser.baseUrl = "$protocol://$hostName:$port" } @@ -135,7 +133,7 @@ class ContainerGebSpec extends GebSpec implements ContainerAwareDownloader { */ @Override String getHostNameFromHost() { - return hostName == DEFAULT_HOSTNAME_FROM_CONTAINER ? DEFAULT_HOSTNAME_FROM_HOST : hostName + return hostNameChanged ? hostName : DEFAULT_HOSTNAME_FROM_HOST } @Override @@ -151,4 +149,12 @@ class ContainerGebSpec extends GebSpec implements ContainerAwareDownloader { private static String getHostIp() { PortForwardingContainer.INSTANCE.network.get().ipAddress } + + private boolean isHostNameChanged() { + return hostNameFromContainer != DEFAULT_HOSTNAME_FROM_CONTAINER + } + + private boolean isNotInitialized() { + webDriverContainer == null + } } \ No newline at end of file