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变 ...
随机推荐
- 在MVC中使用dotless后台动态解析LESSCSS的学习笔记
通过学习LessCSS,我们知道,Less是需要通过编译才能生成 .css 文件,主要使用三种方式进行编译: 1)使用第三方编译工具,在项目发布前编译好放在项目中. 2)在浏览器端解析执行,需要引用 ...
- JAVA 框架之面向对象设计原则
面向对象设计原则: 单一职责原则 SRP : 一个类或者行为只做一件事 . 降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式 里氏替换原则 LSP : 所有引用基类 ...
- mongodb Limit操作
Limit() 方法 要限制 MongoDB 中的记录,需要使用 limit() 方法. limit() 方法接受一个数字型的参数,这是要显示的文档数. 语法: limit() 方法的基本语法如下 & ...
- RxJava四个基础接口
Publisher Subscriber Subscription Processor ----------------------------------- public interface Pub ...
- bootstrap中container和container-fluid的区别与用法
对bootstrap框架有一定了解的朋友都知道,一般页面布局中的开头会使用到container或container-fluid类,那么它们有什么区别呢?不急!下面为您讲解. 我们先来看看官方对这两个类 ...
- Ionic 2 中添加图表
有问题请加入马画藤群:181596813,也强烈欢迎各类建议和需求:Ionic 2 实例开发 今日更新新增章节——Ionic 2 中添加图表: Chart.js是一个在HTML5的<canvas ...
- 四道java语言练习基础题:
一.==符的使 首先看一段比较有意思的代码 Integer a = 1000,b=1000; Integer c = 100,d=100; public void mRun(final String ...
- ajax提交表单无法验证easyui的验证选项(比如required等)
在实际开发中,遇到ajax方式提交表单没法验证easyui的验证选项,这对实际用户体验造成了很大的困扰.当然,这也是理所当然的事情. 解决办法:使用jquery中ajax的beforeSend事件 ...
- Python+Selenium之摘取网页上全部邮箱
本文转载:http://blog.csdn.net/u011541946/article/details/68485981 练习场景:在某一个网页上有些字段是我们感兴趣的,我们希望摘取出来,进行其他操 ...
- java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
今天写SpringMvc时,遇到这样一个问题: java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config at org.sp ...