Skip to content

Commit

Permalink
refactor: Enhance readability by using state getters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matrei committed Nov 12, 2024
1 parent 95ea2a0 commit 3009987
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
}

0 comments on commit 3009987

Please sign in to comment.