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 ...
随机推荐
- MapReduce编程入门实例之WordCount:分别在Eclipse和Hadoop集群上运行
上一篇博文如何在Eclipse下搭建Hadoop开发环境,今天给大家介绍一下如何分别分别在Eclipse和Hadoop集群上运行我们的MapReduce程序! 1. 在Eclipse环境下运行MapR ...
- 2017年2月28日-----------乱码新手自学.net 之特性与验证
现在看asp.net MVC5自学已经到了第六章:数据注解与验证. 话得从以前看MVC music store(音乐商店项目)的源码说起, 最初看music store源码完全就是一脸懵逼,整个程序, ...
- CF1088D Ehab and another another xor problem
思路: 根据异或的性质一位一位来搞.参考了https://blog.lucien.ink/archives/362/ 实现: #include <bits/stdc++.h> using ...
- canvas、svg、canvas与svg的区别
一.canvas canvas 画布,位图 <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形 注意:不要在style中给canvas设置宽高,会有位移差 can ...
- Kendo 单页面应用(一)概述
Kendo 单页面应用(一)概述 Kendo 单页面应用(Single-Page Application,缩写为 SPA)定义了一组类用于简化 Web 应用(Rich Client)开发,最常见的单页 ...
- iOS 应用架构 (二)
iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第二篇,主要讲 View 层的组织和调用方案.上篇主要讲 View ...
- ios 各种锁的使用性能比较
iOS开发中常用的锁有如下几种 来比较一下遇到加锁的情况: 1. @synchronized 关键字加锁 2. NSLock 对象锁 3. NSCondition 4. NSConditionLoc ...
- Cordova for iOS
Cordova,对这个名字大家可能比较陌生,大家肯定听过 PhoneGap 这个名字,Cordova 就是 PhoneGap 被 Adobe 收购后所改的名字. Cordova 是一个可以让 JS 与 ...
- 【虚拟机-远程连接】Azure Linux 虚拟机常见导致无法远程的操作
对Azure虚拟机的一些操作可能会导致无法远程连接,本文罗列了以下导致不能远程连接的场景: 场景1 - 在虚拟机配置IP地址或MAC地址 场景2 - 错误地修改服务的配置文件 场景3 - 误设置防火墙 ...
- lastlog命令
lastlog——检查某特定用户上次登录的时间 命令所在路径:/usr/bin/lastlog 示例1: # lastlog 列出所有用户,并显示用户最后一次登录的时间等信息 示例2: # lastl ...
