appium---App页面滑动

行是知之始,知是行之成。这篇文章主要讲述appium---App页面滑动相关的知识,希望能为你提供帮助。
我们操作app的过程中都会进行页面滑动,那么这个过程通过python怎么实现呢?
 
如何滑动大家都非常的清楚我们手动在app上是如何滑动的,然后自动化只是模仿了手工的方法去实现,我们通过一个图来分析
【appium---App页面滑动】

appium---App页面滑动

文章图片

 
 
从上往下滑动的时候,我们通常会按(X1,Y1)这个坐标,然后往下进行滑动,一直滑到(X2,Y2)这个坐标
从下往上滑动的时候,我们会按(X2,Y2)这个坐标,往上进行滑动到(X1,Y1)这个坐标
当然了左往右进行滑动和从右往左进行滑动的思路都是一样的了。
swipeswipe是appium模块中的一个方法,支持左右滑动和模拟点击的功能
def swipe(self, start_x, start_y, end_x, end_y, duration=None): # 从一个点滑动到另一个点,持续时间可选 """Swipe from one point to another point, for an optional duration.:Args: - start_x - x-coordinate at which to start开始的X轴 - start_y - y-coordinate at which to start开始的Y轴 - end_x - x-coordinate at which to stop结束的X轴 - end_y - y-coordinate at which to stop结束的Y轴 - duration - (optional) time to take the swipe, in ms.持续的时间,单位毫秒:Usage: driver.swipe(100, 100, 100, 400) """

我们从上面的图和swipe的源码中知道,滑动的最主要的参数就是坐标,那么我们如何通进行手机坐标查询呢?
查询坐标 打开指针位置我们可以进入到手机开发者模式当中,然后打开指针位置,这样点击哪里就可以看到哪里的坐标。
appium---App页面滑动

文章图片

  我们也可以通过python的脚本帮我们实现找到坐标的方法
get_window_size
def get_window_size(self, windowHandle=\'current\'): """ Gets the width and height of the current window. # 获取屏幕的长和宽 :Usage: driver.get_window_size() ""

  因为每个手机的坐标可能都不一样,这里我们可以通过先获取手机屏幕的长和宽,然后再次计算需要滑动的坐标位置
# coding:utf-8 from appium import webdriver import time desired_caps = { \'platformName\': \'android\',# 测试版本 \'deviceName\': \'emulator-5554\',# 设备名 \'platformVersion\': \'5.1.1\', # 系统版本 \'appPackage\': \'com.yipiao\', #apk的包名 \'appActivity\': \'.activity.LaunchActivity\', # apk的launcherActivity } driver = webdriver.Remote(\'http://127.0.0.1:4723/wd/hub\', desired_caps) # 获取设备的宽度 x=driver.get_window_size()[\'width\'] # 获取设备的长度 y=driver.get_window_size()[\'height\'] print(x) print(y)

App滑动我们通过智行火车票APP来实战操作下左右滑动,因为我们每个手机的页面不一样,可以通过算出来一个大概值进行匹配
向左滑动
# coding:utf-8 from appium import webdriver import time desired_caps = { \'platformName\': \'Android\',# 测试版本 \'deviceName\': \'emulator-5554\',# 设备名 \'platformVersion\': \'5.1.1\', # 系统版本 \'appPackage\': \'com.yipiao\', #apk的包名 \'appActivity\': \'.activity.LaunchActivity\', # apk的launcherActivity "noReset": True,# 不清空数据 } driver = webdriver.Remote(\'http://127.0.0.1:4723/wd/hub\', desired_caps) time.sleep(6) driver.find_element_by_xpath(\'//*[@text="下次再说"]\').click() l = driver.get_window_size() x1 = l[\'width\'] * 0.75 y1 = l[\'height\'] * 0.5 x2 = l[\'width\'] * 0.25 # 向左滑动 driver.swipe(x1,y1,x2,y1,500) print(\'已经向左滑动了\')

 
appium---App页面滑动

文章图片

在这里安静就不一个个为大家演示了,小伙伴们可以自己尝试下
封装滑动方法我们可以把滑动方法一个个封装起来,这样我们做app自动化测试的过程需要用到的时候就可以直接调用。
def swipeUp(driver, t=500, n=1): \'\'\'向上滑动屏幕\'\'\' l = driver.get_window_size() x1 = l[\'width\'] * 0.5 y1 = l[\'height\'] * 0.75 y2 = l[\'height\'] * 0.25 for i in range(n): driver.swipe(x1, y1, x1, y2, t)def swipeDown(driver, t=500, n=1): \'\'\'向下滑动屏幕\'\'\' l = driver.get_window_size() x1 = l[\'width\'] * 0.5 y1 = l[\'height\'] * 0.25 y2 = l[\'height\'] * 0.75 for i in range(n): driver.swipe(x1, y1, x1, y2,t)def swipLeft(driver, t=500, n=1): \'\'\'向左滑动屏幕\'\'\' l = driver.get_window_size() x1 = l[\'width\'] * 0.75 y1 = l[\'height\'] * 0.5 x2 = l[\'width\'] * 0.25 for i in range(n): driver.swipe(x1, y1, x2, y1, t)def swipRight(driver, t=500, n=1): \'\'\'向右滑动屏幕\'\'\' l = driver.get_window_size() x1 = l[\'width\'] * 0.25 y1 = l[\'height\'] * 0.5 x2 = l[\'width\'] * 0.75 for i in range(n): driver.swipe(x1, y1, x2, y1, t)

 
 
感觉写的对您有帮助的话,点个关注,持续更新~~国庆快乐~~放假了也不要忘记学习哦
 

    推荐阅读