开发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的更多相关文章

  1. selenium 的页面对象模型Page Object

    页面对象模型page object model是selenium中的一种脚本设计模式,它能将页面元素封装起来,与业务操作分隔开, 在页面变化改变时,无需去修改业务逻辑代码,提高脚本维护的效率. 1.p ...

  2. Selenium3+python自动化014-自动化常用设计模式页面对象模型 (Page Object)

    一.概 念: PO(Page Object)设计模式是一种面向对象(页面对象)的设计模式,将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理. 二.优点可以使代码复用,降低维 ...

  3. Selenium2(java)页面对象模型(Page Object) 八

    在开发一个 Selenium WebDriver 测试,我们可以使用页面对象模型.这个模型可以使测 试脚本有更高的可维护性,减少了重复的代码,把页面抽象出来.对象模型也提供了一个注释,帮助缓存远程,避 ...

  4. 页面对象(Page Object)模式

    内容转载自 https://www.cnblogs.com/yytesting/p/6973474.html 页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可 ...

  5. 5.8 页面对象(Page Object)模式

    页面对象(Page Object)模式是目前自动化测试领域普遍使用的设计模式之一,此模式可以大大提高测试代码的复用率,提高测试脚本的编写效率和维护效率,是中级自动化测试工程师的必备技能之一. 1.页面 ...

  6. Python+Selenium使用Page Object实现页面自动化测试

    Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...

  7. Page Object页面设计模式核心要点

      Page Object,页面对象.一种设计模式,实施selenium的最佳实践,体现了web应用与页面显示之间的关系.为什么需要Page Object?测试代码维护的需要:减少代码的编码量,减少代 ...

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

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

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

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

随机推荐

  1. PHP+Mysql+jQuery找回密码

    通常所说的密码找回功能不是真的能把忘记的密码找回,因为我们的密码是加密保存的,一般开发者会在验证用户信息后通过程序生成一个新密码或者生成一个特定的链接并发送邮件到用户邮箱,用户从邮箱链接到网站的重置密 ...

  2. java实现按对象某个字段排序,排序字段和规则自定义

    @SuppressWarnings({ "unchecked", "rawtypes" }) private <T> void sort(List& ...

  3. HBuilder 做移动端app流程

    1.新建一个移动项目 2.编写代码 3.发行-发行为原生安装包,配置参数 选择icon 和引导页

  4. CSS3学习-用CSS制作立体导航栏

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Android Error:Could not run build action using Gradle installation

    错误内容: Error:Could not run build action using Gradle installation ‘D:\AndroidStudio\AS2.x\gradle\grad ...

  6. iOS 应用架构 (二)

    iOS 客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答 iOS 应用架构中的种种问题,本文是其中的第二篇,主要讲 View 层的组织和调用方案.上篇主要讲 View ...

  7. 微信成为HTML5技术流行的最大推手

    很多热点的事件都是厚积薄发,HTML5就是如此.此前iOS和Android系统已经放弃了Flash,这让HTML5有了一个天然的成长基础.而现在手机硬件的提升和HTML5本身的完善,使得基于HTML5 ...

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

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

  9. Spring boot 集成ActiveMQ(包含双向队列实现)

    集百家之长,成一家之言.  1. 下载ActiveMQ https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activ ...

  10. docker单主机网络

    当你安装Docker时,它会自动创建三个网络.你可以使用以下docker network ls命令列出这些网络: [root@localhost ~]# docker network ls NETWO ...