【问题标题】:Is it better to use public void or public [name of page] for methods in page object class?对页面对象类中的方法使用 public void 或 public [页面名称] 是否更好?
【发布时间】:2017-05-24 12:46:06
【问题描述】:

我正在关注页面对象模型。假设我有一个扩展 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 clickRed(){
    driver.findElement(element1).click();
}

【问题讨论】:

  • 这没什么大不了的,我认为两者都可以,但我更喜欢使用第一种方法,因为有时写这样的东西更方便:newPage.clickRed().clickBlue()。 ..
  • 哦,链接对吗?很高兴知道

标签: java selenium selenium-webdriver


【解决方案1】:

这取决于你想要达到的目标。

我喜欢流畅的风格。因此,登录页面对象中的每个方法都返回“this”。

loginPage.enteringUsername("uname")
         .enteringPassword("pwd")
         .submit();

如果你不喜欢这样,你可以让它们'无效'


但是为什么每次在页面对象中都使用driver.findElement 找到元素?

检查这个设计。 http://www.testautomationguru.com/arquillian-graphene-page-fragments/

【讨论】:

  • 谢谢,我去看看页面碎片,以前没听说过!
猜你喜欢
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2021-02-07
  • 2011-08-03
  • 1970-01-01
相关资源
最近更新 更多