selenium的PageObject设计模式
PageObject设计模式
1. Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架。
2. web测试时,建议强烈推荐使用_谷歌或_火狐浏览器。
3. PageObject设计模式:是将某个页面的所有"元素(包含控件)属性"及"元素操作"封装在1个类(Class)里面,以page为单位进行管理。
4. 目的: 测试代码与被测页面对象代码分离,后期如果有页面元素发生了更改,只需要修改相应页面对象的代码(即对应Class文件),而不需要修改测试代。
5. 尽量采用xpath方式来寻找页面元素,而不建议使用name,Link等方法; xpath是基于页面元素所处区域,一般不会发生变化,测试代码基本不受干扰。
6. 将页面元素属性信息与代码分离,即与被测对象代码分离,目的也是为了进一步降低后续因页面变化带来的维护成本。
一、公共类,操作浏览器相关的
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; public class TestBase {
protected WebDriver driver; @BeforeTest
public void setUp() throws Exception {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(, TimeUnit.SECONDS);
} @AfterTest
public void tearDown() throws Exception { driver.quit();
} }
二、登录页面存放的元素
import org.openqa.selenium.By;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; public class LoginPage {
private WebDriver driver; public LoginPage(WebDriver driver) {
this.driver = driver;
} /**
* 用户名
*
* @param driver
* @param userText
*/
public void setPhone(String phoneText) throws NotFoundException {
// driver.findElement(By.id("username")).clear();
// driver.findElement(By.id("username")).sendKeys(userText);
this.setText(driver.findElement(By.id("username")), phoneText);
} /**
* 密码
*
* @param driver
* @param userText
*/
public void setPass(String passText) throws NotFoundException {
// driver.findElement(By.id("username")).clear();
// driver.findElement(By.id("username")).sendKeys(userText);
this.setText(driver.findElement(By.id("password")), passText);
} /**
* 点击登录按钮
*
* @param driver
*/
public void clickButton() throws NotFoundException {
driver.findElement(By.id("butt")).click();
} private void setText(WebElement e,String text)
{
e.clear();
e.sendKeys(text);
} }
三、登录页面提供的方法
import org.openqa.selenium.WebDriver;
import com.page.LoginPage; public class LoginBuss {
private WebDriver driver; public LoginBuss(WebDriver driver) {
this.driver = driver;
}
/**
* 定义登陆业务
*
* @param driver
* @param usename
* @param password
* @return
* @throws InterruptedException
*/
public void login(String username, String password)
throws Exception {
driver.get("xxxxxxxxx");//打开测试网址 LoginPage login_page = new LoginPage(driver); login_page.setPhone(username);//输入用户名
login_page.setPass(password);// 输入密码
login_page.clickButton();//点击登录按钮
} }
四、测试用例
import java.lang.reflect.Method;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.bussiness.LoginBuss;
public class loginCase extends TestBase{ // 用户成功登陆
@Test
public void LoginSucc() throws Exception {
LoginBuss lb = new LoginBuss(driver);
lb.login("xxx", "123abc");// 调用登录的一个事务
Thread.sleep();
} }
selenium的PageObject设计模式的更多相关文章
- PageObject设计模式,在selenium自动化测试中的运用
PageObject设计模式1. Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 2. web测试时,建议 ...
- PageObject设计模式 在selenium 自动化测试里面的应用
PageObject设计模式1. Web自动化测试框架(WebTestFramework)是基于Selenium框架且采用PageObject设计模式进行二次开发形成的框架. 2. web测试时,建议 ...
- 聊聊UI自动化的PageObject设计模式
当我们开发UI自动化测试用例时,需要引用页面中的元素(数据)才能够进行点击(动作)并显示出页面内容.如果我们开发的用例是直接对HTML元素进行操作,则这样的用例无法"应对"页面中U ...
- Page-Object设计模式
自动化脚本初写之际一定是只求完成功能测试,页面by.id.by.name.by.xpath满篇飞.业务逻辑代码重复率也是越来越高.慢慢的写着写着开始重构,开始封装一些方法.代码量好一些的人会在代码开始 ...
- Selenium+java - PageFactory设计模式
前言 上一小节我们已经学习了Page Object设计模式,优势很明显,能更好的体现java的面向对象思想和封装特性.但同时也存在一些不足之处,那就是随着这种模式使用,随着元素定位获取,元素定位与页面 ...
- selenium Object Page 设计模式理解及实现!
Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...
- 基于Python Selenium Unittest PO设计模式详解
本文章会讲述以下几个内容: 1.什么是PO设计模式(Page Object Model) 2.为什么要使用PO设计模式 3.使用PO设计模式要点 4.PO设计模式实例 1.什么是PO设计模式 (Pag ...
- web端自动化——selenium Page Object设计模式
Page Object设计模式的优点如下: ① 减少代码的重复. ② 提高测试用例的可读性. ③ 提高测试用例的可维护性,特别是针对UI频繁变化的项目. 当为Web页面编写测试时,需 ...
- 我所理解的selenium之PO设计模式
下午,花了点时间来整理UI自动化设计,就把我所理解的PO设计模式项目结构脑图整理如下,有不对的地方还望多多包涵.谢谢
随机推荐
- position:absolute和float隐式改变display为inline-block
不论之前是什么类型的元素(display:none除外), 只要设置了position:absolute或float, 都会让元素以display:inline-block的方式显示, 可以设置长宽, ...
- 【BZOJ 4151 The Cave】
Time Limit: 5 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 293 Solved: 144[Submit][Status][Di ...
- 《c程序设计语言》读书笔记-4.1-判断字符串在另一个字符串中的位置
#include <io.h> #include <stdio.h> #include <string.h> #include <stdlib.h> # ...
- 【转】beyond compare 启动提示“应用程序发生错误”
[转]beyond compare 启动提示“应用程序发生错误” 今天到公司BCompare不能打开,重新安装也不能打开.最后处理下,就解决了.方法是把C:\Documents and Setti ...
- Bzoj1407 Savage
Description Input 第1行为一个整数N(1<=N<=15),即 野人的数目.第2行到第N+1每行为三个整数Ci, Pi, Li (1<=Ci,Pi<=100, ...
- Hibernate中双向多对多的两种配置方式
Hibernate中双向多对多的两种配置方式 1.建立多对多双向关联关系 package cn.happy.entitys; import java.util.HashSet; import java ...
- lightgbm 学习笔记
首先是配置文件参数.参考自https://lightgbm.apachecn.org/#/docs/6 配置参数实在是太多了,大家还是去原文档查表吧orz 参数名 可选参数 作用 config= 自填 ...
- makefile函数集锦【转】
转自:http://blog.csdn.net/turkeyzhou/article/details/8612841 Makefile 常用函数表一.字符串处理函数1.$(subst FROM,TO ...
- UVA 1593: Alignment of Code(模拟 Grade D)
题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...
- BZOJ1588 [HNOI2002]营业额统计 splay模板
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MB Submit: 16189 Solved: 6482 [Submit][S ...