代码之家  ›  专栏  ›  技术社区  ›  Mark

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

  •  0
  • Mark  · 技术社区  · 8 年前

    我刚开始编写功能测试的geb规范,非常感谢您在这个问题上的帮助。我对Groovy也很陌生,“未初始化”错误可能与这方面的基本经验不足有关。我正在尝试构建一个模块来自动登录,在这一点上,我可以继续进行进一步的测试,但我收到一个 geb.error.ModuleInstanceNotInitializedException 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 回复  |  直到 8 年前
        1
  •  1
  •   Leonard Brünings    8 年前

    问题是这些线路

    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] }
    }
    
    推荐文章