移动端App自动化之触屏操作自动化

103 阅读3分钟

> 本文节选自霍格沃兹测试学院内部教材

工作中我们经常需要对应用的页面进行手势操作,比如滑动、长按、拖动等,AppiumDriver 为我们提供一个模拟手势操作的辅助类

TouchAction,可以通过它对手机屏幕进行手势操作。

具体用法参见链接:ceshiren.com/t/topic/327…

导入TouchAction  

  * 

    from appium.webdriver.common.touch_action import TouchAction

常用的手势操作  

###  

###  

TouchAction 提供的常用的手势操作有如下操作:

  * Press 按下

  * release 释放

  * move_to 移动

  * tap 点击

  * longPress 长按

  * wait 等待

  * cancel 取消

  * perform 执行

 **press**

 **  

**

TouchAction 提供的 press( ) 方法可以实现对元素或者坐标的按下操作。通常会结合 release( )

方法实现对某个元素的点击(包括按下和抬起两个动作)。

在某个控件上执行 press 操作,用法如下:

  * 

    press(WebElement el)

在坐标为(x,y)的点执行 press 操作,用法如下:  

  * 

    press(int x, int y)

 **release**

 **  

**

释放操作,可以结合其它的事件使用。代表该系列动作的一个结束标志。在某个控件上执行释放操作,用法如下:

  * 

     release(WebElement el)

也可以在上一个操作结束之后执行 release,不添加任何参数,用法如下:  

  * 

    release()

 **move_to**

 **  

**

以控件为目标,从一个点移动到该目标上,用法如下:

  * 

     move_to(WebElement el)

以(x,y)点为目标,从一个点移动到该目标,用法如下:  

  * 

    move_to(WebElement el, int x, int y)

 **tap**

 **  

**

在某个控件的中心点上点击一下,用法如下:

  * 

     tap(WebElement el)

以(x,y)坐标点为目标点击,用法如下:  

  * 

    tap(int x, int y)

以控件 el 的左上角为基准,沿着 x 轴向右移动 x 单位,沿着 y 轴向下移动 y 单位。在该点上点击,用法如下:  

  * 

    tap(WebElement el, int x, int y)

 **longpress**

 **  

**

长按某一控件,用法如下:

  * 

     long_press(WebElement el)

以(x,y)点为目标实现长按,用法如下:  

  * 

    long_press(int x, int y)

在控件的左上角的 x 坐标偏移 x 单位,y 左边偏移 y 单位的坐标上长按。用法如下:

  * 

    long_press(WebElement el, int x, int y)

 **wait**

 **  

**

等待,单位为毫秒。可以在操作事件的过程中,短暂的停留几秒再继续操作。用法如下:

  * 

     wait(long timeout)

 **cancel**

 **  

**

可以取消执行事件链中的事件,用法如下:

  * 

     cancel()

 **perform**

 **  

**

执行事件链中的事件,一般最后会调用这个方法,顺序执行事件链中的动作。用法如下:

  * 

     perform()

案例  

打开测试应用,从元素 “Views” 文本滑动到 “Accessibility” 元素,创建一个测试文件

`test_touchaction.py`,代码如下:

测试 app 官方下载地址:github.com/appium/appi…

  *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *   * 

    #!/usr/bin/env python# -*- coding: utf-8 -*-from appium import webdriverfrom appium.webdriver.common.touch_action import TouchAction  

    class TestTouchAction():    def setup(self):        caps = {}        caps['platformName'] = 'Android'        caps['platformVersion'] = '6.0'        caps['deviceName'] = 'emulator-5554'        caps['appPackage'] = 'io.appium.android.apis'        caps['appActivity'] = 'io.appium.android.apis.ApiDemos'        self.driver = webdriver.Remote(\        "http://127.0.0.1:4723/wd/hub", caps)        self.driver.implicitly_wait(5)  

        def teardown(self):        self.driver.quit()  

        def test_touchaction_unlock(self):        # 点击 Views        el1 = self.driver.find_element_by_accessibility_id(            "Views")        # 点击 Accessibility        el2 = self.driver.find_element_by_accessibility_id(            "Accessibility")        # TouchAction 滑动操作        action = TouchAction(self.driver)        action.press(el1).wait(100).move_to\        (el2).wait(100).release().perform()

上面代码可以看出,首先定位两个元素 el1 与 el2 ,分别作为滑动过程中起点元素和终点元素,将我们滑动的起点元素与终点元素找到之后,创建一个

TouchAction 对象,调用里面的 press( ) 方法实现起点元素的点击,使用 wait( ) 方法在事件之间添加等待,使用 move_to( )

方法完成手势的移动操作,然后调用 release( ) 方法来完成手势的抬起,最后调用 perform( ) 方法对添加到 TouchAction

中的事件链顺序执行。

 ** _