【问题标题】:What is the differnce between page object model and page factory in selenium?selenium 中的页面对象模型和页面工厂有什么区别?
【发布时间】:2016-03-08 11:26:51
【问题描述】:
看起来页面对象模型和页面工厂正在做同样的事情。所以我很困惑。
IpmObjectInitializer initialize = new IpmObjectInitializer(driver.getWebDriver());
// 初始化 BatchCreationPageFactory 类中的元素
batchCreationPageFactory = initialize.getBatchCreationPageFactoryObj();
【问题讨论】:
标签:
java
selenium
testing
selenium-webdriver
automated-tests
【解决方案1】:
Page Object 是一个代表网页并保存功能和成员的类。
public class LogInPage
{
private WebElement userName;
private WebElement password;
public LogInPage() {
}
public void locateElements() {
userName = driver.findElement(By.id("userName"));
password = driver.findElement(By.id("password"));
}
public void doLogIn() {
userName.sendKeys("qwe");
password.sendKeys("123");
}
}
Page Factory 是一种在创建页面对象实例时初始化要在页面对象中与之交互的 Web 元素的方法。
public class LogInPage
{
@FindBy(id="userName")
private WebElement userName;
@FindBy(id="password")
private WebElement password;
public LogInPage() {
PageFactory.initElements(driver, this); // initialize the members like driver.findElement()
}
public void doLogIn() {
userName.sendKeys("qwe");
password.sendKeys("123");
}
}
【解决方案2】:
页面对象模型 (POM)
1.. POM 是一种基于页面分离 selenium 代码的设计模式。
例如:为登录页面创建一个单独的 java 类,为主页创建一个类等。
2.. 页面对象模型是在测试框架中表示应用程序的一种方式。对于应用程序中的每个“页面”,您创建一个页面对象来引用“页面”。
页面工厂
1.. 高级概念(POM + 新功能)或
使用@FindBy 或@FindBys 注解识别元素
对Point#1中声明的所有元素进行初始化。
(在 POM 中,初始化发生在运行中)
PageFactory.initElements(driver,this);
2.. 页面工厂是实现页面对象模型的一种方式。为了支持 Page Object 模式,WebDriver 的支持库包含了一个工厂类。