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变 ...
随机推荐
- 图像分类丨Inception家族进化史「GoogleNet、Inception、Xception」
引言 Google提出的Inception系列是分类任务中的代表性工作,不同于VGG简单地堆叠卷积层,Inception重视网络的拓扑结构.本文关注Inception系列方法的演变,并加入了Xcept ...
- asp.net重启web应用程序域
我把加载到static静态变量中了,是在数据库中存的,这样每次改了一下必须要重启一下web应用程序,每次去iis操作太麻烦了,于是找的了这个重启的办法,一句话代码: System.Web.HttpRu ...
- BroadCast广播机制应用与实例
如何编写广播接收器 第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法. class MyBroadcastReceiver extends Broadcast ...
- VirtualBox中出现UUID have already exists ,并且数字键盘numlock效果相反
原文地址:https://www.cnblogs.com/xqzt/p/5053338.html 原因:由于linux密码登录错误,修改也报错误,所以只能重新安装虚拟机并在其中安装镜像文件,但是安装镜 ...
- rest_framework组件之认证,权限,访问频率
共用的models from django.db import models # Create your models here. class User(models.Model): username ...
- vue使用uglifyjs-webpack-plugin后打包报错
楼主最新对已做项目进行打包优化,配置了打包环境下去除console.log语句插件---使用uglifyjs-webpack-plugin具体代码如下 npm install uglifyjs-web ...
- class类型重定义,防止头文件重复加载
今天调用自己写的一个类,出现了class类型重定义问题,上网查了相关资料,发现是头文件重复include引起的问题. 防止头文件重复加载: 系统那些头文件,无论怎么include都没事,因为一般都用了 ...
- 初识RatingBar
RatingBar,SeekBar和ProgressBar的子类 <RatingBar android:id="@+id/ratingBar2" android:layout ...
- tomcat的work目录不是缓存
最近发现,很多网友喜欢把tomcat的work目录里的东西叫做缓存,其实那不是很恰当,work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作目录,这也正是为什么它 ...
- 检查EXE、DLL、SYS等文件是32位还是64位的
非.NET文件用:dumpbin.exe /headers file.exe(C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin) . ...