selenium2 页面对象模型Page Object
开发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() { }
}
selenium2 页面对象模型Page Object的更多相关文章
- selenium 的页面对象模型Page Object
页面对象模型page object model是selenium中的一种脚本设计模式,它能将页面元素封装起来,与业务操作分隔开, 在页面变化改变时,无需去修改业务逻辑代码,提高脚本维护的效率. 1.p ...
- Selenium3+python自动化014-自动化常用设计模式页面对象模型 (Page Object)
一.概 念: PO(Page Object)设计模式是一种面向对象(页面对象)的设计模式,将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理. 二.优点可以使代码复用,降低维 ...
- Selenium2(java)页面对象模型(Page Object) 八
在开发一个 Selenium WebDriver 测试,我们可以使用页面对象模型.这个模型可以使测 试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来.对象模型也提供了一个注释,帮助缓存远程,避 ...
- 页面对象(Page Object)模式
内容转载自 https://www.cnblogs.com/yytesting/p/6973474.html 页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可 ...
- 5.8 页面对象(Page Object)模式
页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可以大大提高测试代码的复用率,提高测试脚本的编写效率和维护效率,是中级自动化测试工程师的必备技能之一. 1.页面 ...
- Python+Selenium使用Page Object实现页面自动化测试
Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...
- Page Object页面设计模式核心要点
Page Object,页面对象.一种设计模式,实施selenium的最佳实践,体现了web应用与页面显示之间的关系.为什么需要Page Object?测试代码维护的需要:减少代码的编码量,减少代 ...
- python+selenium自动化软件测试(第7章):Page Object模式
什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页 ...
- Python+Selenium框架设计--- Page Object Model
POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...
随机推荐
- centOS 下开启端口号
firewall-cmd --zone=public --add-port=80/tcp --permanent permanent参数表示永久生效 更新防火墙规则 firewall-cmd --r ...
- 《移动Web前端高效开发实战》笔记1——静态布局在移动端上的自适应
1.整体缩放 整体缩放可以用在营销活动页,营销活动可能因为设计美观需求必须使用背景图片而非背景色,因此需要考虑背景图适应屏幕大小.开发者可以用320像素的宽度作为基础宽度(高度可以固定),然后通过计算 ...
- 浅谈BFC与高度塌陷
这个概念我大概是去年时候接触到的吧,略略记录了一下,没有深入研究,恰逢最近秋招,在这里写一写,顺便加深自己的印象. 什么是BFC? 页面中的元素都隐含一个属性Block Formatting Cont ...
- JFinal免费公开课更新中
价值千元的课程,免费报名学习,JFinal学院-小木 录制JFinal视频教程,JFinal核心已经周边涉及到微信小程序开发.数据库.前端实战等.
- vijos 1320 清点人数
背景 NK中学组织同学们去五云山寨参加社会实践活动,按惯例要乘坐火车去.由于NK中学的学生很多,在火车开之前必须清点好人数. 描述 初始时,火车上没有学生:当同学们开始上火车时,年级主任从第一节车厢出 ...
- Firefox火狐广告过滤插件Adblock Plus过滤规则包[中文维护小组]
如果你经常使用Firefox火狐浏览器那么一定知道Adblock Plus这款广告过滤插件,功能非常强大,但是Adblock Plus广告过滤插件自带的过滤规则并不多,而且也不太适合我们中国的网站,在 ...
- DIV在另一个DIV里面垂直居中,水平居中
HTML: <div class="parent"> <div class="children"> <div class=&quo ...
- 第三章 DOM的基本
节点分为不同的类型:元素节点.属性节点和文本节点 getElementById()方法 这个方法将返回一个与那个有着给定id属性值的元素节点相对应的对象.注意大小写.该方法只有一个参数.这个参数也就是 ...
- CMDB 数据加密 最终整合API验证+AES数据加密
当CMDB运行在内网的时候,经过API验证的三关是没有问题的,但是如果运行在外网,有一个问题是,黑客截取后的访问速度比客户端快的时候还会造成数据泄露.为了解决这个问题,就要对数据进行加密 RSA加密 ...
- MySql查询时间段的方法
本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面为您介绍两种MySql查询时间段的方法,供大家参考. MySql的时间字段有 ...