开发Selenium WebDriver测试时,可以使用页面对象模型,这样可使得测试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来。同时页面对象模型也提供了一个注释,帮助缓存远程,避免出现元素过期的问题。
// 定义百度搜索的输入框
@FindBy(id = "kw")
@CacheLookup
public WebElement keyword_input;
三行代码定义一个元素,是一个整体
@FindBy: 定义了你所有查找的元素是以什么方式定位的,此处用的是id,还可以用@FindBy(name = "xx"), @FindBy(className = "xx"), @FindBy(xpath = "xx"), @FindBy(css = "xx")等。
@CacheLookup:即找到元素之后就缓存元素,重复使用的时候可以加快测试速度
WebElement keyword_input:变量名
分离页面元素:
BDPage.java文件内容:
package page;
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;
public class BDPage {
// 定义百度搜索的输入框
@FindBy(id = "kw")
@CacheLookup
public WebElement keyword_input;
// 定义百度搜索的搜索按钮
@FindBy(id = "su")
@CacheLookup
public WebElement search_button;
// 构造函数来初始化元素,即将元素映射到定义好的变量上
public BDPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
}
BDPageTest.java文件代码:
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import page.BDPage;
public class BDPageTest {
WebDriver driver;
@Test
public void f() {
BDPage bdp = new BDPage(driver);
bdp.keyword_input.sendKeys("hello");
bdp.search_button.click();
}
@BeforeTest
public void beforeTest() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://baidu.com");
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
即使实现的分离页面元素,也同样可以分离页面操作,即把一些操作封装到对应页面中。
以下是打开百度主页,并输入“selenium”点击查询按钮的测试用例
BaiDuPage.java文件代码:
package page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BaiDuPage {
// 定义百度搜索的输入框
@FindBy(id = "kw")
@CacheLookup
private WebElement BD_INPUT_KEYWORD;
// 定义百度搜索的搜索按钮
@FindBy(id = "su")
@CacheLookup
private WebElement BD_BUTTON_SEARCH;
private final String url = "https://baidu.com";
private static WebDriver driver;
// 提供一个外部获得driver的方法
public WebDriver getDriver() {
return driver;
}
// 构造函数来初始化元素,即将元素映射到定义好的变量上
public BaiDuPage() {
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
}
public void close() {
driver.quit();
}
public void openUrl() {
driver.get(url);
}
public void searchByKeyword() {
BD_INPUT_KEYWORD.sendKeys("selenium");
BD_BUTTON_SEARCH.click();
}
}
BaiDuPageTest.java文件代码:
package test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import page.BaiDuPage;
public class BaiDuPageTest {
@Test
public void f() {
BaiDuPage baidupage = new BaiDuPage();
baidupage.openUrl();
baidupage.searchByKeyword();
baidupage.close();
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
}
}
页面嵌套对象
使用页面嵌套对象,可以简化测试用例的步骤。
a、打开百度页面
b、输入“selenium关键字”
c、在搜索结果页面的搜索框中检查输入的文本是不是“selenium”
BaiDuPage.java :存储页面元素,相关操作以及嵌套ResultPage对象
ResultPage.java : 存储页面元素以及相关操作
BaiDuPageTest: 执行测试,检查结果
BaiDuPage.java
package page;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BaiDuPage {
// 定义百度搜索的输入框
@FindBy(id = "kw")
@CacheLookup
private WebElement BD_INPUT_KEYWORD;
// 定义百度搜索的搜索按钮
@FindBy(id = "su")
@CacheLookup
private WebElement BD_BUTTON_SEARCH;
private final String url = "https://baidu.com";
private static WebDriver driver;
// 提供一个外部获得driver的方法
public static WebDriver getDriver() {
return driver;
}
// 构造函数来初始化元素,即将元素映射到定义好的变量上
public BaiDuPage() {
driver = new FirefoxDriver();
PageFactory.initElements(driver, this);
}
public void close() {
driver.quit();
}
public void openUrl() {
driver.get(url);
}
public ResultPage searchByKeyword(String keyword) {
BD_INPUT_KEYWORD.sendKeys(keyword);
BD_BUTTON_SEARCH.click();
return new ResultPage();
}
}
ResultPage.java
package page;
import junit.framework.Assert;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class ResultPage {
// 定义百度搜索结果界面的输入框
@FindBy(id = "kw")
@CacheLookup
private WebElement RP_INPUT_KEYWORD;
public ResultPage() {
PageFactory.initElements(BaiDuPage.getDriver(), this);
}
public void checkKeyword() {
Assert.assertEquals(RP_INPUT_KEYWORD.getAttribute("value"), "selenium");
}
}
BaiDuPageTest.java
package test;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import page.BaiDuPage;
public class BaiDuPageTest {
@Test
public void searchTest() {
BaiDuPage baidupage = new BaiDuPage();
baidupage.openUrl();
baidupage.searchByKeyword("selenium").checkKeyword();
baidupage.close();
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
}
}