断言页面时Geb'为null'

时间:2013-07-09 17:39:27

标签: grails spock geb

我正在尝试使用Grails编写一个简单的Geb / Spock测试但是我收到了以下测试失败。

| Failure:  login works correctly(...UserAuthAcceptanceSpec)
|  Condition not satisfied:

at HomePage
|
null

我可以使用浏览器通过调试器跟踪测试,并且可以看到应用程序按预期工作并显示正确的标题。但是,当我尝试调用at检查程序时,测试失败。任何人都可以告诉我为什么测试中的最终断言可能会失败以及为什么'at'检查器似乎为空?

这是我的代码:( Geb v0.9.0,Grails 2.2.2)

Spock规范:

class UserAuthAcceptanceSpec extends GebReportingSpec {

    def "login works correctly"() {

        given: "the correct credentials"
            def theCorrectUsername = "admin"
            def theCorrectPassword = "password"

        when: "logging in"
            to LoginPage
            username = theCorrectUsername
            password = theCorrectPassword 
            submitButton.click() //([HomePage, LoginPage])

        then: "the welcome page is shown"
            heading =~ /(?i)Welcome.*/   // <- same as 'at' checker in HomePage
        and: "the 'at' checker works"
            at HomePage                  // <- fails

    }

LoginPage

class LoginPage extends Page {

    final String path = "/login/auth"

    static content = {
        heading(required: false, wait:true) { $("h1").text() }
        username     { $("input", name:"j_username") }
        password     { $("input", name:"j_password") }
        submitButton { $("input", id:"submit") }
    }

    static at = {
        title =~ /Login.*/
    }

}

主页

class HomePage extends Page {

    final String path   = "/"

    static content = {
        heading(required: false, wait:true) { $("h1").text() }
    }

    static at = {
        heading =~ /(?i)Welcome.*/
    }

}

1 个答案:

答案 0 :(得分:1)

at检查程序应使用==~而不是=~

Geb的隐含断言意味着陈述:

heading1 =~ /(?i)Welcome.*/
heading2 ==~ /(?i)Welcome.*/

有效地成为:

assert (heading1 =~ /(?i)Welcome.*/) == true     // [1]      
assert (heading2 ==~ /(?i)Welcome.*/) == true    // [2] 

[2]将按预期评估为布尔值并传递/失败,而[1]将评估为导致失败的java.util.regex.Matcher

有关两种语法之间差异的解释,请参阅Groovy Regex FAQ