Groovy 只有第一个测试成功运行带有setupSpec()的Spock套件

Groovy 只有第一个测试成功运行带有setupSpec()的Spock套件,groovy,spock,geb,Groovy,Spock,Geb,我一直在用Geb/Spock测试一些web应用程序,但遇到了一个我似乎无法解决的问题 我有一个带有setupSpec()fixture方法的测试类,该方法调用AuthenticationSpec中定义的登录方法,然后还有一系列其他方法,在该页面上断言各种内容: class TestThisPage extends AuthenticationSpec { def setupSpec() { loginComplete("some user ID", "some passw

我一直在用Geb/Spock测试一些web应用程序,但遇到了一个我似乎无法解决的问题

我有一个带有setupSpec()fixture方法的测试类,该方法调用AuthenticationSpec中定义的登录方法,然后还有一系列其他方法,在该页面上断言各种内容:

class TestThisPage extends AuthenticationSpec {
    def setupSpec() {
        loginComplete("some user ID", "some password")
        assert page instanceof ThisPage
    }

    def "Verify some link's text is correct"() {
        when: "The user is at this page"

        then: "The this specific link's text is correct"
        assert  someLink.text().equalsIgnoreCase("I am some text")
    }

    def "Verify that the correct user name is displayed"() {
        when: "The user is signed in and at this page"

        then: "The signed-in user's display name appears correctly on the page"
        assert signInText.equalsIgnoreCase("Signed in as: some user ID")
    }

    def "Verify this page's copyright"() {
        when: "The user is redirected to this page"

        then: "The copyright is correct"
        assert legalInfo.text().trim().equalsIgnoreCase("Some copyright text here")
    }
}
现在,当我以测试套件的形式运行TestThisPage时,只有第一个测试用例(setupSpec()方法之后)通过了,其余的都失败了:
groovy.lang.MissingPropertyException:无法解析

我知道我的内容定义正确:

class ThisPage extends Page {

    static at = {
        title == "This page"
    }
    static content = {
        someLink(to: SomeOtherPage) {$("a[href='someOtherPage.jsp'")}
        signInText  {loginInfo.find("#static-label").text().trim()}
    }
}
我可以切换测试方法的顺序,第一个方法将始终通过,即使最后一次运行失败。如果我将这些测试方法作为单个单元测试运行,它们也都会通过


我实现此功能的唯一方法是在此页面中向when或给定块添加
。我想在每个测试方法中显式地将页面设置为ThisPage是有意义的,但是为什么在作为测试套件运行整个类时,只有第一个测试方法通过(即使没有ThisPage的
)?

每个后续测试方法都不知道您所在的页面

您可以创建页面的共享实例,然后在同一规范内的每个测试都可以访问该实例:

class TestThisPage extends AuthenticationSpec {

    @Shared ThisPage thisPage

    def setupSpec() {
        thisPage = loginComplete("some user ID", "some password")
        assert page instanceof ThisPage
    }

    def "Verify some link's text is correct"() {
        when: "The user is at this page"

        then: "The this specific link's text is correct"
        assert  thisPage.someLink.text().equalsIgnoreCase("I am some text")
    }

    def "Verify that the correct user name is displayed"() {
        when: "The user is signed in and at this page"

        then: "The signed-in user's display name appears correctly on the page"
        assert thisPage.signInText.equalsIgnoreCase("Signed in as: some user ID")
    }

    def "Verify this page's copyright"() {
        when: "The user is redirected to this page"

        then: "The copyright is correct"
        assert thisPage.legalInfo.text().trim().equalsIgnoreCase("Some copyright text here")
    }
}

GebReportingSpec有自己的
setupSpec
方法。如果将
super.setupSpec()
添加为
setupSpec
方法的第一行,该方法是否有效?IntelliJ提供了一条“对“setupSpec”的访问权限超过其访问权限”消息什么是
AuthenticationSpec
?它是@Stepwise规范吗?如果没有,Geb将为每个功能创建一个新的浏览器实例,这意味着您需要在每次清除每个新浏览器实例上的Cookie/session时再次登录。