优秀的框架都有属于自己的思想,在搭建web自动化测试框架时,我们通常都遵循 PO(Page Object)思想。

简单理解就是我们会把每个页面看成一个对象,一切皆对象,面向对象编码,这样会让我们更好的解耦代码,也更好的进行封装和理解。

使用selenium框架来操作页面时,最常用的都是一些点击,输入内容,页面切换等方法,如果每个页面我们都要写一遍这样的操作代码,重复性高,代码冗余。所以我们一般都会把这些共性的操作提取成一个基础类:BasePage。

以下是对该类的简单封装,还有其他的方法欢迎大家留言补充:

BasePage类:

import com.ggf.selenium.taxcp.utils.BrowserUtil;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; import java.util.Set; /**
* @Description: 页面基础类,将公共方法提取出来,如:获取元素内容,点击,等待。。。
* @Author: ggf
* @Date: 2021/05/15
*/
public class BasePage {
/**
* 对元素进行点击
* @param by 定位信息
*/
public void click(By by) {
waitElementClickable(by).click();
} /**
* 对元素输入内容
* @param by 定位信息
* @param datas 输入的内容
*/
public void sendKeys(By by, String datas) {
waitElementVisible(by).sendKeys(datas);
} /**
* 清除元素内容
* @param by
*/
public void clear(By by) {
waitElementVisible(by).clear();
} /**
* 判断元素是否存在。
* 使用isDisplayed() 方法,该方法判断某个元素是否在页面中存在,包含了:
* visibility=hidden 或 display=none 时,只要在HTML代码中存在,
* 就会返回true。
* 扩展:
* isEnable()-->用于判断input、select等元素是否可编辑。
* isSelected()-->判断元素是否被选中。
* @param by 定位信息
* @return 存在:true 不存在:false
*/
public boolean isElementExist(By by) {
return waitElementVisible(by).isDisplayed();
} /**
* 获取元素文本内容
* @param by 定位信息
* @return 返回文本内容
*/
public String getElementText(By by) {
return waitElementVisible(by).getText();
} /**
* 获取元素属性值
* @param by 定位信息
* @param attributeName 属性名称
* @return 返回属性值
*/
public String getElementAttributeValue(By by, String attributeName) {
return waitElementVisible(by).getAttribute(attributeName);
} /**
* 等待元素可见,显式等待10秒。
* @param by 定位信息
* @return 返回element对象
*/
public WebElement waitElementVisible(By by) {
WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(),10);
return webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by));
} /**
* 等待元素可点击,显式等待10秒。
* @param by 定位信息
* @return 返回element对象
*/
public WebElement waitElementClickable(By by) {
WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
return webDriverWait.until(ExpectedConditions.elementToBeClickable(by));
} /**
* 等待iframe可见并且根据By切换到iframe框架中
* @param by 元素定位信息
*/
public void waitIframeAndSwitch(By by) {
WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));
} /**
* 等待iframe可见并且根据名称切换。
* @param frameName iframe名称
*/
public void waitIframeAndSwitch(String frameName) {
WebDriverWait webDriverWait = new WebDriverWait(BrowserUtil.getDriver(), 10);
webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameName));
} /**
* 滑动列表找元素并且进行点击
* @param selectedText 选中元素文本
* @param by 正在加载类似元素的定位表达式
* @throws InterruptedException
*/
public static void clickElementInList(String selectedText, By by) throws InterruptedException {
// 滑动之前的页面源代码信息
String beforeSource = "";
// 滑动之后的页面源代码信息
String afterSource = "";
// 获取webdriver
WebDriver driver = BrowserUtil.getDriver(); // 循环条件
// 1、找到了元素,跳出循环
// 2、如果没有找到元素???怎么跳出循环
while (true) {
WebElement webElement = driver.findElement(by);
beforeSource = driver.getPageSource();
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].scrollIntoView(0);", webElement);
// 如果当前页面有想要找的元素,怎么判断是否有??--getPageSource
if (driver.getPageSource().contains(selectedText)) {
driver.findElement(By.linkText(selectedText)).click();
break;
}
Thread.sleep(1000);
afterSource = driver.getPageSource();
// 页面元素没有变化---滑动到了最底部
if (afterSource.equals(beforeSource)) {
break;
}
Thread.sleep(1500);
}
} /**
* 滚动到指定元素上的方法
* @param by 定位信息
*/
public void scrollIntoElement(By by){
WebElement webElement= waitElementVisible(by);
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) BrowserUtil.getDriver();
javascriptExecutor.executeScript("arguments[0].scrollIntoView(0);", webElement);
} /**
* 根据title切换窗口的方法
* @param title 窗口的标题
*/
public void switchWindowByTitle(String title){
WebDriver driver = BrowserUtil.getDriver();
Set<String> handles = driver.getWindowHandles();
// 切换窗口的方式--循环遍历handles集合
for (String handle : handles) {
//判断是哪一个页面的句柄??--根据什么来判断???title
if(driver.getTitle().equals(title)){
break;
}else{
//切换窗口--根据窗口标识来切换
driver.switchTo().window(handle);
} }
} /**
* 根据url内容切换窗口的方法
* @param urlFlag 窗口的标题
*/
public void switchWindowByUrl(String urlFlag){
WebDriver driver = BrowserUtil.getDriver();
Set<String> handles = driver.getWindowHandles(); for (String handle : handles) {
//根据url内容判断句柄
if(driver.getCurrentUrl().contains(urlFlag)){
break;
}else{
//切换窗口
driver.switchTo().window(handle);
} }
}
}

BrowserUtil类:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver; /**
* @Description: 浏览器工具类,控制浏览器的生命周期,浏览器启动,关闭
* @Author: ggf
* @Date: 2021/05/15
*/
public class BrowserUtil { /**
*为解决多线程问题,通过ThreadLocal机制来控制
*/
private static ThreadLocal<WebDriver> threadLocal = new ThreadLocal<WebDriver>(); /**
* 通过传入不同的浏览器名称,获取浏览器驱动
* @param driverType 不同的浏览器 chrome ie Firefox
*/
public static void setDriverType(String driverType) {
// 判断输入内容是否为空。
if (driverType != "" && driverType != null) {
// 创建不同的浏览器驱动
if ("chrome".equalsIgnoreCase(driverType)) {
ChromeDriver chromeDriver = new ChromeDriver();
setDriver(chromeDriver);
}else if ("ie".equalsIgnoreCase(driverType)) {
InternetExplorerDriver ieDriver = new InternetExplorerDriver();
setDriver(ieDriver);
}else if ("firefox".equalsIgnoreCase(driverType)) {
FirefoxDriver fireDriver = new FirefoxDriver();
setDriver(fireDriver);
}
}
} /**
* 从线程ThreadLocal中获取对象
* @return
*/
public static WebDriver getDriver() {
return threadLocal.get();
} /**
* 设置对象到ThreadLocal中
* @param driver
*/
public static void setDriver(WebDriver driver) {
threadLocal.set(driver);
} /**
* 关闭浏览器
*/
public void closeBrowser() {
getDriver().close();
}
}

web自动化框架—BasePage 类的简单封装的更多相关文章

  1. 基于Selenium的web自动化框架

    转自 : https://www.cnblogs.com/AlwinXu/p/5836709.html 1 什么是selenium Selenium 是一个基于浏览器的自动化工具,它提供了一种跨平台. ...

  2. 基于Selenium的Web自动化框架增强篇

    在写完上一篇“基于Selenium的Web自动化框架”(http://www.cnblogs.com/AlwinXu/p/5836709.html)之后一直没有时间重新审视该框架,正好趁着给同事分享的 ...

  3. 【转】基于Selenium的web自动化框架(python)

    1 什么是selenium Selenium 是一个基于浏览器的自动化工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.Sel ...

  4. Web自动化框架LazyUI使用手册(2)--先跑起来再说(第一个测试用例-百度搜索)

    作者:cryanimal QQ:164166060 上篇文章中,简要介绍了LazyUI框架,本文便来演示,如何从无到有快速搭建基于lazyUI的工程,并成功运行第一个测试用例. 本文以百度搜索为例,选 ...

  5. Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)

    概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...

  6. web自动化框架如何设计

    web自动化框架如何设计po模式总结: 1. 页面对象模型:当页面特别多的时候,代码更好的维护 2. Po是pageObject设计模式,用来管理和维护一组web元素的对象库 3. 每一个page c ...

  7. 关于Spring-JDBC测试类的简单封装

    关于Spring-JDBC测试类的简单封装 1.简单封装 /** * Created with IntelliJ IDEA. * * @Author: Suhai * @Date: 2022/04/0 ...

  8. Web自动化框架之五一套完整demo的点点滴滴(excel功能案例参数化+业务功能分层设计+mysql数据存储封装+截图+日志+测试报告+对接缺陷管理系统+自动编译部署环境+自动验证false、error案例)

    标题很大,想说的很多,不知道从那开始~~直接步入正题吧 个人也是由于公司的人员的现状和项目的特殊情况,今年年中后开始折腾web自动化这块:整这个原因很简单,就是想能让自己偷点懒.也让减轻一点同事的苦力 ...

  9. Web自动化框架LazyUI使用手册(1)--框架简介

    作者:cryanimal QQ:164166060 web端自动化简介 web端自动化,即通过自动化的方式,对Web页面施行一系列的仿鼠标键盘操作,以达到对Web页面的功能进行自动化测试的目的. 其一 ...

随机推荐

  1. 3、MyBatis教程之CURD操作

    4.CURD操作 1.查询 根据用户 Id查询用户 在UserMapper中添加对应方法 public interface UserMapper { List<User> getUserL ...

  2. 用jar命令打包war远程部署

    最近在看jboss的相关漏洞,用jmx-console进行war远程部署的时候碰到一个jsp转war的问题,研究了半天,记录一下免得搞忘了. 一开始网上是说的直接把jsp文件压缩成zip,再把后缀名改 ...

  3. 百度地图一套JS API,非常实用

    百度地图一套JS API,非常实用 import mapStyleJson from "./mapStyleJson"; import $ from "jquery&qu ...

  4. 【论文笔记】Learning Fashion Compatibility with Bidirectional LSTMs

    论文:<Learning Fashion Compatibility with Bidirectional LSTMs> 论文地址:https://arxiv.org/abs/1707.0 ...

  5. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  6. Hadoop完整搭建过程(一):本地模式

    1 本地模式 本地模式是最简单的模式,所有模块都运行在一个JVM进程中,使用本地文件系统而不是HDFS. 本地模式主要是用于本地开发过程中的运行调试用,下载后的Hadoop不需要设置默认就是本地模式. ...

  7. Idea使用指南--实用版

    idea使用指南--基础配置: 视频链接:https://www.bilibili.com/video/av21735428/?p=1 idea安装: 快捷方式create destop shortc ...

  8. shell脚本 4 函数与正则

    shell函数 shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直接调用该段代码. 格式 func() {   #指定函数名 ...

  9. kubernetes 的API 介绍

    在API conventions doc中描述了API的全部协议. 在API Reference文档中描述了API的端点.资源类型和示例. 在Controlling API Access doc中讨论 ...

  10. OOP-面向对象(三)

    魔术方法的使用 # 使用 __new__ 创建单态模式 class Singleton(): __obj = None def __new__(cls, *args, **kwargs): if cl ...