Appium_8_滑动/连续滑动/多点触碰

一,简单滑动方法

在Appium中模拟用户滑动操作需要使用swipe方法,该方法定义如下:

   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
         - start_y - y-coordinate at which to start
         - end_x - x-coordinate at which to stop
         - end_y - y-coordinate at which to stop
         - duration - (optional) time to take the swipe, in ms.

        :Usage:
            driver.swipe(100, 100, 100, 400)
        """

滑动解析

滑动主要分为:

  1. 水平滑动
  2. 垂直滑动
  3. 任意方向滑动

滑动轨迹图如下:

代码实现:

def get_size():
    x = driver.get_window_size()  #获取屏幕大小,返回dict
    return x
def swipe_slide(num1,num2,num3,num4,times):
    l = get_size()
    print(l)
    x_start = int(l.get('width')*num1)
    print(x_start)
    x_end = int(l.get('width')*num2)
    print(x_end)
    y_start = int(l.get('height')*num3)
    y_end = int(l.get('height')*num4)
    driver.swipe(x_start,y_start,x_end,y_end,times)    #滑动,前四个参数为坐标,第五个参数为持续时间(毫秒)

 

二,连续滑动操作

滑动操作一般是两点之间的滑动,而实际使用过程中用户可能要进行一些多点连续滑动操作。如九宫格滑动操作,连续拖动图片移动等场景。那么在Appium中该如何模拟这类操作呢?

TouchAction

Touch Action包含一些列操作,比如按压、长按、点击、移动、暂停。由着些不同操作可以组成一套动作。使用TochAction需要先导入对应的模块

from appium.webdriver.common.touch_action import TouchAction

按压

方法:press() 开始按压一个元素或坐标点(x,y)。通过手指按压手机屏幕的某个位置。 press也可以接收屏幕的坐标(x,y)。

press(self, el=None, x=None, y=None)

e:

TouchAction(driver).press(x=0,y=308)

 

长按

方法:longPress() 开始按压一个元素或坐标点(x,y)。 相比press()方法,longPress()多了一个入参,既然长按,得有按的时间吧。duration以毫秒为单位。1000表示按一秒钟。其用法与press()方法相同。

long_press(self, el=None, x=None, y=None, duration=1000)

 

点击

方法:tap() 对一个元素或控件执行点击操作。用法参考press()。

tap(self, element=None, x=None, y=None, count=1)

移动

方法:move_to() 将指针从上一个点移动到指定的元素或点。

move_to(self, el=None, x=None, y=None)

注意:

移动到目位置有时是算绝对坐标点,有时是基于前面一个坐标点的偏移量,这个要结合具体App来实践。

暂停

方法:Wait()

wait(self, ms=0)

暂停脚本的执行,单位为毫秒。

 

释放

方法release() 结束的行动取消屏幕上的指针。

release(self)

执行

perform() 执行的操作发送到服务器的命令操作。

perform(self)

TouchAction九宫格滑动操作

TouchAction(driver).press(x=219, y=282).wait(1000) \
        .move_to(x=346, y=291).wait(1000) \
        .move_to(x=523, y=279).wait(1000) \
        .move_to(x=364, y=433).wait(1000) \
        .move_to(x=216, y=576).wait(1000) \
        .move_to(x=362, y=583).wait(1000) \
        .move_to(x=514, y=585).wait(1000) \
        .release().perform()

 

三,多手势操作

使用场景

在使用地图App中,我们经常需要对界面进行缩放操作来更加便利的查看位置。那么在Appium中怎样去模拟这类操作呢?

MultiAction

MultiAction 是多点触控的类,可以模拟用户多点操作。主要包含 add()  perform() 两个方法, MultiAction可以结合前面所学的 ActionTouch可以模拟出用户的多个手指滑动的操作效果;

from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction

加载:

方法add(self, *touch_actions)将TouchAction对象添加到MultiAction中,稍后再执行。

参数:

  • touch_actions - 一个或多个TouchAction对象,描述一个手指要执行的动作链

用法

a1 = TouchAction(driver)
a2 = TouchAction(driver)

a1.press(el1).move_to(el2).release()
a2.press(el2).move_to(el1).release()

MultiAction(driver).add(a1, a2)

执行

perform(self) 执行存储在对象中的操作。

用法

MultiAction(driver).add(a1, a2).perform()

Ps:是不是有点类似Python里面的多线程和多进程的使用。

多点触控操作实践——地图App缩放

滑动原理图解

代码实现

def get_size():
    '''获取屏幕大小'''
    size_dict = driver.get_window_size()
    x = size_dict.get('width')
    y = size_dict.get('height')
    return x,y
def shrink():
    '''缩小'''
    x,y = get_size()
    print(x,y)
    action1 = TouchAction(driver)
    action2 = TouchAction(driver)
    add_action = MultiAction(driver)
    #指定操作
    action1.press(x=x * 0.4, y=y * 0.4).wait(1000).move_to(x=x * 0.2, y=y * 0.2).wait(1000).release()
    action2.press(x=x * 0.6, y=y * 0.6).wait(1000).move_to(x=x * 0.8, y=y * 0.8).wait(1000).release()
    add_action.add(action1,action2)
    #执行操作
    add_action.perform()

 

参考资料

http://appium.io/docs/cn/writing-running-appium/touch-actions/

https://stackoverflow.com/questions/38565116/zoom-action-in-android-using-appium-python-client

 

参考资料:

https://blog.csdn.net/weixin_40180628/article/details/79170053

 

 

猜你喜欢

转载自blog.csdn.net/qq_42758861/article/details/82258283