def swipe(self, start_x, start_y, end_x, end_y, duration=None):
"""Swipe from one point to another point, for an optional duration.
从一个点滑动到另一个点,duration是滑动时间
:Args:
- start_x - 开始滑动的x坐标
- start_y - 开始滑动的y坐标
- end_x - 结束点x坐标
- end_y - 结束点y坐标
- duration - 持续时间,单位毫秒,时间越短速度越快。默认为None可不填,一般设置500-1000毫秒比较合适
:Usage:
driver.swipe(100, 100, 100, 400)
1 #coding=utf-8
2 from appium import webdriver
3 import time
4 desired_caps = {
5 #android的apk
6 'platformName': 'Android',
7 #手机设备名称
8 'deviceName': '192.168.175.101:5555',
9 #android系统的版本号
10 'platformVersion': '5.1',
11 # 'deviceName': 'ceshi',
12 # 'platformVersion': '7.0',
13 # 'udid':'83f04b4b5338424e',
14
15
16 'app':r'C:\Users\bzART\Desktop\app-artmall-release.apk',
17 # apk包名
18 'appPackage': 'com.baozhenart.artmall',
19 # apk的launcherActivity
20 'appActivity': 'com.baozhenart.artmall.SplashActivity',
21 #默认neReset为false,不要在会话前重置应用状态,每次启动都跟第一次启动一样,变为true则不一样
22 'noReset':'False',
23 #下面两行代码是为了屏蔽软件盘以至于可以输入中文
24 'unicodeKeyboard':True,#使用unicode编码方式发送字符串
25 'resetKeyboard':True#将键盘隐藏起来
26 }
27 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
28 driver.implicitly_wait(10)
29 time.sleep(5)
30 #获取屏幕尺寸,由于手机屏幕分辨率不一样,同一个元素在不同手机上的坐标不一样,滑动坐标不能写死。可以获取屏幕宽和高,再通过比例计算
31 def get_size():
32 x=driver.get_window_size()['width']
33 y=driver.get_window_size()['height']
34 return x,y
35 #显示屏幕尺寸(width,height)
36 l=get_size()
37 print(l)
38
39 #向左滑动屏幕,n滑动次数,t滑动持续时间
40 def swipeLeft(driver,n,t=500):
41 l=get_size()
42 x1=l[0]*0.9 #起始x坐标
43 y1=l[1]*0.5 #y坐标
44 x2=l[0]*0.1 #终点x坐标
45 #向左滑动n次
46 for i in range(n):
47 driver.swipe(x1,y1,x2,y1,t)
48 #向右滑动屏幕
49 def swipeRight(driver,n,t=500):
50 l=get_size()
51 y1=l[1]*0.5
52 x1=l[0]*0.25
53 x2=l[0]*0.95
54 for i in range(n):
55 driver.swipe(x1,y1,x2,y1,t)
56 #向上滑动屏幕
57 def swipeUp(driver,n,t=500):
58 l=get_size()
59 x1=l[0]*0.5
60 y1=l[1]*0.95
61 y2=l[1]*0.35
62 for i in range(n):
63 driver.swipe(x1,y1,x1,y2,t)
64 #向下滑动屏幕
65 def swipeDown(driver,n,t=500):
66 l = get_size()
67 x1 = l[0] * 0.5
68 y1 = l[1] * 0.35
69 y2 = l[1] * 0.85
70 for i in range(n):
71 driver.swipe(x1, y1, x1, y2, t)
72 if __name__=="__main__":
73 swipeLeft(driver,2)
74 time.sleep(2)
75 swipeRight(driver,1)