一个方法returnsbrowser.page是什么意思?

What does it mean if a method returns browser.page?

我想开始练习更多 GEB/selenium 最佳实践。

我发现了一些我试图模仿的文档,但我没有得到想要的结果,我相信这可能是我的误解造成的。

我找到了这段代码 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
  }
}

然后继续说这个结构允许你进行这个调用:

DashboardPage dashboardPage = to(HomePage).clickLoginLink().login('user1', 'password1')

我尝试设置了一些类似的代码如下:

页数:

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
    }

}

脚本:

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
}

当我 运行 这段代码时,我得到:

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.

从示例中我可以理解,返回 browser.page 应该是初始化页面,因为我向静态内容添加了 to 标志。

我在这里错过了什么?

我需要将测试脚本中 @Shared 下的全局变量设置为 static 如果你问我这实际上是违反直觉的。

所以

def loginPage = new LoginPage()
def systemDashboardPage = new SystemDashboardPage()

我需要输入

static loginPage = new LoginPage()
static systemDashboardPage = new SystemDashboardPage()

您不能动态更改全局变量而不将它们转换为静态变量......