我正在使用geb spock和groovy创建一个自动化脚本。在我的测试类中,我有多个测试,我想在运行任何测试之前导航到主页
我创建了一个方法并尝试在我的geb测试中重用它,但出现以下错误:
geb.error.PageInstanceNotInitializedException: Instance of page class pages.Test_HomePage has not been initialized. Please pass it to Browser.to(), Browser.via(), Browser.page() or Browser.at() before using it.
at geb.Page.uninitializedException(Page.groovy:521)
at geb.content.UninitializedPageContentSupport.getContent(UninitializedPageContentSupport.groovy:30)
at geb.content.PageContentSupport.propertyMissing(PageContentSupport.groovy:39)
at geb.Page.propertyMissing(Page.groovy:99)
at pages.Test_HomePage.clickOnHomePage(Test_HomePage.groovy:46)
at com.abc.vcctest.TestNavigationInitialTestSpec.Navigate to Home page of test portal(TestNavigationInitialTestSpec.groovy:44)
这是我的示例geb测试:
@TestMixin(GrailsUnitTestMixin)
class TestNavigationInitialTestSpec extends LoginBaseTestSpec {
@Shared
Test_HomePage test_HomePage = new Test_HomePage()
def cleanupSpec() {
browser.close()
}
def setup() {
}
def cleanup() {
}
def navigateToHomePage() {
Test_HomePage test_HomePage = at Test_HomePage
test_HomePage.clickOnHomePage()
at Test_HomePage
}
def "Navigate to Home page of test portal"() {
given:
HomePage homePage = at HomePage
when: "Click on Home tab/link"
navigateToHomePage()
test_HomePage.clickOnHomePage()
def crashDisplayed = test_HomePage.crashPageDisplayed()
then: "You are on Home Page"
!crashDisplayed || { at Test_HomePage }
}
def "Navigate to Change Password page of testportal"() {
given:
at Test_HomePage
when: "Click on Change Password"
test_HomePage.clickOnChangePassword()
Test_ChangePassword test_ChangePassword = at Test_ChangePassword
def crashDisplayed1 = test_ChangePassword.crashPageDisplayed()
then: "You are on Change Password Page"
!crashDisplayed1 || { at Test_ChangePassword }
at Test_ChangePassword
}
这是我的Test_HomePage类:
class Test_HomePage extends HomePage2{
static url = '/home'
static at = {
waitFor(message:"The Test Portal title is missing."){driver.title == "Test Portal"}
}
static content = {
pageCrash(required: false, cache: false) { $("h1") }
homePageLink {
$("a[href = '/Link1']")
}
changePasswordLink{
$("a[href='/Link2']")
}
updateEmailLink{
$ ("a[href = '/Link3']")
}
}
void crashPageDisplayed() {
if (pageCrash.isDisplayed()) {
driver.navigate().back()
}
}
void clickOnHomePage() {
homePageLink.click()
}
void clickOnChangePassword(){
changePasswordLink.click()
}
void clickUpdateEmail(){
updateEmailLink.click()
}
}
我是geb和spock的新手,我可能会犯一个非常愚蠢的错误,但任何帮助或建议都将非常感谢,以有效的方式实现这一点。
您应该使功能方法彼此独立,以便可以按任意顺序调用它们。你为什么不把类似的东西放到你的 setup()
方法中Test_HomePage
?然后,它将在每个特征方法的开头调用。即使您在给定的:
块中为每个单个特征方法执行此操作,您仍然需要检查
之前。
我想你应该稍微研究一下盖布书。顺便说一下,这就是错误消息试图向您解释的内容:
geb.error.PageInstanceNotInitializedException:
Instance of page class pages.Test_HomePage has not been initialized.
Please pass it to Browser.to(), Browser.via(), Browser.page() or Browser.at() before using it.
因此,在< code > navigatetohome page()中,您的第一个命令应该是:
Test_HomePage test_HomePage = to Test_HomePage