『App自动化测试之Appium应用篇』| Appium常用API及操作

简介: 『App自动化测试之Appium应用篇』| Appium常用API及操作

1 press_keycode

1.1 键盘操作

  • press_keycodeAppium的键盘相关函数;
  • 可以实现键盘的相关操作,比如返回、按键、音量调节等等;
  • 函数使用方法为:
driver.press_keycode(KeyCode)

1.2 关于KeyCode

  • 以上press_keycode方法中传入参数KeyCode,而KeyCode是对应的键值码;
  • 其可以传入对应的键值名,也可以传入具体键值名的值(对应数字)。

1.3 press_keycode源码

  • press_keycode源码如下:
    def press_keycode(self, keycode: int, metastate: Optional[int] = None, flags: Optional[int] = None) -> 'WebDriver':
        """Sends a keycode to the device.

        Android only. Possible keycodes can be found
        in http://developer.android.com/reference/android/view/KeyEvent.html.

        Args:
            keycode: the keycode to be sent to the device
            metastate: meta information about the keycode being sent
            flags: the set of key event flags

        Returns:
            Union['WebDriver', 'Keyboard']: Self instance
        """
        ext_name = 'mobile: pressKey'
        args = {
   'keycode': keycode}
        if metastate is not None:
            args['metastate'] = metastate
        if flags is not None:
            args['flags'] = flags
        try:
            self.assert_extension_exists(ext_name).execute_script(ext_name, args)
        except UnknownMethodException:
            # TODO: Remove the fallback
            self.mark_extension_absence(ext_name).execute(Command.PRESS_KEYCODE, args)
        return cast('WebDriver', self)
  • 从源码中可以看出,想要找到对应的键值名可以直接去官网查看。

1.4 电话键相关

  • 以下为部分(非全部,仅参考)电话键相关键值名:
键值名 说明 键值
KEYCODE_HOME HOME 3
KEYCODE_BACK 返回键 4
KEYCODE_CALL 拨号键 5
KEYCODE_EKDCALL 挂机键 6
KEYCODE_VOLUME_UP 音量增加键 24
KEYCODE_VOLUME_DOWN 音量减减键 25
KEYCODE_POWER 电源键 26
KEYCODE_CAMERA 拍照键 27
KEYCODE_MENU 菜单键 82
KEYCODE_NOTIFICATION 通知键 83
KEYCODE_SEARCH 搜索键 84

1.5 控制键相关

  • 以下为部分(非全部,仅参考)控制键相关键值名:
键值名 说明 键值
KEYCODE_DPAD_UP 导航键 向上 19
KEYCODE_DPAD_DOWN 导航键 向下 20
KEYCODE_DPAD_LEFT 导航键 向左 21
KEYCODE_DPAD_RIGHT 导航键 向右 22
KEYCODE_DPAD_CENTER 导航键 确定键 23
KEYCODE_TAB TAB 61
KEYCODE_ENTER 回车键 66
KEYCODE_DEL 退格键 67
KEYCODE_ESCAPE ESC 111
KEYCODE_FORWARD_DEL 删除键 112
KEYCODE_CAPS_LOCK 大写锁定键 115
KEYCODE_SCROLL_LOCK 滚动锁定键 116

1.6 基本按键相关

  • 以下为部分(非全部,仅参考)基本按键相关键值名:
  • 其中按键0-9键值为7-16,比如:
键值名 说明 键值
KEYCODE_0 按键’0’ 7
KEYCODE_1 按键’1’ 8
KEYCODE_2 按键’2’ 9
  • 其中字母A-Z的键值为29-54,比如:
键值名 说明 键值
KEYCODE_A 按键’A’ 29
KEYCODE_B 按键’B’ 30
KEYCODE_C 按键’C’ 31

1.7 组合键相关

  • 以下为部分(非全部,仅参考)组合键相关键值名:
键值名 说明
KEYCODE_ALT_LEFT ALT+LIFT
KEYCODE_ALEERT_RIGHT ALT_RIGHT
KEYCODE_CTRL_LEFT Ctrl_lEFT
KEYCODE_CTRL_RIGHT Ctrl_RIGHT
KEYCODE_SHIFT_LEFT Shift+lEFT
KEYCODE_SHIFT_RIGHT Shift+RIGHT

1.8 符号键相关

  • 以下为部分(非全部,仅参考)符号键相关键值名:
键值名 说明
KEYCODE_PLUS 按键’+’
KEYCODE_MINUS 按键’-’
KEYCODE_STAR 按键’*’
KEYCODE_SLASH 按键’/’
KEYCODE_EQUALS 按键’=’
KEYCODE_AT 按键’@’
KEYCODE_POUND 按键’#’
KEYCODE_SPACE 空格键

1.9 使用举例

  • 使用方法为:
driver.press_keycode(4) # 返回键
driver.press_keycode(84) # 搜索键
  • 或者可以使用keyevent方法:
driver.keyevent(66) # 回车键
driver.keyevent(67) # 退格键

2 swip方法

2.1 swip说明

  • swip()方法是从一个坐标位置滑动到另一个坐标位置;
  • 也就是说两点之间的滑动。

2.2 swip使用方法

  • 可以查看swip源码来看下如何使用:
    def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> 'WebDriver':
        """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: defines the swipe speed as time taken to swipe from point a to point b, in ms.

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

        Returns:
            Union['WebDriver', 'ActionHelpers']: Self instance
        """
        touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")

        actions = ActionChains(self)
        actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
        actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
        actions.w3c_actions.pointer_action.pointer_down()
        if duration > 0:
            actions.w3c_actions = ActionBuilder(self, mouse=touch_input, duration=duration)
        actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
        actions.w3c_actions.pointer_action.release()
        actions.perform()
        return cast('WebDriver', self)
  • 从以上看需要至少四个参数swipe(self, start_x: int, start_y: int, end_x: int, end_y: int);

2.3 使用示例

  • 比如坐标从(100,200)滑动到(300,400):
driver.swipe(100, 200, 300, 400)
  • 再比如从(400,500)滑动到(600,700)持续3秒:
driver.swipe(400, 500, 600, 700, 3000)

3 scroll方法

  • scroll()方法是从一个元素滑动到另一个元素,直到页面自动停止;
  • 使用方法为:
    def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> 'WebDriver':
        """Scrolls from one element to another

        Args:
            origin_el: the element from which to begin scrolling (center of element)
            destination_el: the element to scroll to (center of element)
            duration: defines speed of scroll action when moving from originalEl to destinationEl.
                Default is 600 ms for W3C spec.

        Usage:
            driver.scroll(el1, el2)
  • 比如从用户名滑动到密码输入框:
user_name = driver.find_element(AppiumBy.XPATH, "//*[@text='用户名']")
user_passwd = driver.find_element(AppiumBy.XPATH, "//*[@text='密码']")
driver.scroll(user_name, user_passwd)

4 drag_and_drop方法

  • drag_and_drop()方法从一个元素滑动到另一个元素,第二个元素代替第一个元素原本屏幕上的位置;
  • 使用方法为:
    def drag_and_drop(self, origin_el: WebElement, destination_el: WebElement) -> 'WebDriver':
        """Drag the origin element to the destination element

        Args:
            origin_el: the element to drag
            destination_el: the element to drag to

        Returns:
            Union['WebDriver', 'ActionHelpers']: Self instance
        """
  • 比如:
user_name = driver.find_element(AppiumBy.XPATH, "//*[@text='用户名']")
user_passwd = driver.find_element(AppiumBy.XPATH, "//*[@text='密码']")
driver.drag_and_drop(user_name, user_passwd)

5 TouchAction方法

  • TouchAction可实现手势的操作,比如滑动、拖动、长按等操作;
  • 使用方法是先需要导入TouchAction
from appium.webdriver.common.touch_action import  TouchAction

5.1 tap方法

  • tap()方法模拟手指对某个元素或坐标按下并快速抬起;
  • 使用方法为:
    def tap(
        self,
        element: Optional['WebElement'] = None,
        x: Optional[int] = None,
        y: Optional[int] = None,
        count: int = 1,
    ) -> 'TouchAction':
        """Perform a tap action on the element

        Args:
            element: the element to tap
            x : x coordinate to tap, relative to the top left corner of the element.
            y : y coordinate. If y is used, x must also be set, and vice versa
  • 比如:
TouchAction(driver).tap(user_name).perform()

5.2 press方法

  • press()方法是手指一直按下;
  • 使用方法:
    def press(
        self,
        el: Optional['WebElement'] = None,
        x: Optional[int] = None,
        y: Optional[int] = None,
        pressure: Optional[float] = None,
    ) -> 'TouchAction':
        """Begin a chain with a press down action at a particular element or point

        Args:
            el: the element to press
            x: x coordiate to press. If y is used, x must also be set
            y: y coordiate to press. If x is used, y must also be set
  • 比如:
TouchAction(driver).press(x=100, y=200).perform()

5.3 release方法

  • release()方法是模拟手指抬起;
  • 使用方法:
    def release(self) -> 'TouchAction':
        """End the action by lifting the pointer off the screen

        Returns:
            `TouchAction`: Self instance
        """
        self._add_action('release', {
   })

        return self
  • 比如:
TouchAction(driver).press(x=100, y=200).release().perform()

5.4 wait方法

  • wait()方法是模拟手指等待;
  • 使用方法为:
 def wait(self, ms: int = 0) -> 'TouchAction':
        """Pause for `ms` milliseconds.

        Args:
            ms: The time to pause

        Returns:
            `TouchAction`: Self instance
        """
  • 比如按下等待3秒后抬起:
TouchAction(driver).press(x=100, y=200).wait(3000).release().perform()

5.5 move_to方法

  • move_to()方法是模拟手指移动;
  • 使用方法:
    def move_to(
        self, el: Optional['WebElement'] = None, x: Optional[int] = None, y: Optional[int] = None
    ) -> 'TouchAction':
        """Move the pointer from the previous point to the element or point specified

        Args:
            el: the element to be moved to
            x: x coordiate to be moved to. If y is used, x must also be set
            y: y coordiate to be moved to. If x is used, y must also be set

        Returns:
            `TouchAction`: Self instance
        """
  • 比如:
TouchAction(driver).press(x=400, y=500).move_to(500, 600).perform()
目录
相关文章
|
7月前
|
供应链 搜索推荐 数据挖掘
探秘京东 API 接口的神奇应用场景
京东API如同数字钥匙,助力商家实现商品、库存、订单等多平台高效同步,提升效率超80%。支持物流实时追踪,增强用户满意度;赋能精准营销与数据分析,决策准确率提升20%以上,全面优化电商运营。
193 1
|
8月前
|
人工智能 自然语言处理 机器人
使用 API 编程开发扣子应用
扣子(Coze)应用支持通过 API 编程,将 AI 聊天、内容生成、工作流自动化等功能集成至自有系统。主要 API 包括 Bot API(用于消息交互与会话管理)及插件与知识库 API(扩展功能与数据管理)。开发流程包括创建应用、获取密钥、调用 API 并处理响应,支持 Python 等语言。建议加强错误处理、密钥安全与会话管理,提升集成灵活性与应用扩展性。
2663 0
|
9月前
|
监控 供应链 搜索推荐
电商数据开发实践:深度剖析1688商品详情 API 的技术与应用
在电商数字化转型中,数据获取效率与准确性至关重要。本文介绍了一款高效商品详情API,具备全维度数据采集、价格库存管理、多媒体资源获取等功能,结合实际案例探讨其在电商开发中的应用价值与优势。
|
7月前
|
Ubuntu API C++
C++标准库、Windows API及Ubuntu API的综合应用
总之,C++标准库、Windows API和Ubuntu API的综合应用是一项挑战性较大的任务,需要开发者具备跨平台编程的深入知识和丰富经验。通过合理的架构设计和有效的工具选择,可以在不同的操作系统平台上高效地开发和部署应用程序。
297 11
|
8月前
|
人工智能 数据可视化 测试技术
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
AI 时代 API 自动化测试实战:Postman 断言的核心技巧与实战应用
1048 11
|
8月前
|
安全 API 数据安全/隐私保护
【Azure 环境】Microsoft Graph API实现对Entra ID中应用生成密码的时间天数
本文介绍如何通过 Azure 的 App Management Policy 限制用户在创建 AAD 应用程序的 Client Secret 时设置最长 90 天的有效期。通过 Microsoft Graph API 配置 defaultAppManagementPolicy,可有效控制密码凭据的生命周期,增强安全管理。
212 4
|
8月前
|
Java API 开发者
揭秘淘宝详情 API 接口:解锁电商数据应用新玩法
淘宝详情API是获取商品信息的“金钥匙”,可实时抓取标题、价格、库存等数据,广泛应用于电商分析、比价网站与智能选品。合法调用,助力精准营销与决策,推动电商高效发展。(238字)
329 0
|
Java 测试技术 C#
自动化测试之美:从Selenium到Appium
【10月更文挑战第3天】在软件开发的海洋中,自动化测试如同一艘航船,引领着质量保证的方向。本文将带你领略自动化测试的魅力,从Web端的Selenium到移动端的Appium,我们将一探究竟,看看这些工具如何帮助我们高效地进行软件测试。你将了解到,自动化测试不仅仅是技术的展示,更是一种提升开发效率和产品质量的智慧选择。让我们一起启航,探索自动化测试的世界!
|
Web App开发 IDE 测试技术
自动化测试的利器:Selenium 框架深度解析
【10月更文挑战第2天】在软件开发的海洋中,自动化测试犹如一艘救生艇,让质量保证的过程更加高效与精准。本文将深入探索Selenium这一强大的自动化测试框架,从其架构到实际应用,带领读者领略自动化测试的魅力和力量。通过直观的示例和清晰的步骤,我们将一起学习如何利用Selenium来提升软件测试的效率和覆盖率。
|
测试技术 数据安全/隐私保护 开发者
自动化测试的奥秘:如何用Selenium和Python提升软件质量
【9月更文挑战第35天】在软件开发的海洋中,自动化测试是那艘能引领我们穿越波涛的帆船。本文将揭开自动化测试的神秘面纱,以Selenium和Python为工具,展示如何构建一个简单而强大的自动化测试框架。我们将从基础出发,逐步深入到高级应用,让读者能够理解并实现自动化测试脚本,从而提升软件的质量与可靠性。

热门文章

最新文章