0

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?

nmg49
  • 1,356
  • 1
  • 11
  • 28
  • Possible duplicate of [Class cannot resolve module as content unless @Stepwise used](https://stackoverflow.com/questions/46435166/class-cannot-resolve-module-as-content-unless-stepwise-used) – Leonard Brünings Oct 26 '17 at 19:29
  • Adding @Stepwise didn't solve anything? Also, the basis of that question was very different. In that case, it was a matter of mixing up setup() and setupSpec() methods. In my case, it's a data table failing to substitute values as one would expect. – nmg49 Oct 26 '17 at 19:43
  • Please read the accepted answer, `@Stepwise` is not the solution it just hid the problem. – Leonard Brünings Oct 27 '17 at 07:38
  • Okay I stand corrected, the answer is indeed buried in there. That being said, the approach to this question is different enough that I don't think it's fair to call it a duplicate. The other approach is "while is Stepwise doing this" and my question is "why are data tables doing this". I was not able to extrapolate my answer from this question, and I'm guessing neither would the majority of people. – nmg49 Oct 27 '17 at 12:23
  • I see this question has been down-voted a lot. What is wrong with it? It was clear enough for @erdi to give me a successful answer that I was able to use in my work. Could somebody give me feedback on how to make this a better question? – nmg49 Oct 27 '17 at 21:28
  • No idea why it was down voted. It's a valid question in my opinion and a lot of people get caught out, at least initially, by the fact that `where:` blocks are executed prior to execution of test iterations. – erdi Oct 29 '17 at 10:29

1 Answers1

7

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
erdi
  • 6,944
  • 18
  • 28