• 技术分享 | app自动化测试(Android)–触屏操作自动化


    导入TouchAction

    Python 版本

    from appium.webdriver.common.touch_action import TouchAction
    
    • 1

    Java 版本

    import io.appium.java_client.TouchAction;
    
    • 1

    常用的手势操作

    press 按下

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

    press 按下

    release 释放

    move_to/moveTo 移动

    tap 点击

    long_press/longPress 长按

    wait 等待

    cancel 取消

    perform 执行

    TouchAction 提供的 press( ) 方法可以实现对元素或者坐标的按下操作。通常会结合 release( ) 方法实现对某个元素的点击(包括按下和抬起两个动作)。

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

    Python 版本

    按下某个元素,用法如下:

    press(WebElement el)
    
    • 1

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

    press(int x, int y)
    
    • 1

    Java 版本

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

    press(int x, int y)
    
    • 1

    release 释放

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

    Python 版本

    release(WebElement el)
    
    • 1

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

    release()
    
    • 1

    Java 版本

    release()
    
    • 1

    移动

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

    Python 版本

    move_to(WebElement el)
    
    • 1

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

    move_to(WebElement el, int x, int y)
    
    • 1

    Java 版本

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

    moveTo(WebElement el, int x, int y)
    
    • 1

    tap 点击

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

    Python 版本

    tap(WebElement el)
    
    • 1

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

    tap(WebElement el, int x, int y)
    
    • 1

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

    tap(int x, int y)
    
    • 1

    Java版本

    只提供坐标点击,用法如下:

    tap(int x, int y)
    
    • 1

    长按

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

    Python 版本

    long_press(WebElement el)
    
    • 1

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

    long_press(int x, int y)
    
    • 1

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

    long_press(WebElement el, int x, int y)
    
    • 1

    Java 版本

    只提供坐标点击,用法如下:

    longPress(int x, int y)
    
    • 1

    等待

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

    Python 版本

    wait(long timeout)
    
    • 1

    Java 版本

    wait(long timeout)
    
    • 1

    cancel 取消

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

    Python 版本

    cancel()
    
    • 1

    Java 版本

    cancel()
    
    • 1

    执行 perform

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

    Python 版本

    perform()
    
    • 1

    Java 版本

    perform()
    
    • 1

    案例

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

    测试 app 官方下载地址:https://github.com/appium/appium/tree/master/sample-code/apps

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # 测试文件 test_touchaction.py
    from appium import webdriver
    from 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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    public class TouchActionTest {
        static AppiumDriver driver;
    
        @BeforeAll
        public static void beforeAll() throws MalformedURLException {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability("deviceName", "emulator-5554");
            caps.setCapability("platformName", "Android");
            caps.setCapability("appPackage", "io.appium.android.apis");
            caps.setCapability("appActivity", "io.appium.android.apis.\
            ApiDemos");
            URL appiumServer = new URL("http://127.0.0.1:4723/wd/hub");
            driver = new AndroidDriver(appiumServer, caps);
            driver.manage().timeouts().implicitlyWait(10, \
            TimeUnit.SECONDS);
        }
    
        @Test
        void test() {
            // 创建 TouchAction 对象
            TouchAction action = new TouchAction<>(driver);
            // TouchAction 滑动操作
            action.press(PointOption.point((int) (width * 0.5), \
            (int) (height * 0.8))).waitAction(WaitOptions.\
            waitOptions(Duration.ofSeconds(2))).moveTo(\
            PointOption.point((int) (width * 0.5), \
            (int) (height * 0.2))).release().perform();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    以上两段代码实现了相同的操作,创建了一个 TouchAction 对象,调用里面的 press() 方法实现起点元素的点击,使用 wait() 方法在事件之间添加等待,使用 move_to()/moveTo() 方法完成手势的移动操作,然后调用 release() 方法来完成手势的抬起,最后调用 perform() 方法对添加到 TouchAction 中的事件链顺序执行。

    最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。

    学习不要孤军奋战,最好是能抱团取暖,相互成就一起成长,群众效应的效果是非常强大的,大家一起学习,一起打卡,会更有学习动力,也更能坚持下去。你可以加入我们的测试技术交流扣扣群:914172719(里面有各种软件测试资源和技术讨论)

    喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!

  • 相关阅读:
    vi使用方法详细介绍
    VS2019+QT5.15调用动态库dll带有命名空间
    (windows10)设置环境变量简化EVOSUITE的运行
    数据可视化基础篇-图形语法
    如何使用并查集解决朋友圈问题?
    SSM集成
    【Python日志模块全面指南】:记录每一行代码的呼吸,掌握应用程序的脉搏
    解决-linux 一次 高并发处理过程。
    06-分布式消息队列Kafka
    Proxy应用场景
  • 原文地址:https://blog.csdn.net/wx17343624830/article/details/125504271