Add methods to an assigned closure with GroovyDSL

162 views Asked by At

Geb uses a static field called content to define the contents of a page or module. The value of the content field is a closure.

 
class GebishOrgHomePage extends Page {
    static content = {
        manualsMenu {
            module MenuModule, $("#header-content ul li", 0)
        }
        links { $('.link-list li a') }
    }
}

Intellij already has support for this content dsl, however it does not support the module and moduleList methods. This causes limited auto-complete support when working with modules.

To fix this I'd like to write a GroovyDSL script that adds the missing method definitions to the content closure and its nested closures. However, I've no idea how to add methods to a closure that is not passed to a method, since enclosingCall requires a concrete method name.

And the other thing is that those methods must have a generic return type like this:


<T extends Module> T module(Class<T> m) {
    // return an instance of T
}
1

There are 1 answers

7
erdi On

If you use the latest snapshot then module() calls will be understood by your IDE. This is down to moving module() to Navigator exactly for what you are after - autocompletion and strong typing.

Have a look at the current version of section 6.4 of the Book of Geb. The moduleList() will be gone in a future release and that section explains what to use instead. The module() method taking a map argument to initialise module properties will also go, you now initialise the module yourself and pass the instance to module() and there is an example of doing this in 6.4. Thanks to all that you will get autocompletion around module defintions and usage in IntelliJ.