appium滑动操作(向上滑动、向下滑动、向左滑动、向右滑动)
测试app:今日头条apk
测试设备:夜游神模拟器
代码如下:
先用x、y获取当前的width和height
def getSize(): #获取当前的width和height的x、y的值
x = driver.get_window_size()['width'] #width为x坐标
y = driver.get_window_size()['height'] #height为y坐标
return (x, y)
屏幕向上滑动
def swipeUp(t): #当前向上滑动swipeup
l = getSize()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.75)
y2 = int(l[1] * 0.25)
driver.swipe(x1, y1, x1, y2,500) #设置时间为500
swipeUp(9000) #向上滑动9000
屏幕向左滑动
def swipLeft(t): #当前向左进行滑动swipleft
l=getSize()
x1=int(l[0]*0.75)
y1=int(l[1]*0.5)
x2=int(l[0]*0.05)
driver.swipe(x1,y1,x2,y1,500)
swipLeft(3000) #向左滑行3000
屏幕向右滑动
def swipRight(t): #向右滑行swipright
l=getSize()
x1=int(l[0]*0.05)
y1=int(l[1]*0.5)
x2=int(l[0]*0.75)
driver.swipe(x1,y1,x2,y1,500)
swipRight(3000) #向右滑行3000,回到初始位置
屏幕向下滑动
def swipeDown(t): #向下滑动swipedown
l = getSize()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.25)
y2 = int(l[1] * 0.75)
driver.swipe(x1, y1, x1, y2,500)
swipeDown(10000) #向下滑动10000
测试今日头条向上、向下、向左、向右滑动操作完整代码
#coding=utf-8
from appium import webdriver
import time
desired_caps={
'platformName':'Android',
'deviceName':'127.0.0.1:62001', #模拟器名称
'platformVersion':'4.4.2', #安卓版本
'appPackage':'com.ss.android.article.news', #当前apk的包名
'appActivity':'com.ss.android.article.news.activity.SplashBadgeActivity' #当前apk的appActivity
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(10) def getSize(): #获取当前的width和height的x、y的值
x = driver.get_window_size()['width'] #width为x坐标
y = driver.get_window_size()['height'] #height为y坐标
return (x, y) def swipeUp(t): #当前向上滑动swipeup
l = getSize()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.75)
y2 = int(l[1] * 0.25)
driver.swipe(x1, y1, x1, y2,500) #设置时间为500
swipeUp(9000) #向上滑动9000 def swipLeft(t): #当前向左进行滑动swipleft
l=getSize()
x1=int(l[0]*0.75)
y1=int(l[1]*0.5)
x2=int(l[0]*0.05)
driver.swipe(x1,y1,x2,y1,500)
swipLeft(3000) #向左滑行3000 def swipeDown(t): #向下滑动swipedown
l = getSize()
x1 = int(l[0] * 0.5)
y1 = int(l[1] * 0.25)
y2 = int(l[1] * 0.75)
driver.swipe(x1, y1, x1, y2,500)
swipeDown(10000) #向下滑动10000 def swipRight(t): #向右滑行swipright
l=getSize()
x1=int(l[0]*0.05)
y1=int(l[1]*0.5)
x2=int(l[0]*0.75)
driver.swipe(x1,y1,x2,y1,500)
swipRight(3000) #向右滑行3000,回到初始位置
time.sleep(20)
driver.quit() #退出当前的app