def setup_function():
# 连接当前设备
# HUAWEI honor 10 分辨率:2280*1080
# OPPO R15 分辨率:2280*1080
# Vivo x21 分辨率:2280*1080
# onePlus 5T 分辨率:2160×1080 poco sevices启动不稳定
# xiaomi mix2 元素无法识别
# onePlus pocoserver无法启动
device = Android()
#获取设备号
currentDevice = device.get_default_device()
print("现在连接的测试设备:", currentDevice)
auto_setup(__file__, logdir=True, devices=[
"android://127.0.0.1:5037/{currentDevice}?cap_method=MINICAP_STREAM&&ori_method=MINICAPORI&&touch_method=MINITOUCH".format(
currentDevice=currentDevice),
])
stop_app('chuxin.shimo.shimowendang')
start_app('chuxin.shimo.shimowendang')
def Android_device(self):
''' 连接小米等安卓设备,需要参数cap_method=javacap '''
device = Android()
# 获取设备号
currentDevice = device.get_default_device()
print("现在连接的测试设备:", currentDevice)
connect_device("android://127.0.0.1:5037/{currentDevice}?cap_method=javacap".format(
currentDevice=currentDevice))
# 获取设备的高度和宽度
width, height = device().get_current_resolution()
# 校准滑动的起点和终点
start_pt = (width * 0.9, height / 2)
end_pt = (width * 0.1, height / 2)
# 滑动5次:
for i in range(5):
swipe(start_pt, end_pt)
sleep(1) # 等待设备的响应
w,h=device().get_current_resolution()#获取手机分辨率
touch([0.5*w, 0.5*h])#点击手机中心位置
swipe((0.5*w,0.8*h),vector=(0,-0.5),duration=0.1) #在0.1s内上划0.5个屏幕
airtest 获取分辨率 绝对坐标 相对坐标
airtest 获取当前屏幕分辨率
width = G.DEVICE.display_info['width']
height = G.DEVICE.display_info['height']
print(width,height)
已知相对坐标 [0.12,0.709],转换成绝对坐标
x1 = 0.12*width
y1 = 0.709*height
touch([x1,y1])
已知绝对坐标[88.1060],转换成相对坐标
x2 = 88/width
y2 = 1060/height
poco.click([x2,y2])
滑动屏幕
进入搜索结果以后,需要查看下面的各种问题,此时就需要不断向上滑动屏幕。这里有一点需要特别注意,Airtest只能获取当前屏幕上的元素布局信息,不在屏幕上的内容是无法获取的。这一点和Selenium是不一样的。
滑动屏幕使用的命令为swipe
,滑动屏幕需要使用坐标信息。但这种坐标和屏幕分辨率无关。这里的坐标
定义为:(x, y),其中x为横坐标,y为纵坐标。屏幕左上角为(0, 0),屏幕右下角为(1, 1),从左向右,横坐标从0逐渐增大到1,从上到下,纵坐标从0逐渐增大到1。
现在我要把屏幕向上滑动,那么在真机上面,我是先按住屏幕下方,然后把屏幕向上滑动,所以代码可以这样写:
# poco.swipe(起点坐标,终点左边)
poco.swipe([0.5, 0.8], [0.5, 0.2])
在一般情况下:
- 向上滑动,只需要改动纵坐标,且起点值大于终点值
- 向下滑动,只需要改动纵坐标,且起点值小于终点值
- 向左滑动,只需要改动横坐标,且起点值大于终点值
- 向右滑动,只需要改动横坐标,且起点值小于终点值