使用页面工厂模型自动下拉

时间:2016-06-06 23:37:58

标签: selenium selenium-webdriver

我在我的项目中使用Page工厂模型,因此:

public class Loginsingup {
  WebDriver driver;
  public Loginsingup(WebDriver driver) {
    this.driver = driver; PageFactory.initElements(driver, this);
  }

  @FindBy(id="FirstName") WebElement fname;
}

如何识别页面工厂中的下拉值。我知道另一种方式:

Select country = new Select(driver.findElement(By.id("SelectedCountryID")));    
country.selectByVisibleText("UNITED STATES");

但如何在页面工厂中执行此操作。我为所有页面创建了单独的Object Repository包,并为测试用例创建了另一个包。

我已将所有java类保存在对象存储库中。但是元素就像删除我在java类中编写的内容以及如何在测试用例中访问它。

2 个答案:

答案 0 :(得分:1)

在WebElement访问器周围添加一个简单的包装器访问器,如下所示:

@FindBy(id="SelectedCountryID") WebElement countryDropdown;

public Select getCountrySelect() {
  return new Select(countryDropdown);
}

您也可以添加有用的mutators,就像这样(我发明了Country类:您可能更喜欢传递ID或其他内容):

public void setCountry(Country country) {
  getCountrySelect().selectByValue(country.getId());
}

答案 1 :(得分:1)

对于Page Factory select下拉,给定的代码将起作用:

@FindBy(id="FirstName") WebElement fname;

Select dropdown = new Select(fname);  
dropdown.selectByVisibleText("UNITED STATES");