Skip to content

Commit

Permalink
fix: enhance IDE autocompletion
Browse files Browse the repository at this point in the history
This commit introduces explicit delegation to improve convenience and support for IDE autocompletion. The following delegates have been added:

- `browser` object delegate
- `page` object delegate
- `report(String)` method delegate
- `getPageSource()` method delegate
- Refactor `ContainerAwareDownloadSupport` to a more generic support trait and move `createFileInputSource` method to it.
- Move support classes to `support` package.
  • Loading branch information
matrei committed Jan 22, 2025
1 parent 59cf0f6 commit 3ff12e8
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 75 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,12 @@ java {
sourceCompatibility = JavaVersion.VERSION_11
}

tasks.withType(GroovyCompile).configureEach {
// Preserve method parameter names in compiled Groovy classes
// IDE support for parameter hints
options.debug = true
options.compilerArgs << '--parameters'
}

tasks.named('bootRun') { enabled = false }
tasks.named('findMainClass') { enabled = false }

This file was deleted.

47 changes: 25 additions & 22 deletions src/testFixtures/groovy/grails/plugin/geb/ContainerGebSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package grails.plugin.geb

import geb.Browser
import geb.Page
import geb.report.CompositeReporter
import geb.report.PageSourceReporter
import geb.report.Reporter
import geb.test.GebTestManager
import geb.transform.DynamicallyDispatchesToBrowser
import grails.plugin.geb.support.ContainerGebFileInputSource
import grails.plugin.geb.support.ContainerSupport
import org.testcontainers.containers.BrowserWebDriverContainer
import org.testcontainers.images.builder.Transferable
import spock.lang.Shared
import spock.lang.Specification

Expand All @@ -48,12 +49,7 @@ import spock.lang.Specification
* @since 4.1
*/
@DynamicallyDispatchesToBrowser
abstract class ContainerGebSpec extends Specification implements ContainerAwareDownloadSupport {

@Shared
@Delegate(includes = ['getBrowser', 'report'])
@SuppressWarnings('unused')
static GebTestManager testManager
abstract class ContainerGebSpec extends Specification implements ContainerSupport {

/**
* Get access to container running the web-driver, for convenience to execInContainer, copyFileToContainer etc.
Expand All @@ -66,6 +62,27 @@ abstract class ContainerGebSpec extends Specification implements ContainerAwareD
@Shared
static BrowserWebDriverContainer container

@Shared
static GebTestManager testManager

@Delegate(interfaces = false)
Page getPage() {
testManager.browser.page
}

@Delegate
Browser getBrowser() {
testManager.browser
}

void report(String message) {
testManager.report(message)
}

String getPageSource() {
testManager.browser.driver.pageSource
}

static void setTestManager(GebTestManager testManager) {
this.testManager = testManager
}
Expand All @@ -80,18 +97,4 @@ abstract class ContainerGebSpec extends Specification implements ContainerAwareD
Reporter createReporter() {
new CompositeReporter(new PageSourceReporter())
}

/**
* Copies a file from the host to the container for assignment to a Geb FileInput module.
* This method is useful when you need to upload a file to a form in a Geb test and will work cross-platform.
*
* @param hostPath relative path to the file on the host
* @param containerPath absolute path to where to put the file in the container
* @return the file object to assign to the FileInput module
* @since 4.2
*/
File createFileInputSource(String hostPath, String containerPath) {
container.copyFileToContainer(Transferable.of(new File(hostPath).bytes), containerPath)
return new ContainerGebFileInputSource(containerPath)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 original author or authors
* Copyright 2024-2025 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 @@ -15,6 +15,7 @@
*/
package grails.plugin.geb

import grails.plugin.geb.support.LocalhostDownloadSupport
import grails.testing.mixin.integration.Integration
import groovy.transform.CompileStatic
import groovy.transform.TailRecursive
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.plugin.geb.support

import geb.download.DownloadSupport
import grails.plugin.geb.ContainerGebSpec
import groovy.transform.CompileStatic
import groovy.transform.SelfType
import org.testcontainers.images.builder.Transferable
import spock.lang.Shared

/**
* Features for supporting Geb tests running in a container.
*
* @author Mattias Reichel
* @since 4.1
*/
@CompileStatic
@SelfType(ContainerGebSpec)
trait ContainerSupport implements DownloadSupport {

@Shared
@Delegate
static DownloadSupport downloadSupport

/**
* Sets the {@link DownloadSupport} instance to use for file downloads.
* This allows for setting a custom implementation of {@code DownloadSupport}
* when downloading from a container.
*
* @param downloadSupport the {@code DownloadSupport} instance to use
* @since 4.1
*/
static void setDownloadSupport(DownloadSupport downloadSupport) {
this.downloadSupport = downloadSupport
}

/**
* Copies a file from the host to the container for assignment to a Geb FileInput module.
* This method is useful when you need to upload a file to a form in a Geb test and will work cross-platform.
*
* @param hostPath relative path to the file on the host
* @param containerPath absolute path to where to put the file in the container
* @return the file object to assign to the FileInput module
* @since 4.2
*/
File createFileInputSource(String hostPath, String containerPath) {
container.copyFileToContainer(Transferable.of(new File(hostPath).bytes), containerPath)
return new ContainerGebFileInputSource(containerPath)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2024 original author or authors
* Copyright 2024-2025 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 @@ -13,20 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.plugin.geb
package grails.plugin.geb.support

import geb.Browser
import geb.download.DefaultDownloadSupport
import groovy.transform.CompileStatic
import groovy.transform.PackageScope

import java.util.regex.Pattern

/**
* @author Mattias Reichel
* @since 4.1
*/
@PackageScope
@CompileStatic
class LocalhostDownloadSupport extends DefaultDownloadSupport {

Expand Down

0 comments on commit 3ff12e8

Please sign in to comment.