0

Given a geb.Page like:

class ConsultaPage extends Page {

static url = "web/search-basic"

static content = {
    titol { $('h4',0).text() }
    searchBox { $(name: "criteria").module(TextInput) }
    searchButton { $(By.id("submit_button")) }
    seriesBuscadorButton { $('img', src:"/web/resources/img/find") }
}

static at = {
    $("#serveis h2").text() == "Menú consulta"
}

void openSeriesBuscador(){
    seriesBuscadorButton.click()
}
}

And a Spec like:

class id_3031_PageSpec extends BaseGebSpec {

def "login ok com usuari normal accedeix a consulta i obre minibuscador series"(){
    given:
        via iArxiuPage
    when:
        // unrelated stuff...       
    then:
        to ConsultaPage
    and:
        //this works -> $('img', src:"/refweb/resources/img/find")*.click()            
        //this also works-> seriesBuscadorButton.click()            
        openSeriesBuscador()
    then:
        titol.equals("Cerca i selecció de sèrie")       
}
}

(BaseGebSpec is just a GebSpec with common setupSpec() for reading commons properties to all specs)

If I run this I get:

Condition not satisfied:

openSeriesBuscador()
|
null

Condition not satisfied:

openSeriesBuscador()
|
null

If instead of using: openSeriesBuscador() method (that uses a content variable with the same $() logic) I use $('img', src:"/refweb/resources/img/find").click() or seriesBuscadorButton.click() it works perfectly.

I think I 'm not understanding properly the content variables functionalities or accessing ways, but I'm not been able to find it looking at Geb Book. Can anyone help me to understand that behaviour?

Why accessing static content variable inside a method's Page fail but not accesing from the Spec class? Thanks in advance!

exoddus
  • 2,230
  • 17
  • 27

2 Answers2

1

the problem is in method

void openSeriesBuscador(){
    seriesBuscadorButton.click()
}

it should be

def openSeriesBuscador(){
    seriesBuscadorButton.click()
}

notice def instead of void

First thing is that you need to know is what does def stands for explained here: https://stackoverflow.com/a/9247169/426096

and "magic" in then block of spock: http://mrhaki.blogspot.de/2010/07/spock-spotlight-assert-magic.html

Community
  • 1
  • 1
gajo
  • 729
  • 3
  • 10
  • 19
  • As @erdi points, your solution make it works but my problem wasn't just about my method declaration but where it's called (then/and blocks). I've realized now that implicits asserts where executed over openSeriesBuscador() method, and that wasn't my intention. Thanks anyway for your help! – exoddus Dec 04 '15 at 10:31
  • @exoddus okay, but this is what you asked for. Sure, it is wrong how you've placed the code, but you've asked qoute: "Why accessing static content variable inside a method's Page fail but not accesing from the Spec class?" and if you want to use it, you need to use it like I've mentioned. Glad it works for you – gajo Dec 04 '15 at 13:17
1

You should call your openSeriesBuscador() in a when: block and not a then: block.

erdi
  • 6,944
  • 18
  • 28
  • I was calling in a "and:" block, not in a "then:" one. Anyway thanks for the advice! – exoddus Dec 04 '15 at 07:47
  • An `and:` block positioned after a `then:` block is equivalent to a `then:` block and statements put in such a block will be asserted for you hence the error. Changing the return type to `def` makes it work but it's more of a workaround then a solition because you don't really want to be asserting the result of that call in this case. – erdi Dec 04 '15 at 08:09