基于Appium,封装自己的常用方法
Appium算是老牌移动端App自动化测试工具了,在使用它的过程中,使用者经常会根据个人习惯,把较常用的方法封装在一起,方便调用。以下是我的封装,希望对你有启发。
from typing import Dict, NoReturn, Tuple, List, Union, Optional
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from appium.webdriver.webelement import WebElement as MobileWebElement
from selenium.webdriver.common.by import By
from loguru import logger
import time class AppDriver:
def __init__(self, command_executor: str, desired_caps: Optional[Dict]) -> NoReturn:
self.driver = webdriver.Remote(command_executor, desired_caps) def find_element(self, element: Tuple[str, Union[str, Dict]]) -> MobileWebElement:
"""
寻找元素
"""
by = element[0]
value = element[1]
try:
if self.is_element_exist(element):
if by == "id":
return self.driver.find_element(By.ID, value)
elif by == "name":
return self.driver.find_element(By.NAME, value)
elif by == "class":
return self.driver.find_element(By.CLASS_NAME, value)
elif by == "text":
return self.driver.find_element(By.LINK_TEXT, value)
elif by == "partial_text":
return self.driver.find_element(By.PARTIAL_LINK_TEXT, value)
elif by == "xpath":
return self.driver.find_element(By.XPATH, value)
elif by == "css":
return self.driver.find_element(By.CSS_SELECTOR, value)
elif by == "tag":
return self.driver.find_element(By.TAG_NAME, value)
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
except Exception as e:
logger.error(">>>>>>>> failed to find element: %s is %s. Error: %s" % (by, value, e)) def find_elements(self, element: Tuple[str, Union[str, Dict]]) -> Union[List[MobileWebElement], List]:
"""
寻找一组元素
"""
by = element[0]
value = element[1]
try:
if self.is_element_exist(element):
if by == "id":
return self.driver.find_elements(By.ID, value)
elif by == "name":
return self.driver.find_elements(By.NAME, value)
elif by == "class":
return self.driver.find_elements(By.CLASS_NAME, value)
elif by == "text":
return self.driver.find_elements(By.LINK_TEXT, value)
elif by == "partial_text":
return self.driver.find_elements(By.PARTIAL_LINK_TEXT, value)
elif by == "xpath":
return self.driver.find_elements(By.XPATH, value)
elif by == "css":
return self.driver.find_elements(By.CSS_SELECTOR, value)
elif by == "tag":
return self.driver.find_elements(By.TAG_NAME, value)
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
except Exception as e:
logger.error(">>>>>>>> failed to find elements: %s is %s. Error: %s" % (by, value, e)) def find_all_child_element_by_xpath(self, element: Tuple[str, Union[str, Dict]]) -> Union[List[MobileWebElement], List]:
"""
寻找元素的所有子元素
"""
by = element[0]
value = element[1]
try:
if self.is_element_exist(element):
if by == "xpath":
child_value = value + '/child::*'
return self.driver.find_elements(By.XPATH, child_value)
else:
raise NameError("Please enter the correct targeting elements 'xpath'.")
except Exception as e:
logger.error(">>>>>>>> failed to find elements: %s is %s. Error: %s" % (by, value, e)) def save_screenshot(self, picture_name: str) -> NoReturn:
"""
获取屏幕截图
"""
fmt = '%Y%m%d%H%M%S' # 定义时间显示格式
date = time.strftime(fmt, time.localtime(time.time())) # 把传入的元组按照格式,输出字符串
picture_name = "../Result/" + picture_name + "-" + date + ".jpg"
self.driver.get_screenshot_as_file(picture_name) def get_screen_size(self) -> Tuple[int, int]:
"""
获取手机屏幕大小
"""
x = self.driver.get_window_size()['width']
y = self.driver.get_window_size()['height']
return x, y def swipe_screen(self, direction: str, duration_ms: int = 800) -> NoReturn:
"""
屏幕向上滑动
"""
location = self.get_screen_size()
if direction.lower() == "up":
x = int(location[0] * 0.5)
start_y = int(location[1] * 0.75)
end_y = int(location[1] * 0.25)
self.driver.swipe(x, start_y, x, end_y, duration_ms)
elif direction.lower() == "down":
x = int(location[0] * 0.5)
start_y = int(location[1] * 0.25)
end_y = int(location[1] * 0.75)
self.driver.swipe(x, start_y, x, end_y, duration_ms)
elif direction.lower() == "left":
start_x = int(location[0] * 0.75)
y = int(location[1] * 0.5)
end_x = int(location[0] * 0.05)
self.driver.swipe(start_x, y, end_x, y, duration_ms)
elif direction.lower() == "right":
start_x = int(location[0] * 0.05)
y = int(location[1] * 0.5)
end_x = int(location[0] * 0.75)
self.driver.swipe(start_x, y, end_x, y, duration_ms)
else:
print("请输入正确的方向") def tap_screen(self, positions: List[Tuple[int, int]], duration: Optional[int] = None) -> NoReturn:
"""
用最多五个手指轻拍一个特定的地方,保持一定的时间
用法:tap_screen([(100, 20), (100, 60), (100, 100)], 500)
"""
self.driver.tap(positions, duration) def click(self, element: Tuple[str, Union[str, Dict]], found_index: int = -1) -> NoReturn:
"""
点击按钮
"""
if found_index == -1:
self.find_element(element).click()
else:
self.find_elements(element)[found_index].click() def send_keys(self, element: Tuple[str, Union[str, Dict]], value: str, clear_first: bool = False, click_first: bool = False, found_index: int = -1) -> NoReturn:
"""
键盘输入
"""
if found_index == -1:
if click_first:
self.find_element(element).click()
if clear_first:
self.find_element(element).clear()
self.find_element(element).send_keys(value)
else:
if click_first:
self.find_elements(element)[found_index].click()
if clear_first:
self.find_elements(element)[found_index].clear()
self.find_elements(element)[found_index].send_keys(value) def scroll_to_text(self, text) -> NoReturn:
"""
滚动到指定的text
"""
uiautomator_cmd = "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"%s\").instance(0))" % text
self.driver.find_element_by_android_uiautomator(uiautomator_cmd) def get_attribute(self, element: Tuple[str, Union[str, Dict]], attribute_name: str = 'text', found_index: int = -1) -> Optional[Union[str, Dict]]:
"""
获取元素属性
"""
if found_index == -1:
return self.find_element(element).get_attribute(attribute_name)
else:
return self.find_elements(element)[found_index].get_attribute(attribute_name) def is_element_exist(self, element: Tuple[str, Union[str, Dict]], wait_seconds: int = 10) -> bool:
"""
判断元素是否存在
"""
by = element[0]
value = element[1] try:
if by == "id":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.ID, value)))
elif by == "name":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.NAME, value)))
elif by == "class":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, value)))
elif by == "text":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, value)))
elif by == "partial_text":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, value)))
elif by == "xpath":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.XPATH, value)))
elif by == "css":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, value)))
elif by == "tag":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.TAG_NAME, value)))
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
except:
return False
return True def is_text_exist(self, text: str, wait_seconds: int = 10) -> bool:
"""
判断text是否于当前页面存在
"""
for i in range(wait_seconds):
if text in self.driver.page_source:
return True
time.sleep(1)
return False def quit(self) -> NoReturn:
"""
退出驱动
"""
self.driver.quit()
基于Appium,封装自己的常用方法的更多相关文章
- 基于APPIUM测试微信公众号的UI自动化测试框架(结合Allure2测试报告框架)
框架初衷 前两周组内的小伙伴跟我说她现在测试的微信公众号项目(保险)每次上新产品时测试起来很费时,存在大量的重复操作(点点点),手工测试每个产品可能需要半天到一天的时间,复杂的产品需要两天. 由于保险 ...
- 基于appium的app自动化测试框架
基于appium框架的app自动化测试 App自动化测试主要难点在于环境的搭建,appium完全是基于selenium进行的扩展,所以app测试框架也是基于web测试框架开发的 一.设备连接 (即构建 ...
- 基于highcharts封装的组件-demo&源码
前段时间做的项目中需要用到highcharts绘制各种图表,其实绘制图表本身代码很简单,但是由于需求很多,有大量的图形需要绘制,所以就不得不复制粘贴大量重复(默认配置等等)的代码,所以,后来抽空自己基 ...
- 零成本实现Android/iOS自动化测试:基于Appium和Test Perfect
https://item.taobao.com/item.htm?spm=a230r.1.14.14.42KJ3L&id=527677900735&ns=1&abbucket= ...
- 基于appium的移动端自动化测试,密码键盘无法识别问题
基于appium做自动化测试,APP密码键盘无法识别问题解决思路 这个问题的解决思路如下: 1.针对iOS无序键盘:首先,iOS的密码键盘是可识别的,但是,密码键盘一般是无序的.针对这个情况,思路是用 ...
- 基于jQuery封装的分页组件
前言: 由于项目需要实现分页效果,上jQuery插件库找了下,但是木有找到自己想要的效果,于是自己封装了个分页组件. 思路: 主要是初始化时基于原型建立的分页模板然后绑定动态事件并实现刷新DOM的分页 ...
- 基于AFN封装的带缓存的网络请求
给大家分享一个基于AFN封装的网络请求 git: https://github.com/zhouxihi/NVNetworking #带缓存机制的网络请求 各类请求有分带缓存 , 不带缓存, 可自定义 ...
- 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件
目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...
- 基于dispatch_after封装YXTimer
基于dispatch_after封装YXTimer 本人根据dispatch_after封装了一个定时器,支持block以及代理的方式来激活定时器,适用于对精度要求低,耗时短的地方,高端大气上档次,低 ...
- 基于epoll封装的事件回调miniserver
epoll技术前两节已经阐述过了,目前主要做一下封装,很多epoll的服务器都是采用事件回调方式处理, 其实并没有什么复杂的,我慢慢给大家阐述下原理. 在networking.h和networking ...
随机推荐
- SQL SERVER 按时间计算每天某值的平均值
在报表需求中,有针对求每天按时间分配数据的平均值,在经过查找后,找到一种方法,供参考. 1.新建视图 2.编写语句 SELECT TOP (100) PERCENT AVG(dbo.漕盈日运行.CO ...
- Apache ActiveMQ(cve-2015-5254)
影响版本 Apache ActiveMQ 5.13.0之前5.x版本中存在安全漏洞 复现 使用工具执行命令 工具地址 https://github.com/matthiaskaiser/jmet/re ...
- 从零开始学习JAVA(入门基础)
目录 博主从零开始学习JAVA(入门基础) 1.搭建JAVA开发环境 卸载JDK(未安装的请忽略) 安装JDK 2.编程语言中,何为编译型与解释型 编译型 解释型 3.第一个JAVA应用程序 4.JA ...
- 使用vue实现用户管理 添加及删除功能
简单的管理系统-增删改查 添加及删除功能 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- fastboot刷机小脚本
在Windows系统下,一般刷机命令是在cmd路径下执行如下命令: 1.adb reboot bootloader2.fastboot flash boot +boot路径3.fastboot fla ...
- 在Linearlayout中新增ScrollView支持滚动
https://blog.csdn.net/wenzhi20102321/article/details/53491176 1.一般只需要在布局中加个ScrollView即可 2.如果布局中包含lis ...
- 建立局域网内使用的CentOS7-OpenStack源
by 无若 1. 先建立局域网内使用的CentOS7源 这个参看 http://www.cnblogs.com/gleaners/p/5735472.html 2. 抓取所有OpenStack的包,文 ...
- VueApp 自动更新解决plus is not defined问题
一,今天用VueApp 做自动更新调用按照网上列子直接 Plus 打包编译后出现了plus is not defined 发现需要引用document.addEventListener("p ...
- SSRF详解
上一篇说了XSS的防御与绕过的思路,这次来谈一下SSRF的防御,绕过,利用及危害 0x01 前置知识梳理 前置知识涉及理解此漏洞的方方面面,所以这部分要说的内容比较多 SSRF(Server-Side ...
- 30 个极大提高开发效率超级实用的 VSCode 插件
Visual Studio Code 的插件对于在提升编程效率和加快工作速度非常重要.这里有 30 个最受欢迎的 VSCode 插件,它们将使你成为更高效的搬砖摸鱼大师.这些插件主要适用于前端开发人员 ...