Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composing Geb pages with Groovy traits

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.

like image 611
chrylis -cautiouslyoptimistic- Avatar asked Sep 16 '14 23:09

chrylis -cautiouslyoptimistic-


1 Answers

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 = {...}
}
like image 88
erdi Avatar answered Oct 14 '22 14:10

erdi