Search code examples
groovyspockgeb

Using Spock Data Tables to Test Geb Page Objects


Full Disclosure: I'm very new to both Geb and Spock.

As part of a test suite I'm working on, we have to test run the same test on several page elements. I would love to be able to abstract this behavior using a Spock data-table. However, when I do this, Geb complains that it doesn't recognize the page property.

Here is a bare-bones example of what I'm talking about:

when:
textBox = value
submit()

then:"value is updated"
at SuccessPage
textBox == value

where:
textBox | value
box1    | val1
box2    | val2
box3    | val3

In this example, boxes 1-3 are defined in the content object of a Page.

These tests work when I do them independently, but not when I use a data table. Why isn't the Geb element getting substituted correctly?


Solution

  • Data tables are executed outside of the context of the test for which they are specified. They have to be executed that way to know how to actually construct multiple iterations of your test based on them. In that context box1 does not point to a page property as you're browser is not yet pointing at SuccessPage.

    To get around it you will need to use content names (which will be instances of String) and resolve them as properties of the page when you are in the right context:

    when:
    page."$textBox" = value
    submit()
    
    then:"value is updated"
    at SuccessPage
    page."$textBox" == value
    
    where:
    textBox | value
    'box1'  | val1
    'box2'  | val2
    'box3'  | val3