可以将一个页面或一类元素看做是一个页面对象,一个页面对象对应一个类,将元素对象的获取定义在这一个类中,页面的元素分别定义,然后分别调用,使用@FindBy(id="XX")、@CacheLookup、public WebElement XX,然后调用PageFactory.initElements()方法来初始化元素,如:将百度首页的输入框与搜索按扭这两个元素分离,编写BaiduPage类,本文使用java编写
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
/*
* 把百度首页的输入框与搜索按扭这两个元素分离
* 用PageFactory.initElements()方法来初始化元素
*/
public class BaiduPage {
//定义百度首页搜索的输入框
@FindBy(id="kw")
@CacheLookup
public WebElement keyword_input;
//定义百度首页搜索按扭
@FindBy(id="su")
@CacheLookup
public WebElement search_button;
//创建一个构造函数,并且调用PageFactory.initElements()方法来初始化元素,换句话说,将元素映射到我们定义好的变量上
public BaiduPage(WebDriv