重构的geb登录规范到模块导致“未初始化”错误

时间:2017-09-27 22:46:53

标签: groovy spock functional-testing geb

我是新手为功能测试编写geb规范并感谢您对此问题的帮助。我也是Groovy的新手,“未初始化”错误可能与基本缺乏经验有关。我正在尝试构建一个模块来自动登录,此时我可以继续进行进一步的测试,但是我收到geb.error.ModuleInstanceNotInitializedException。以前,我没有使用模块,只能使用规范登录我的页面。我在这里根据rcgeorge23的响应答案重构了一个模块方法: How to refactor common Geb test sequences

规范:

import spock.lang.Unroll

class FlowSpecs extends GebReportingSpec {

    @Unroll
    def "setup Spec"() {
        given:
            to QUserPage
        when:    
            authModule.signIn("mwalle","password")
        then:
            assert { $("span", "class":"login-text z-label")[3].text() == "mwalle" }
    }

页面:

package pages.app

import geb.Page
import AuthModule

class QUserPage extends Page {

    static url = "/qsystem/quser"
    static at = { title == "QSystem" }

    static content = {
        authModule { module AuthModule }
    }
}

和模块:

import geb.Module 

class AuthModule extends Module {

    def idUser = js."zk.Widget.\$('\$usr').uuid" + "-real"
    def idPass = js."zk.Widget.\$('\$pwd').uuid"

    static content = {
        loginUser { $("input", id:idUser) }
        loginPass { $("input", id:idPass) }
        loginButton { $("button",class:"login-button z-button")[0] }
    }

    void signIn(String username = "mwalle", String password = "password") {
        loginUser.value(user)
        loginPass.value(pass)
        loginButton.click()
    }
}

完整错误是:

geb.error.ModuleInstanceNotInitializedException: Instance of module class AuthModule has not been initialized. Please pass it to Navigable.module() or Navigator.module() before using it.
    at geb.Module.uninitializedException(Module.groovy:757)
    at geb.Module.getJs(Module.groovy:96)
    at AuthModule.<init>(AuthModule.groovy:5)
    at geb.navigator.AbstractNavigator.module(AbstractNavigator.groovy:356)
    at geb.content.NavigableSupport.module(NavigableSupport.groovy:207)
    at geb.content.PageContentTemplateFactoryDelegate.module(PageContentTemplateFactoryDelegate.groovy:31)
    at pages.app.QUserPage._clinit__closure2$_closure3(QUserPage.groovy:12)
    at pages.app.QUserPage._clinit__closure2$_closure3(QUserPage.groovy)
    at geb.content.PageContentTemplate.invokeFactory(PageContentTemplate.groovy:97)
    at geb.content.PageContentTemplate.create_closure1(PageContentTemplate.groovy:59)
    at geb.content.PageContentTemplate.create_closure1(PageContentTemplate.groovy)
    at geb.content.PageContentTemplate.create(PageContentTemplate.groovy:82)
    at geb.content.PageContentTemplate.get(PageContentTemplate.groovy:54)
    at geb.content.DefaultPageContentSupport.getContent(DefaultPageContentSupport.groovy:42)
    at geb.content.PageContentSupport.propertyMissing(PageContentSupport.groovy:39)
    at geb.Page.propertyMissing(Page.groovy:112)
    at geb.Browser.propertyMissing(Browser.groovy:216)
    at geb.spock.GebSpec.propertyMissing(GebSpec.groovy:60)
    at FlowSpecs.setup Spec(FlowSpecs.groovy:23)

1 个答案:

答案 0 :(得分:1)

问题是这些行

def idUser = js."zk.Widget.\$('\$usr').uuid" + "-real"
def idPass = js."zk.Widget.\$('\$pwd').uuid"

js需要初始化的导航器,您可以使用at geb.Module.getJs(Module.groovy:96)

查看

我会强烈建议使用其他选择器(css​​或其他),如果你可以而不是使用javascript来查找id。如果你不能将查找代码移动到内容块中。

static content = {
    loginUser { $("input", id: js."zk.Widget.\$('\$usr').uuid" + "-real") }
    loginPass { $("input", id: js."zk.Widget.\$('\$pwd').uuid") }
    loginButton { $("button",class:"login-button z-button")[0] }
}