0
我是新手写功能测试的geb规格,并感谢您对此问题的帮助。我对Groovy也不熟悉,而且“未初始化”错误可能是因为缺乏基本的经验。我正在尝试构建一个模块来自动登录,此时我可以继续进行进一步测试,但我收到geb.error.ModuleInstanceNotInitializedException
。以前,不使用模块,我只能使用规范登录到我的页面。基于rcgeorge23在这里的回答,我正在重构这个模块方法: How to refactor common Geb test sequences。重构geb登录规范导致“未初始化”的错误
该规范:
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)
这解决了这个问题,谢谢。我很好奇为什么你建议不要使用JS查找。 ZKoss随机化每个客户端负载上的id值,但保证每个具有id的html元素都有唯一的值,所以在开发应用程序时,我认为调用ZK客户端引擎来获取id要比选择其他可能会改变的东西好构建,然后导致测试失败,然后需要时间来重构。 – Mark
我对ZKoss并不熟悉,但通常最好使用常量的css/xpath选择器,例如'$('。login-form input [name =“username”]')'这样可以减少错误来源,从而使您的测试更可靠。而且,对浏览器的要求更少,从而加快了您的测试速度。在本地运行测试时,这可能会忽略不计,但如果您使用云服务,则延迟非常明显。 –