I wanted to start practicing some more of GEB/selenium best practices.
I found some documentation that I am trying to mimic but I am not getting the desired result and I believe it may be do to my misunderstanding.
I found this code snippet here:
class HomePage extends geb.Page {
static content = {
loginLink(to: LoginPage) { $("#login_link") }
}
LoginPage clickLoginLink() {
loginLink.click()
return browser.page
}
}
class LoginPage extends geb.Page {
static content = {
usernameField { $("#username") }
passwordField { $("#password") }
loginButton(to: DashboardPage) { $("#login_button") }
}
DashboardPage login(String username, String password) {
usernameField.value(username)
passwordField.value(password)
loginButton.click()
return browser.page
}
}
It then goes on to say that this structure allows you to make this call:
DashboardPage dashboardPage = to(HomePage).clickLoginLink().login('user1', 'password1')
I have tried to set up something slightly similar the code is as follows:
Page:
class LoginPage extends Page {
static at = { $(By.xpath("//h1[text()='Welcome to Your Company JIRA']")).displayed }
static url = "http://localhost:2990/jira/login.jsp"
static content = {
LOGIN_USER {$(By.id("login-form-username"))}
LOGIN_PASS {$(By.id("login-form-password"))}
LOGIN_BTN (to: SystemDashboardPage, toWait:10) {$(By.id("login-form-submit"))}
banner {module JiraBanner}
}
def logIn(user, pass){
LOGIN_USER.value(user)
LOGIN_PASS.value(pass)
LOGIN_BTN.click()
return browser.page
}
}
Script:
class Test extends GebReportingSpec {
//The Pages we will use for this test
@Shared
def loginPage = new LoginPage()
def systemDashboardPage = new SystemDashboardPage()
def "Login to Jira"(){
when: "we navigate to Jira Login"
loginPage = to LoginPage
and: "we login"
systemDashboardPage = loginPage.logIn("admin", "admin")
then: "We should be at the home page"
assert systemDashboardPage instanceof SystemDashboardPage
}
}
def "Navigate to Issues Admin Page"(){
when: "we Navigate to the Admin Issues Page"
issuesAdminsPage = systemDashboardPage.banner.goToIssuesAdmin()
then: "we should be at the issue admin Page"
assert issuesAdminsPage instanceof IssuesAdminPage
}
When I run this code though I get:
Instance of page class Pages.SystemDashboardPage has not been initialized.
Please pass it to Browser.to(), Browser.via(), Browser.page() or
Browser.at() before using it.
geb.error.PageInstanceNotInitializedException: Instance of page class
Pages.SystemDashboardPage has not been initialized. Please pass it to
Browser.to(), Browser.via(), Browser.page() or Browser.at() before using it.
From what I can understand from the example, returning browser.page should be initializing the page since I added the to
flag to the static content.
What am I missing here?
答案 0 :(得分:0)
我需要将测试脚本中@Shared
下的全局变量设置为static
,如果你问我,这实际上是反直觉的。
所以而不是
def loginPage = new LoginPage()
def systemDashboardPage = new SystemDashboardPage()
我需要把
static loginPage = new LoginPage()
static systemDashboardPage = new SystemDashboardPage()
你不能动态地改变全局变量而不将它们作为静态变形.......