为什么要使用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思想的更多相关文章

  1. Selenium的PO模式(Page Object Model)[python版]

     Page Object Model 简称POM  普通的测试用例代码: .... #测试用例 def test_login_mail(self): driver = self.driver driv ...

  2. Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)

            研究Selenium + python 自动化测试有近两个月了,不能说非常熟练,起码对selenium自动化的执行有了深入的认识. 从最初无结构的代码,到类的使用,方法封装,从原始函数 ...

  3. 浅析selenium的page object模式

    selenium目前比较流行的设计模式就是page object,那么到底什么是page object呢,简单来说,就是把页面作为对象,在使用中传递页面对象,来使用页面对象中相应的成员或者方法,能更好 ...

  4. python+selenium自动化软件测试(第7章):Page Object模式

    什么是Page ObjectModel模式Page Objects是selenium的一种测试设计模式,主要将每个页面看作是一个class.class的内容主要包括属性和方法,属性不难理解,就是这个页 ...

  5. 这可能是最简单的Page Object库

    做过web自动化测试的同学,对Page object设计模式应该不陌生. Page object库应该根据以下目标开发: Page object应该易于使用 清晰的结构 PageObjects 对于页 ...

  6. Python+Selenium框架设计--- Page Object Model

    POM(Page Object Model):页面对象模型,POM是一种最近几年非常流行的自动化测试模型,或者思想,POM不是一个框架,就是一个解决问题的思想.采用POM的目的,是为了解决前端中UI变 ...

  7. Appium+Python之PO模型(Page object Model)

    思考:我们进行自动化测试时,如果把代码都写在一个脚本中,代码的可读性会变差,且后期代码维护也麻烦,最好的想法就是测试对象和测试用例可以分离,可以很快定位问题,代码可读性高,也比较容易理解.这里推荐大家 ...

  8. Page Object设计模式(一)

    一.简介 主要特点体现在“对界面交互细节的封装”上,使测试用例更专注于业务的操作,从而提高测试用例的可维护性.解决UI变动问题. page对象的一个基本原则经验法则是:凡是人能做的事,page对象通过 ...

  9. 初识Page Object

    PageObject是UI自动化测试项目开发实践的最佳设计模式之一,它的主要特点体现在对界面交互细节的封装上,使测试用例更加专注于业务的操作,从而提高测试用例的可维护性. 1.认识Page Objec ...

  10. 使用page object模式抓取几个主要城市的pm2.5并从小到大排序后写入txt文档

    #coding=utf-8from time import sleepimport unittestfrom selenium import webdriverfrom selenium.webdri ...

随机推荐

  1. 洛谷P3928 SAC E#1 - 一道简单题 Sequence2

    提交地址 题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强. ...

  2. VS连接Access数据库--连接字符串及执行查询语句的方法(增删改查,用户名查重,根据用户获取密码查询)

    ACCESS数据的连接及语句执行操作,不难,久不用会生疏,每次都要找资料,干脆自己整理下,记录下来,需要的时候,直接查看,提高效率.也供初学者参考 1.连接字符串 public static stri ...

  3. 老技术,UrlRewriter实现全站伪静态

    看人家做淘宝客很火,就做了个网站.seo的话当然需要全站伪静态了,问了下空间商不支持mvc,尼玛,好吧,isapi_rewrite支持吗?“额,不支持!” -_-! 额,好吧,搬出n年前的东西了:微软 ...

  4. 记录下这周的mysql调优工作

    这周一至周四基本都在做mysql的测试和调优工作,包括erlang端对mysql的写入测试,到今天为止暂且告一段落,下周先做下其他的开发.    测试环境    使用的测试环境是aliyun的杭州节点 ...

  5. 关于dopost和doget中文乱码问题

    1.doPost方法请求方式为Post 请求内容中包含请求体,因此解决方法较简单,只要改变请求体的编码即可,具体方法setCharacterEncoding("utf-8"); 2 ...

  6. SharePoint运行状况分析器有关磁盘空间不足的警告

    对于负责管理SharePoint内部部署安装的SharePoint管理员,SharePoint Health Analyzer是一款出色的工具.此功能不仅有助于解决服务器故障和服务失败的问题,还提供了 ...

  7. jsp之数据提交与获取(传统方法)

    package com.java.model; public class Student { private String name; private int age; public String g ...

  8. 【转】iOS开发-文件管理(一)

    iOS开发-文件管理(一) 一.iOS中的沙盒机制 iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.pli ...

  9. 百度site网址显示完整站点信息的分析

    去年赛花红就发现百度site本博客网址,仅出现找到相关结果数约多少个,数字为估算值,网站管理员如需了解更准确的索引量,请使用百度站长平台等字样.但赛花红又发现有的网站却显示着完整的站点信息,当时以为是 ...

  10. phpmyadmin设置不密码,不登录直接进入

    1.config.sample.inc.php改为config.inc.php 2.加入或更改代码: [php] $cfg['Servers'][$i]['auth_type'] = 'config' ...