APP自动化针对PO模式进行二次封装之basepage
APP自动化跟WEB自动化所使用的框架基本一样,都是采用的PO模式结合pytest框架编写自动化测试脚本,为了提高代码的复用性、稳定性和易维护性,我们针对PO模式进行了二次封装,将日志,等待以及异常截图加入到其中,app相比较web而言,没有鼠标 、下拉框选择、frame切换、窗口切换和上传等操作,但它有属于自己的滑屏、toast获取、上下文切换、应用切换等操作。
import time
from datetime import datetime
import allure
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from Common.handle_logger import case_logger
from Common.constants import OUTPUTS_DIR class BasePage:
'''
BasePage类,针对PageObjects类的二次封装
''' def __init__(self, driver: WebDriver):
self.driver = driver def wait_element_to_be_visible(self, loc, img_doc, timeout=20, frequency=0.5):
'''
等待元素可见
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("开始等待页面元素<{}>是否可见!".format(loc))
start_time = time.time()
WebDriverWait(self.driver, timeout, frequency).until(EC.visibility_of_element_located(loc))
except Exception as e:
case_logger.error("页面元素<{}>等待可见失败!".format(loc))
self.save_screenshot(img_doc)
raise e
else:
end_time = time.time()
case_logger.info("页面元素<{}>等待可见,等待时间:{}秒".format(loc, round(end_time - start_time, 2))) def wait_element_to_be_click(self, loc, img_doc, timeout=20, frequency=0.5):
'''
等待元素可点击
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("开始等待页面元素<{}>是否可点击!".format(loc))
start_time = time.time()
WebDriverWait(self.driver, timeout, frequency).until(EC.element_to_be_clickable(loc))
except Exception as e:
case_logger.error("页面元素<{}>等待可点击失败!".format(loc))
self.save_screenshot(img_doc)
raise e
else:
end_time = time.time()
case_logger.info("页面元素<{}>等待可点击,等待时间:{}秒".format(loc, round(end_time - start_time, 2))) def wait_element_to_be_exist(self, loc, img_doc, timeout=20, frequency=0.5):
'''
等待元素存在
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("开始等待页面元素<{}>是否存在!".format(loc))
start_time = time.time()
WebDriverWait(self.driver, timeout, frequency).until(EC.presence_of_element_located(loc))
except Exception as e:
case_logger.error("页面元素<{}>等待存在失败!".format(loc))
self.save_screenshot(img_doc)
raise e
else:
end_time = time.time()
case_logger.info("页面元素<{}>等待存在,等待时间:{}秒".format(loc, round(end_time - start_time, 2))) def save_screenshot(self, img_doc):
'''
页面截屏保存截图
:param img_doc: 截图说明
:return:
'''
file_name = OUTPUTS_DIR + "\\{}_{}.png".format(datetime.strftime(datetime.now(), "%Y%m%d%H%M%S"), img_doc)
self.driver.save_screenshot(file_name)
with open(file_name, mode='rb') as f:
file = f.read()
allure.attach(file, img_doc, allure.attachment_type.PNG)
case_logger.info("页面截图文件保存在:{}".format(file_name)) def get_element(self, loc, img_doc):
'''
获取页面中的元素
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:return: WebElement对象
'''
case_logger.info("在{}中查找元素<{}>".format(img_doc, loc))
try:
ele = self.driver.find_element(*loc)
except Exception as e:
case_logger.error("在{}中查找元素<{}>失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e
else:
return ele def get_elements(self, loc, img_doc):
'''
获取页面中的所有元素
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:return: WebElement对象
'''
case_logger.info("在{}中查找所有元素<{}>".format(img_doc, loc))
try:
ele = self.driver.find_elements(*loc)
except Exception as e:
case_logger.error("在{}中查找所有元素<{}>失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e
else:
return ele def input_text(self, text, loc, img_doc, timeout=20, frequency=0.5):
'''
对输入框输入文本内容
:param text: 输入的文本内容
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("在{}中输入元素<{}>的内容为{}".format(img_doc, loc, text))
self.wait_element_to_be_visible(loc, img_doc, timeout, frequency)
self.get_element(loc, img_doc).send_keys(text)
except Exception as e:
case_logger.error("在元素<{}>中输入内容{}失败!".format(loc, text))
self.save_screenshot(img_doc)
raise e def clear_text(self, loc, img_doc, timeout=20, frequency=0.5):
'''
清除文本框的内容
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("在{}中清除元素<{}>的文本内容".format(img_doc, loc))
self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
self.get_element(loc, img_doc).clear()
except Exception as e:
case_logger.error("在{}中清除元素<{}>的文本内容失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e def click_button(self, loc, img_doc, timeout=20, frequency=0.5):
'''
点击按钮
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("在{}中点击元素<{}>".format(img_doc, loc))
self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
self.get_element(loc, img_doc).click()
except Exception as e:
case_logger.error("在{}中点击元素<{}>失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e def get_element_text(self, loc, img_doc, timeout=20, frequency=0.5):
'''
获取WebElement对象的文本值
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return: WebElement对象的文本值
'''
try:
case_logger.info("在{}中获取元素<{}>的文本值".format(img_doc, loc))
self.wait_element_to_be_visible(loc, img_doc, timeout, frequency)
text = self.get_element(loc, img_doc).text
except Exception as e:
case_logger.error("在{}中获取元素<{}>的文本值失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e
else:
case_logger.info("获取到的元素文本值为:{}".format(text))
return text def get_elements_text(self, loc, img_doc, timeout=20, frequency=0.5):
'''
获取WebElement对象的所有文本值
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return: WebElement对象的文本值的列表
'''
try:
case_logger.info("在{}中获取元素<{}>的所有文本值".format(img_doc, loc))
self.wait_element_to_be_visible(loc, img_doc, timeout, frequency)
all_text = self.get_elements(loc, img_doc)
text_list = []
for one_text in all_text:
text_list.append(one_text.text)
except Exception as e:
case_logger.error("在{}中获取元素<{}>的所有文本值失败!".format(img_doc, loc))
self.save_screenshot(img_doc)
raise e
else:
case_logger.info("获取到的元素文本值列表为:{}".format(text_list))
return text_list def get_element_attr(self, attr_name, loc, img_doc, timeout=20, frequency=0.5):
'''
获取WebElement对象的属性值
:param attr_name: 属性名称
:param loc: 元素定位的XPATH元组表达式
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return: WebElement对象的属性值
'''
try:
case_logger.info("在{}中获取元素<{}>的属性{}的值".format(img_doc, loc, attr_name))
self.wait_element_to_be_exist(loc, img_doc, timeout, frequency)
value = self.get_element(loc, img_doc).get_attribute(attr_name)
except Exception as e:
case_logger.error("在{}中获取元素<{}>的属性{}的值失败!".format(img_doc, loc, attr_name))
self.save_screenshot(img_doc)
raise e
else:
case_logger.info("获取到的元素属性{}的值为{}".format(attr_name, value))
return value def sliding_screen(self, direction, img_doc):
'''
滑屏操作
:param direction: 滑屏方向:上-up;下-down;左-left;右-right
:param img_doc: 截图说明
:return:
'''
size = self.driver.get_window_size()
try:
case_logger.info("开始向{}方向滑动".format(direction))
if direction.lower() == 'up':
self.driver.swipe(start_x=size['width'] * 0.5,
start_y=size['height'] * 0.9,
end_x=size['width'] * 0.5,
end_y=size['height'] * 0.1,
duration=200)
elif direction.lower() == 'down':
self.driver.swipe(start_x=size['width'] * 0.5,
start_y=size['height'] * 0.1,
end_x=size['width'] * 0.5,
end_y=size['height'] * 0.9,
duration=200)
elif direction.lower() == 'left':
self.driver.swipe(start_x=size['width'] * 0.9,
start_y=size['height'] * 0.5,
end_x=size['width'] * 0.1,
end_y=size['height'] * 0.5,
duration=200)
elif direction.lower() == 'right':
self.driver.swipe(start_x=size['width'] * 0.1,
start_y=size['height'] * 0.5,
end_x=size['width'] * 0.9,
end_y=size['height'] * 0.5,
duration=200)
else:
case_logger.error("方向选择错误!")
except Exception as e:
case_logger.error("向{}方向滑动屏幕失败!".format(direction))
self.save_screenshot(img_doc)
raise e def get_toast_msg(self, partial_text, img_doc):
'''
获取toast文本信息
:param partial_text: 不完整文本
:param img_doc: 截图说明
:return: toast文本
'''
loc = (MobileBy.XPATH, '//*[contains(@text,"{}")]'.format(partial_text))
try:
WebDriverWait(self.driver, 10, 0.01).until(EC.presence_of_element_located(loc))
msg = self.driver.find_element(*loc).text
print("toast出现了!!!")
return msg
except Exception as e:
print("好可惜,toast没找到!!")
case_logger.error("获取toast文本失败!")
self.save_screenshot(img_doc)
raise e def switch_to_webview(self, loc, img_doc, timeout=20, frequency=0.5):
'''
切换到webview页面
:param loc: webview页面的元素
:param img_doc: 截图说明
:param timeout: 等待的超时时间
:param frequency: 轮询频率
:return:
'''
try:
case_logger.info("等待元素{}可见,并进行webview切换".format(loc))
start_time = time.time()
WebDriverWait(self.driver, timeout, frequency).until(EC.visibility_of_element_located(loc))
cons = self.driver.contexts
case_logger.info("开始切换到webview:{}".format(cons[-1]))
self.driver.switch_to.context(cons[-1])
except Exception as e:
case_logger.error("切换webview失败!")
self.save_screenshot(img_doc)
raise e
else:
end_time = time.time()
case_logger.info("切换到webview:{}成功,等待时间:{}秒".format(cons[-1], round(end_time - start_time, 2))) def switch_to_native_app(self, img_doc):
'''
切换到app原生页面
:param img_doc: 截图说明
:return:
'''
try:
case_logger.info("切换到app原生页面")
self.driver.switch_to.context('NATIVE_APP')
except Exception as e:
case_logger.error("切换到app原生页面失败!")
self.save_screenshot(img_doc)
raise e def application_switching(self, package_name, activity_name, img_doc):
'''
应用切换
:param package_name: 包名
:param activity_name: 欢迎页面名
:param img_doc: 截图说明
:return:
'''
try:
case_logger.info("切换应用到{}".format(package_name))
self.driver.start_activity(app_package=package_name, app_activity=activity_name)
except Exception as e:
case_logger.error("切换应用到{}失败!".format(package_name))
self.save_screenshot(img_doc)
raise e
APP自动化针对PO模式进行二次封装之basepage的更多相关文章
- web自动化针对PO模式进行二次封装之basepage
在PO模式当中,我们做到了页面对象与测试用例的分离,但在页面对象编写时,我们仍然还有优化的空间.页面对象有一些共同的基本操作,可以封装起来,并可以在基本操作当中加上日志和异常截图的处理.比如说我们在查 ...
- 自动化测试po模式是什么?自动化测试po分层如何实现?-附详细源码
一.什么是PO模式 全称:page object model 简称:POM/PO PO模式最核心的思想是分层,实现松耦合!实现脚本重复使用,实现脚本易维护性! 主要分三层: 1.基础层BaseP ...
- PO模式学习笔记
框架: 1.PO模式应用(测试对象和测试用例分离)(写到简历中) 2.引入ddt 3.深入分层:测试数据分离 4.遵循原则:测试用例的独立性 5.深入分层:元素定位分离 6.框架优化:提取basepa ...
- 【基于Puppeteer前端自动化框架】【二】PO模式,断言(如何更简便逻辑的写测试代码)
一.概要 前面介绍了Puppeteer+jest+TypeScript做UI自动化,但是这知识基础的,我们实现自动化要考虑的很多,比如PO模式,比如配置文件,比如断言等等.下面就来一一实现我是怎么用p ...
- 说说UI自动化中的PO模式
PO模式,全称PageObject模式,即页面对象模式.将页面定位与业务操作分离. po模式有以下几个优点: 1.易读性好 2.扩展性高 3.复用性强 4.维护性好 5.代码冗余率低 了解了po模式及 ...
- 【Selenium07篇】python+selenium实现Web自动化:PO模型,PageObject模式!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第七篇博 ...
- Python+Selenium+Unittest实现PO模式web自动化框架(1)
1.什么是PO模式? PO是Page Object的缩写 PO模式是自动化测试项目开发实践的最佳设计模式之一,讲页面定位和业务操作分开,也就是把对象的定位和测试脚本分开,从而提供可维护性. 主要有以下 ...
- selenium(12)-web UI自动化项目实战(PO模式,代码封装)
web UI自动化项目实战-项目 项目使用禅道,所以你需要搭建1个禅道,搭建禅道的方法和步骤见 https://www.cnblogs.com/xinhua19/p/13151296.html 搭建U ...
- App 自动化框架设计思路
最近在整理和学习Appium+Java 自动化框架,对APP自动化框架的部分设想参考了一些文章,先进行整理下: 框架的思路一: 思考引入:https://www.cnblogs.com/yunfeio ...
随机推荐
- 001_Java概述与环境搭建
Java由来: SUN公司开发,95年推出,96年推出JDK1.0版本 09年被Oracle(甲骨文)收购 詹姆斯·高斯林被称作“Java之父” JavaSE:Java Standard Editoi ...
- 修改CentOS7网卡名字
需求:CentOS7网卡名字不像之前CentOS6版本的eth0,eth1这样的名字了,而是enoxxx比较长,不太容易记忆,操作也不方便!这时候就需要改网卡名字了. 1 :查看你的CentOS7 ...
- Vue中实现聊天窗口overflow:auto自动滚动到底部,实现显示当前最新聊天消息
在做消息的项目,当有新消息的时候让新消息出现在最底部,此时的box用的是overflow:auto 注意:vue项目需要注意在dom结构渲染完再进行操作 <div class="mai ...
- C#中提示:当前上下文中不存在名称“ConfigurationManager”
场景 想要在程序中获取App.config中设置的内容. 想要通过 ConfigurationManager.AppSettings[key]; 来进行获取,已经添加 using System.Con ...
- css3 伪类实现右箭头→
css3 实现右箭头→ <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- MySQL数据库Group by分组之后再统计数目Count(*)与不分组直接统计数目的区别
简述问题“统计最新时刻处于某一状态的设备的数量” 1. 首先子查询结果,可以看到每个设备最新的状态信息 2.1 在子查询的基础上,对设备状态进行分组,进行统计每个状态的设备数量 2.1.1 可以看到处 ...
- leetcode菜鸡斗智斗勇系列(3)--- Jewels and Stones珠宝和钻石
1.原题: https://leetcode.com/problems/jewels-and-stones/ You're given strings J representing the types ...
- 现代“十二要素应用”与 Kubernetes
"十二要素应用"为开发SaaS应用提供了方法上的指导,而Docker能够提供打包依赖,解耦后端服务等特性,使得两者非常吻合.这篇文章介绍了Docker特性怎样满足了开发" ...
- 【1】基于OpenCV的DLL动态库隐式连接
1DLL的作用 DLL是一个包含可由多个程序同时使用的代码和数据的库.例如:在Windows操作系统中,Comdlg32 DLL执行与对话框有关的常见函数.因此,每个程序都可以使用该DLL中包含的功能 ...
- python网络爬虫之解析网页的BeautifulSoup(爬取电影图片)[三]
目录 前言 一.BeautifulSoup的基本语法 二.爬取网页图片 扩展学习 后记 前言 本章同样是解析一个网页的结构信息 在上章内容中(python网络爬虫之解析网页的正则表达式(爬取4k动漫图 ...