【问题标题】:Execution order of Inherited class and @FindBy classInherited 类和@FindBy 类的执行顺序
【发布时间】: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(),那么根据我的执行步骤将是:

  1. [在 TestClass 中] 创建了 Firefox 驱动程序
  2. 网站将在 Firefox 浏览器中打开 [在 TestClass 中]
  3. @FindBy 注释将首先被调用或加载到内存中(我认为这是延迟加载)[在 TestPO 中]
  4. 静态块 [在 BasePO 中]
  5. initElements [在 BasePO 中]
  6. 可以访问搜索框的 WebElement,并且可以在 [in TestClass] 上执行 sendKeys 操作

但如果我不正确,那么何时会调用 @FindBy 注释以及执行顺序是什么

【问题讨论】:

  • 为什么不尝试将调试点放在以上所有内容上。并查看执行顺序,这就是您想要的。

标签: java selenium testng


【解决方案1】:

执行顺序为:

  1. [在 TestClass 中] 创建了 Firefox 驱动程序
  2. 网站将在 Firefox 浏览器中打开 [在 TestClass 中]
  3. 静态块 [在 BasePO 中]
  4. initElements [在 BasePO 中]
  5. @FindBy 注释将被初始化(在 PageFactory.java 中定义的初始化)[在 TestPO 中]
  6. 可以访问搜索框的WebElement,并且可以在[在TestClass]上执行sendKeys操作

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多