【问题标题】:Only first test succeeds running Spock suite with setupSpec()只有第一个测试成功运行带有 setupSpec() 的 Spock 套件
【发布时间】:2017-04-28 14:34:15
【问题描述】:

我一直在尝试使用 Geb/Spock 来测试一些 Web 应用程序,但遇到了一个我似乎无法弄清楚的问题。

我有一个带有 setupSpec() 固定方法的测试类,它调用在 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: Unable to resolve <some content identifier specified on ThisPage>

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

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

我可以切换测试方法的顺序,第一个总是通过,即使最后一次运行失败。如果我将这些测试方法作为单个单元测试运行,它们也都通过了。

我让它工作的唯一方法是如果我将at ThisPage 添加到 when 或 given 块。我想在每个测试方法中将页面显式设置为 ThisPage 是有意义的,但是为什么在将整个类作为测试套件运行时,只有第一个测试方法通过(即使没有 at ThisPage)?

【问题讨论】:

  • GebReportingSpec 有自己的 setupSpec 方法。如果在 setupSpec 方法的第一行添加 super.setupSpec() 是否有效?
  • IntelliJ 给出“对 'setupSpec' 的访问权限超出其访问权限”消息
  • AuthenticationSpec 是什么?
  • 它是@Stepwise 规范吗?如果没有,那么 Geb 将为每个功能创建一个新的浏览器实例,这意味着您每次都需要重新登录,因为每个新浏览器实例上的 cookie/会话都会被清除。

标签: groovy spock geb


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2021-12-17
    • 2014-10-13
    • 2015-02-19
    相关资源
    最近更新 更多