我正在遵循页面对象模型。假设我有一个扩展 BasePage 的 NewPage 类,并且在该类中我有如下方法:
public NewPage clickRed(){
driver.findElement(element1).click();
return this;
}
public NewPage clickBlue(){
driver.findElement(element2).click();
return this;
}
public OtherPage goToNextPage(){
this.clickRed();
this.clickBlue();
super.clickNextButton();
return new OtherPage(driver);
}
}
这样可以吗,或者对于 clickRed() 和 clickBlue() 方法,将这些方法设置为 public void 是更好的做法吗?就像这样:
public void clickRed(){
driver.findElement(element1).click();
}
最佳答案
这取决于您想要实现的目标。
我喜欢流畅的风格。因此,登录页面对象中的每个方法都会返回“this”。
loginPage.enteringUsername("uname")
.enteringPassword("pwd")
.submit();
如果您不喜欢这样,可以将它们保留为“无效”
<小时/>但是为什么每次在页面对象中都使用 driver.findElement
查找元素?
检查这个设计。 http://www.testautomationguru.com/arquillian-graphene-page-fragments/
关于java - 对于页面对象类中的方法,使用 public void 还是 public [页面名称] 更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44158823/