【问题标题】:Understanding how to access content objects at geb.Page了解如何在 geb.Page 访问内容对象
【发布时间】:2015-11-24 12:40:32
【问题描述】:

鉴于geb.Page 喜欢:

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()
}
}

还有一个像这样的规范:

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 只是一个带有通用 setupSpec() 的 GebSpec,用于读取所有规范的公共属性)

如果我运行它,我会得到:

Condition not satisfied:

openSeriesBuscador()
|
null

Condition not satisfied:

openSeriesBuscador()
|
null

如果不使用: openSeriesBuscador() 方法(使用具有相同 $() 逻辑的内容变量) 我使用$('img', src:"/refweb/resources/img/find").click()seriesBuscadorButton.click() 效果很好。

我认为我没有正确理解内容变量的功能或访问方式,但我无法通过 Geb Book 找到它。谁能帮我理解这种行为?

为什么在方法的页面中访问静态内容变量会失败,但不能从 Spec 类中访问? 提前致谢!

【问题讨论】:

    标签: java spock geb


    【解决方案1】:

    您应该在 when: 块而不是 then: 块中调用您的 openSeriesBuscador()

    【讨论】:

    • 我调用的是“and:”块,而不是“then:”块。无论如何感谢您的建议!
    • 位于then: 块之后的and: 块等同于then: 块,并且放置在这样一个块中的语句将为您断言,因此会出现错误。将返回类型更改为 def 使其工作,但它更多的是一种解决方法,而不是一种解决方案,因为在这种情况下你真的不想断言该调用的结果。
    【解决方案2】:

    问题出在方法上

    void openSeriesBuscador(){
        seriesBuscadorButton.click()
    }
    

    应该是

    def openSeriesBuscador(){
        seriesBuscadorButton.click()
    }
    

    注意 def 而不是 void

    首先你需要知道 def 代表什么,这里解释:https://stackoverflow.com/a/9247169/426096

    then spock 块中的“魔法”: http://mrhaki.blogspot.de/2010/07/spock-spotlight-assert-magic.html

    【讨论】:

    • 正如@erdi 指出的那样,您的解决方案使其有效,但我的问题不仅仅是我的方法声明,而是它的调用位置(然后/和块)。我现在已经意识到隐式断言在 openSeriesBuscador() 方法上执行的位置,这不是我的意图。无论如何感谢您的帮助!
    • @exoddus 好的,但这是你要求的。当然,您放置代码的方式是错误的,但是您问过 qoute:“为什么访问方法的 Page 内的静态内容变量失败但不能从 Spec 类访问?”如果你想使用它,你需要像我提到的那样使用它。很高兴它对你有用
    猜你喜欢
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多