Composing Geb pages with Groovy traits
Asked Answered
F

1

6

I have a responsive site and would like to separate out the concerns of whether parts of my page template are collapsed from the main content per-page:

trait DesktopPage {
    static content = {
        header { $('nav', id:'nav-container') }
    }
}

trait MobilePage {
    // other stuff
}

trait HomePage {
    static url = ''
    static at = { title == 'My Site' }
}

class DesktopHomePage extends Page implements DesktopPage, HomePage {}

However, the Geb runtime does not appear to collect the static description blocks off of the traits, instead acting as if they weren't present.

Is it possible to compose concerns like this implicitly using traits with Geb? If not, is there a syntax that will let me pull in the information from implemented traits? HomePage.at doesn't resolve.

Frankie answered 16/9, 2014 at 23:58 Comment(3)
What happens if you remove the static in the traits?Baking
@WillP Doesn't work with the Geb idiom. It has an idiosyncratic DSL for those bits inspired by Grails.Frankie
Out of curiosity: do the behavior changes in any way if you declare those traits as interfaces?Baking
J
3

If you have look at the documentation on traits and static fields you will notice that it explicitly mentions that mixing in a trait that declares a static field doesn't add the field to the class. Geb was created way before traits were added to Groovy thus using them to compose page was definitely not taken into account when designing the API.

If the url and at checker are the same for both pages and only the content differs between mobile and desktop version why don't you simply use inheritance?

class HomePage {
    static url = ''
    static at = { title == 'My Site' }
}

class DesktopHomePage extends HomePage {
    static content = {...}
}

class MobileHomePage extends HomePage {
    static content = {...}
}
Jaymie answered 20/9, 2014 at 12:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.