我为登录页面创建了单独的类,如下所示:
class LoginPage extends Page{
static at = { title == "XXX" }
static content = {
username(wait:true) { $(By.xpath('//input[@name="mailAddress"]'))}
password(wait:true) { $(By.xpath('//input[@name="password"]'))}
}
}
现在使用以下代码访问另一个名为HomePageLogin的类的用户名和密码:
Browser.drive {
to LoginPage
at LoginPage
report "AtLoginPage"
username.value("username")
password.value("password")
}
但是收到以下错误:
Caught: groovy.lang.MissingPropertyException: No such property: username for class: MyPackage.HomePageLogin
groovy.lang.MissingPropertyException: No such property: username for class: MyPackage.HomePageLogin
at MyPackage.HomePageLogin$_run_closure1.doCall(HomePageLogin.groovy:18)
at MyPackage.HomePageLogin$_run_closure1.doCall(HomePageLogin.groovy)
at geb.Browser.drive(Browser.groovy:1061)
at geb.Browser$drive$0.callStatic(Unknown Source)
at geb.Browser.drive(Browser.groovy:1031)
at geb.Browser$drive.call(Unknown Source)
at MyPackage.HomePageLogin.run(HomePageLogin.groovy:12)
如何设置用户名和密码?
我可以使用以下代码在HomePageLogin类中设置名称和密码:
Browser.drive {
to LoginPage
at LoginPage
report "AtLoginPage"
$("input",name:"mailAddress").value("ID")
$("input",name:"password").value("pass")
}
但我不想要内联脚本。我希望通过相应的类来定义和访问元素,在这种情况下是LoginPage。
答案 0 :(得分:1)
如果$("input",name:"")
选择器有效,请尝试将它们放在页面对象中,如下所示:
class LoginPage extends Page {
static at = { title == "XXX" }
static content = {
username(wait:true) { $("input",name:"mailAddress") }
password(wait:true) { $("input",name:"password") }
}
}