Appium swipe之屏幕上、下、左、右滑动

在appium 的appium\webdriver\extensions\action_helpers.py下提供了一个可以上下左右滑动的方法:swipe()

这个方法用起来到也比较简单,首先获取屏幕的宽度、高度,然后计算出滑动的开始位置到结束位置的距离,

再把参数传递给swipe()调用即可:

不过要先弄清楚手机屏幕的起始坐标位置,要不然,滑动时就打不到效果了。

手机屏幕的起始坐标都是在左上角,也就是说左上角开始位置x轴、y轴都是0。弄清楚这一点,接一下来就好做了。

如下图

具体实现代码如下:

#-*-encoding:utf-8-*-
from appium import webdriver 
from time import sleep

desired_caps = {
	"platformName":"Android",
	"platformVersion":"6.0",
	"deviceName":"PJQDU16715003110",
	# "appPackage":"com.tencent.mtt",
	# "appActivity":"com.tencent.mtt.MainActivity",
	# "appActivity":"com.android.chrome",
	# "appPackage":"com.tencent.mobileqq",
	# "appActivity":"com.tencent.mobileqq.activity.SplashActivity",
	"appPackage":"com.tencent.mm",
	"appActivity":".ui.LauncherUI",
	"automationName":"uiautomator2",
	"unicodeKeyboard":"True",
	"resetKeyboard":"True",
	"noReset":"True",
	"chromeOptions":{"androidProcess":"com.tencent.mm:tools"}
# PJQDU16715003110
}

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)


sleep(5)

#获取手机屏幕宽、高
x = driver.get_window_size()["width"]
y = driver.get_window_size()["height"]
# print x,y


def swipe_down(driver,start_y=0.25,stop_y=0.75,duration=3000):
	#按下手机屏幕,向下滑动
	#注意,向下滑时,x轴不变,要不然就变成了斜向下滑动了
	#@duration:持续时间
	x1 = int(x*0.5)
	y1 = int(y*start_y)
	x2 = int(x*0.5)
	y2 = int(y*stop_y)
	# print x1,y1,x2,y2
	driver.swipe(x1,y1,x2,y2,duration)


def swipe_up(driver,start_y=0.75,stop_y=0.25,duration=3000):
	#按下手机屏幕,向上滑动
	#注意,向上滑时,x轴不变,要不然就变成了斜向下滑动了
	#@duration:持续时间
	x1 = int(x*0.5)
	y1 = int(y*start_y)
	x2 = int(x*0.5)
	y2 = int(y*stop_y)
	# print x1,y1,x2,y2
	driver.swipe(x1,y1,x2,y2,duration)


def swipe_left(driver,star_x=0.75,stop_x=0.25,duration=3000):
	#按下手机屏幕,向左边滑动
	#注意,向左边滑时,y轴不变
	#@duration:持续时间
	x1 = int(x*star_x)
	y1 = int(y*0.5)
	x2 = int(x*stop_x)
	y2 = int(y*0.5)
	# print x1,y1,x2,y2
	driver.swipe(x1,y1,x2,y2,duration)


def swipe_right(driver,star_x=0.25,stop_x=0.75,duration=3000):
	#按下手机屏幕,向右边滑动
	#注意,向左边滑时,y轴不变
	#@duration:持续时间
	x1 = int(x*star_x)
	y1 = int(y*0.5)
	x2 = int(x*stop_x)
	y2 = int(y*0.5)
	# print x1,y1,x2,y2
	driver.swipe(x1,y1,x2,y2,duration)

  

 

posted @ 2019-05-12 10:20  西夏一品唐  阅读(1232)  评论(0编辑  收藏  举报