一.配置连接
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)
原文地址:https://www.cnblogs.com/pythonywy/p/12627021.html
时间: 2024-10-02 07:31:31