【发布时间】:2016-12-03 10:58:14
【问题描述】:
我们使用 @FindBy 注释来获取使用页面对象建模概念的 Web 元素。
例如
@FindBy(xpath = "//input[@type='text'][@placeholder='Search']")
WebElement searchBox;
但为了获得searchBox 元素,我们需要先调用PageFactory.initElements(driver, this);,然后才能对该元素执行操作。
我已经设计了以下框架。
父类:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import core.driver.WebDriverManager;
public abstract class BasePO {
static {
System.out.println("Static Block");
}
BasePO() {
System.out.println("initElements");
initElements();
}
private void initElements() {
PageFactory.initElements(getDriver(), this);
}
}
儿童班:
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class TestPO extends BasePO{
@FindBy(css = "input[title='Search']")
WebElement searchBox;
@FindBy(css = "input[value='Google Search']")
WebElement searchButton;
public void sendKeys(String inputText){
searchBox.sendKeys(inputText);
}
public void clickOnSearchButton(){
searchButton.click();
}
}
测试类:
import org.testng.annotations.Test;
import core.pageobject.TestPO;
public class TestClass{
@Test
public void test123() {
driver = new FirefoxDriver();
driver.get("http://www.gsmarena.com/");
TestPO tpo = new TestPO();
tpo.search("iphone 7");
}
}
如果我运行测试用例:test(),那么根据我的执行步骤将是:
- [在 TestClass 中] 创建了 Firefox 驱动程序
- 网站将在 Firefox 浏览器中打开 [在 TestClass 中]
-
@FindBy注释将首先被调用或加载到内存中(我认为这是延迟加载)[在 TestPO 中] - 静态块 [在 BasePO 中]
- initElements [在 BasePO 中]
- 可以访问搜索框的 WebElement,并且可以在 [in TestClass] 上执行 sendKeys 操作
但如果我不正确,那么何时会调用 @FindBy 注释以及执行顺序是什么
【问题讨论】:
-
为什么不尝试将调试点放在以上所有内容上。并查看执行顺序,这就是您想要的。