使用Airtest中swipe方法由於不同分辨率的手機上滑動的坐標位置不同,所以想要兼容所有的手機,僅僅靠固定坐標就會出現問題
想要兼容所有的手機,可以按照如下思路進行
1、首先獲取手機的分辨率,可以使用Airtest中的poco模塊的get_screen_size()方法
poco.get_screen_size()
此時獲取到了手機的分辨率,可以看出屏幕寬等於900,長度等於1600
2、將屏幕的寬度和長度分別賦值為x和y,注意屏幕左上角的坐標為(0,0),所以左下角的坐標為(0,1600),右上角的坐標為(900,0),右下角的坐標為(900,1600)
xy = poco.get_screen_size() x = xy[0] y = xy[1]
3、按照屏幕的比例進行滑動,我想要從屏幕的右側向左側滑動,就可以按照如下方法進行,從(0.9*x,0.5*y)的坐標移動到(0.1*x,0.5y)的坐標,也就是從(810,800)的坐標移動到(90,800)的坐標
swipe((0.9*x,0.5*y),(0.1*x,0.5*y),duration=1)
4、完整代碼運行
# -*- encoding=utf8 -*- __author__ = "fengruishou" from airtest.core.api import * from poco.drivers.android.uiautomation import AndroidUiautomationPoco poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False) auto_setup(__file__) print(poco.get_screen_size()) xy = poco.get_screen_size() x = xy[0] y = xy[1] swipe((0.9*x,0.5*y),(0.1*x,0.5*y),duration=1)