Page-Object思想
为什么要使用page-object
集中管理元素对象
集中管理一个page内的公共方法
后期维护方便
集中管理元素对象
实现方法:

调用方法:
WebElement element = driver.findElement(Test7.input);
Page类的实现
目的:
有了page类后,在具体的脚本中,要用到哪个page,就new这个page的对象,然后调用里面的公共方法即可
实现方法:

Page类的实现
说明:
Page类是一个基础类,其它的Page类都要继承该类,比如:
在具体的脚本中的使用方法:

具体代码如下:
Page.java具体代码:
package com.selenium.utils; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait; public class Page {
protected WebDriver driver; public Page(WebDriver driver) {
this.driver = driver;
} private boolean waitForToDisplayed(final By key) {
boolean waitDisplayed = new WebDriverWait(driver, 10)
.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(key).isDisplayed();
}
});
return waitDisplayed;
} protected WebElement getElement(WebDriver driver, By key) {
WebElement element = null;
if (this.waitForToDisplayed(key)) {
element = driver.findElement(key);
}
return element;
} }
DemoPage.java:
package com.selenium.utils; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class DemoPage extends Page { public static By input = By.id("user");
public static By link = By.xpath("//div[id='link']/a");
public static By select = By.cssSelector("select[name='select']");
public static By radio = By.name("identity");
public static By check = By.xpath("//div[@id='checkbox']/input");
public static By button = By.className("button");
public static By alert = By.className("alert");
public static By action = By.className("over");
public static By upload = By.id("load");
public static String iframe = "aa";
public static By multiWin = By.className("open");
public static By wait = By.className("wait"); public DemoPage(WebDriver driver) {
super(driver);
// TODO Auto-generated constructor stub
} public void input(WebDriver Driver, String message) {
WebElement element = this.getElement(Driver, input);
element.sendKeys(message);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void input_clear(WebDriver Driver) {
WebElement element = this.getElement(Driver, input);
element.clear();
}
}
IframePage.java:
package com.selenium.utils; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class IframePage extends Page { public static By input = By.id("user");
public static By link = By.xpath("//div[id='link']/a");
public static By select = By.cssSelector("select[name='select']");
public static By radio = By.name("identity");
public static By check = By.xpath("//div[@id='checkbox']/input");
public static By button = By.className("button");
public static By alert = By.className("alert");
public static By action = By.className("over");
public static By upload = By.id("load");
public static By iframe = By.xpath("//iframe[@name='aa']");
public static By multiWin = By.className("open");
public static By wait = By.className("wait"); public IframePage(WebDriver driver) {
super(driver);
// TODO Auto-generated constructor stub
} public void input(WebDriver Driver, String message) {
WebElement element = this.getElement(Driver, input);
element.sendKeys(message);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void input_clear(WebDriver Driver) {
WebElement element = this.getElement(Driver, input);
element.clear();
} public void switchToIframe(WebDriver Driver) {
WebElement element = this.getElement(Driver, iframe);
Driver.switchTo().frame(element);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void swipeOutFromIframe(WebDriver Driver) {
Driver.switchTo().defaultContent();
}
}
具体用起来是这样的:
TestPageObject.java
package com.selenium.test; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import com.selenium.utils.DemoPage;
import com.selenium.utils.IframePage; public class TestPageObject {
private WebDriver driver; @BeforeClass
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("file:///D:/BaiduYunDownload/selenium2/demo.html");
} @AfterClass
public void tearDown() {
driver.close();
// driver.quit();
} @Test
public void testInput() {
DemoPage dp = new DemoPage(driver);
dp.input(driver, "i love you");
dp.input_clear(driver); IframePage ifp = new IframePage(driver);
ifp.switchToIframe(driver);
ifp.input(driver, "iframe");
ifp.input_clear(driver);
ifp.swipeOutFromIframe(driver); dp.input(driver, "i love you again");
dp.input_clear(driver);
} }
最后打个广告,不要介意哦~
最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!
链接:http://www.dataguru.cn/invite.php?invitecode=G863
Page-Object思想的更多相关文章
- Selenium的PO模式(Page Object Model)[python版]
Page Object Model 简称POM 普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...
- 浅析selenium的page object模式
selenium目前比较流行的设计模式就是page object,那么到底什么是page object呢,简单来说,就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好 ...
- python+selenium自动化软件测试(第7章):Page Object模式
什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页 ...
- 这可能是最简单的Page Object库
做过web自动化测试的同学,对Page object设计模式应该不陌生. Page object库应该根据以下目标开发: Page object应该易于使用 清晰的结构 PageObjects 对于页 ...
- Python+Selenium框架设计--- Page Object Model
POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...
- Appium+Python之PO模型(Page object Model)
思考:我们进行自动化测试时,如果把代码都写在一个脚本中,代码的可读性会变差,且后期代码维护也麻烦,最好的想法就是测试对象和测试用例可以分离,可以很快定位问题,代码可读性高,也比较容易理解.这里推荐大家 ...
- Page Object设计模式(一)
一.简介 主要特点体现在“对界面交互细节的封装”上,使测试用例更专注于业务的操作,从而提高测试用例的可维护性.解决UI变动问题. page对象的一个基本原则经验法则是:凡是人能做的事,page对象通过 ...
- 初识Page Object
PageObject是UI自动化测试项目开发实践的最佳设计模式之一,它的主要特点体现在对界面交互细节的封装上,使测试用例更加专注于业务的操作,从而提高测试用例的可维护性. 1.认识Page Objec ...
- 使用page object模式抓取几个主要城市的pm2.5并从小到大排序后写入txt文档
#coding=utf-8from time import sleepimport unittestfrom selenium import webdriverfrom selenium.webdri ...
随机推荐
- Jquery会死吗?我为什么不用vue写富文本!
一.事件背景: 我最近开源了一个个人耗时半年打造的富文本及一套适用于web后台的ui框架,在gitee上受到网友们的关注,部分网友对我采用jquery的技术栈提出了质疑.总结起来:无非是jquery已 ...
- where whereis locate find 的用法
1.where :where ifconfig.用来搜索命令,显示命令是否存在以及路径在哪 2.whereis:whereis vim .用来搜索程序名,而且只搜索二进制文件(参数-b).man说明文 ...
- Generator 和 函数异步应用 笔记
Generator > ES6 提供的一种异步编程解决方案 > Generator 函数是一个状态机,封装了多个内部状态.还是一个遍历器对象生成函数.返回<label>遍历器对 ...
- windows下安装pm2
安装pm2 npm install pm2 -g 添加系统环境变量 PM2_HOME=C:\Users\PCONE\.pm2 打开新的cmd命令行窗口,执行以下命令来安装服务 pm2-service- ...
- MVC4学习之官方教程中迁移版本库报错
因工作需要,学习MVC4,但是微软官方教程中迁移版本库步骤在本地测试报错 官方教程地址:http://www.asp.net/mvc/overview/older-versions/getting-s ...
- java面试题(杨晓峰)---第四讲强引用、软引用、弱引用、幻想引用有什么区别?
在java语言中,除了原始数据类型的变量,其他所有都是所谓的引用类型,指向各种不同的对象,理解引用对于掌握java对象生命周期和JVM内部相关机制非常有帮助. 今天问题:强引用.软引用.弱引用.幻想引 ...
- coredata 关系的删除规则
http://blog.csdn.net/Hello_Hwc/article/details/46375517 关系的删除规则-Delete Rule Deny 关系的destination中只要有一 ...
- Mac OS X El Capitan系统完整性保护System Integrity Protection (SIP)
http://blog.csdn.net/yulimin/article/details/49992031 引言:前段时间经历了XCode编译器代码被注入的事件后,这次 Mac OS X El Cap ...
- 安装JDK1.8以及配置环境变量的步骤
一. 首先到官网下载jdk1.8,下载的版本分为windows和linux,这里需要安装操作系统进行下载.我的是64位就下载x64,32位系统则下载x86 二. 然后就是安装,双击进行安装,这里不用更 ...
- DB2中创建表
CONNECT TO TEST; CREATE TABLE DB2ADMIN.PERSON ( ID BIGINT NOT NULL , NAME BIGINT , FLAG BIGINT , ADD ...
