一、airtest介绍
- 官网:https://airtest.doc.io.netease.com/
- Airtest API文档:https://airtest.readthedocs.io/en/latest/
二、poco介绍
Poco是一款基于UI控件搜索的自动化框架,与基于图像识别的Airtest不同的是,Poco可以使用类似 poco(“OK”).click() 的方式来获取并操作节点
-
导入poco
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
-
启动APP
def lautch_app(self,package,activity=None):
"""
启动app
:param package:
:param activity:
:return:
"""
try:
start_app(package,activity=None)
return True
except Exception as e:
print("启动失败:{}".format(e))
print("启动{}失败!!!".format(package))
return False
-
页面滑动相关方法
- 获取手机屏幕大小
def get_myWindow_size(self):
"""
获取设备的高度和宽度
:return:
"""
width, height = self.poco.get_screen_size()
return width, height
- 上滑屏幕
def mobile_page_up_or_down_swip(self,start_x=0.5,start_y=0.6,end_y=0.9):
'''
页面上下滑动
:return:
'''
size = self.get_myWindow_size()
print("size[0]={0},size[1]={1}".format(size[0],size[1]))
logger.info("size[0]={0},size[1]={1}".format(size[0],size[1]))
x1 = size[0] * start_x # size[0]取元组的第一个值,*0.5表示中间的点
y1 = size[1] * start_y # size[1]取元组的第二个值,*0.1表示距离底部近
y2 = size[1] * end_y
time.sleep(2)
swipe((x1*start_x, y1*start_y),vector=(x1*start_x,y2*end_y))
- 左滑屏幕
def mobile_page_left_or_right_swip(self,start_x= 0.5,start_y= 3/4,end_x= 1/6):
'''
页面左右滑动
:return:
'''
size = self.get_myWindow_size()
print("size[0]={0},size[1]={1}".format(size[0],size[1]))
x1 = int(size[0] * start_x) # size[0]取元组的第一个值,*0.5表示中间的点
y1 = int(size[1] * start_y) # size[1]取元组的第二个值,*0.1表示距离底部近
x2 = int(size[0] * end_x)
time.sleep(2)
swipe((x1 * start_x, y1 * start_y),(x2 * end_x, y1 * start_y))
- 元素点击
def click_element(self,method,context):
"""
点击元素
:param name:
:return:
"""
if method == "name":
self.poco(name=context).click()
elif method == "text":
self.poco(text=context).click()
- 输入文本
def input_element(self,method,context,value):
"""
输入
:param method:
:param context:
:return:
"""
if method == "name":
self.poco(name=context).set_text(value)
elif method == "text":
self.poco(text=context).set_text(value)
- 模拟键盘点击
def click_keyboard(self,name):
"""
模拟键盘点击
:param name:
:return:
"""
keyevent(name)
- 检测某元素是否存在,通过name和text
def find_element_by_method(self,method,context):
"""
通过method判断元素是否存在
:return:
"""
if method == "name":
return self.poco(name=context).exists()
elif method == "text":
return self.poco(text=context).exists()
else:
pass