appnium实现向上滑动,向下滑动,向左滑动,向右滑动
程序员文章站
2022-03-28 20:27:52
一.配置连接 二.向各个方向滑动 ......
一.配置连接
from appium import webdriver desired_caps = {} desired_caps['platformname'] = 'android' # 系统名 desired_caps['platformversion'] = '5.1.1' # adb版本 desired_caps['devicename'] = 'emulator-5554' # adb的名字 cmd输入adb devices可以查到 #导出模拟器中的apk,然后使用android kille将apk包进行反编译 desired_caps['apppackage'] = 'com.liulianp.android' # app的包名 desired_caps['appactivity'] = 'com.liulianp.android.module.welcome.splashactivity' # app的主入口名 desired_caps['unicodekeyboard'] = true desired_caps['resetkeyboard'] = true #启动appium-desktop服务器,服务器ip根据实际填写 ip = 'appnium的ip' prot = 'appnium的prot' driver = webdriver.remote(f'http://{id}:{prot}/wd/hub', desired_caps)
二.向各个方向滑动
#原理使用driver.swipe方法() swipe(self, start_x, start_y, end_x, end_y, duration=none) start_x:起始横坐标 start_y:起始纵坐标 end_x:结束横坐标 end_y:结束纵坐标 duration:起始位置到结束位置用时间单位ms #你可以把它想象成手指在app界面的一开始按着的位置到结束的位置
def get_size(driver): '''获取长宽''' x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] return x,y def swipeup(driver, t=500, n=1): '''向上滑动屏幕''' x,y=get_size(driver) x1 = x * 0.5 y1 = y * 0.2 y2 = y * 0.8 for i in range(n): driver.swipe(x1, y1, x1, y2, t) def swipedown(driver, t=500, n=1): '''向下滑动屏幕''' x,y=get_size(driver) x1 = x * 0.5 y1 = y * 0.2 y2 = y * 0.8 for i in range(n): driver.swipe(x1, y1, x1, y2,t) def swipleft(driver, t=500, n=1): '''向左滑动屏幕''' x,y=get_size(driver) x1 = x * 0.8 y1 = y * 0.5 x2 = x * 0.2 for i in range(n): driver.swipe(x1, y1, x2, y1, t) def swipright(driver, t=500, n=1): '''向右滑动屏幕''' x,y=get_size(driver) x1 = x * 0.2 y1 = y * 0.5 x2 = x * 0.8 for i in range(n): driver.swipe(x1, y1, x2, y1, t)