App自动化测试:高级控件交互方法

简介

APP 的控件元素不仅涵盖了基础用户界面操作,还包括用户与应用程序中更复杂、功能丰富的空间之间的互动。这种交互远不止于简单的按钮或输入框。通过借助 Appium 的 Actions,能够完成对应用程序进行手势识别的交互。这意味着可以通过各种手势,如滑动、缩放、长按等,实现更灵活、直观的用户体验。这种高级交互使得应用程序更具交互性和吸引力,为用户提供了更深入参与应用功能的途径。

Actions 介绍

Actions 是 Appium 中的关键类,专门设计用于执行各种手势和交互操作,包括但不限于点击、滑动、长按等。这个类的存在使得在移动端应用程序的自动化测试中,通过 Appium 可以轻松地控制设备活模拟器执行多样化的手势操作,通过 Actions ,测试人员可以模拟用户真实的操作行为,确保应用在不同交互场景下的稳定性和可靠性。这一功能对于移动应用的全面测试和质量保证至关重要。

ActionChains 和 Actions 区别

ActionChains 是 Selenium WebDriver 中的一个类,可用于执行一系列的操作,如鼠标悬停、拖放、按下键盘等。

而 Actions 是 Appium 中的一个类,用于执行手势和交互操作,如点击、滑动、长按等。

尽管两者的名称相似,但它们是针对不同的自动化测试环境而设计的。

ActionChains 适用于网页自动化测试,通过 Selenium WebDriver 控制浏览器执行各种交互操作,并提供了一系列方法来模拟用户的行为。

而 Actions 则适用于移动端应用程序的自动化测试,通过 Appium 控制设备或模拟器执行各种手势操作。

除了适用于不同的自动化测试环境之外, ActionChains 和 Actions 的用法和语法也略有不同。在 Selenium WebDriver 中使用 ActionChains 时,可以通过链式调用方法来执行一系列操作,并使用 perform() 方法来触发操作的执行。而在 Appium 中使用 Actions 时,需要创建 TouchAction 对象,并使用其提供的方法来执行手势操作,并使用 perform() 方法来触发手势的执行。

Actions 用法

在使用 ActionChains 进行用户交互自动化时,首先需要导入 ActionChains 类以及其他相关模块,然后定义一个 ActionChains 实例,并将 driver 传入。之后,可以通过定义输入源和具体的动作来实现各种用户交互操作。

  • 导入 ActionChains 类及其他模块
  • 定义 ActionChains 实例 'actions',传入 driver
  • 定义输入源
  • 定义动作
  • 执行动作

滑动解锁示例

  • 安装手势密码锁 app(TouchAction.apk)
  • 打开应用
  • 点击【设置手势】
  • 完成手势操作(如图)

实现手势滑动时,通常需要结合坐标,并可通过设置设备的输入选项,从界面中找到具体的坐标点。

手势滑动路径如下图所示:

Python 版本


class TestActionChains:

    def setup_class(self):
        # 设置启动参数
        caps = {
            "platformName": "Android",
            "appium:appPackage": "cn.kmob.screenfingermovelock",
            "appium:appActivity": "com.samsung.ui.FlashActivity",
            "appium:noReset": True,
            "appium:shouldTerminateApp": True,
        }
        # 初始化 driver
        self.driver = webdriver.Remote('http://localhost:4723', options=UiAutomator2Options().load_capabilities(caps))
        # 设置隐式等待
        self.driver.implicitly_wait(15)

    def teartdown_class(self):
        # 退出应用程序
        self.driver.quit()

    def test_slide_to_unlock(self):
        # 点击设置手势
        self.driver.find_element(by=AppiumBy.ID, value="cn.kmob.screenfingermovelock:id/patternTxt").click()
        print(self.driver.get_window_size())
        # 定义ActionChains实例
        actions = ActionChains(self.driver)
        # 定义输入源
        actions.w3c_actions = ActionBuilder(self.driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
        # 定义动作 pointer_down按下 pause暂停 release释放
        # 需要实现3个点之间的滑动,A->B 水平滑动 B—>C 竖直滑动
        bounds = self.driver.find_element(AppiumBy.ID, 'cn.kmob.screenfingermovelock:id/patternView').get_attribute(
            'bounds')
        actions.w3c_actions.pointer_action.move_to_location(204, 377)
        actions.w3c_actions.pointer_action.pointer_down()
        actions.w3c_actions.pointer_action.move_to_location(930, 373)
        # 停顿0.5s 模拟在两个点之间进行拖拽操作
        actions.w3c_actions.pointer_action.pause(0.5)
        actions.w3c_actions.pointer_action.move_to_location(846, 1150)
        actions.w3c_actions.pointer_action.pause(0.5)
        actions.w3c_actions.pointer_action.release()
        # 执行操作
        actions.perform()
                # 获取【继续】按钮的 clickable 属性值
        result = self.driver.find_element(AppiumBy.ID, "cn.kmob.screenfingermovelock:id/btnTwo").get_attribute(
            "clickable")
        # 断言【继续按钮】可点击
        assert result == "true"

总结

  • Actions 用法
  • 滑动解锁示例
全部评论

相关推荐

1 1 评论
分享
牛客网
牛客企业服务