当前位置: 首页 > news >正文

浅尝Appium自动化框架

浅尝Appium自动化框架

  • Appium自动化框架介绍
  • Appium原理
  • Appium使用
    • 安装平台驱动
    • 实战

Appium自动化框架介绍

Appium 是一个开源的自动化测试框架,最初设计用于移动应用的测试,但现在它也扩展了对桌面端应用的支持。Appium 使得自动化测试变得更加简单,并且支持跨平台,能够同时对 iOS、Android、Windows 和 macOS 平台上的应用进行自动化测试。
不同于Selenium只是用来自动化测试web程序,Appium可以自动化测试各个平台的原生应用。
👉👉👉官网

Appium原理

+------------------+
|  Test Scripts    |
|  (Java, Python,  |
|  JavaScript)     |
+--------+---------+|v
+--------+---------+
|  Appium Server   |
|  (HTTP Server)   |
+--------+---------+|+------------------------------+------------------------------------+|                   |                  |                    |v                   v                  v                    v
+------------+   +-------------+     +--------------+   +--------------+
| iOSDriver |   | AndroidDriver |   | WindowsDriver |   | macOSDriver  |
|  XCUITest |   | (UIAutomator) |   | (WinAppDriver)|   | (macOSDriver)|
+------------+   +-------------+     +--------------+   +--------------+|                |                   |                  |
+------------+   +-------------+     +--------------+   +--------------+
| iOS App |      | Android App|      | Windows App|      | macOS App |
+------------+   +-------------+     +--------------+   +--------------+
  • Test Scripts
    测试脚本可以使用不同编程语言编写,如 Java、Python 或 JavaScript,向 Appium Server 发送 HTTP 请求。
  • Appium Server
    Appium Server 是一个 HTTP 服务器,负责接收客户端的请求,并将请求转发给相应的平台驱动程序。
  • 平台驱动
    iOSDriver: 用于 iOS 平台,支持通过 XCUITest 或 UIAutomation 与 iOS 设备交互。
    AndroidDriver: 用于 Android 平台,支持通过 UIAutomator 或 Espresso 与 Android 设备交互。
    WindowsDriver (WinAppDriver): 用于 Windows 平台,支持通过 WinAppDriver 进行桌面应用的自动化测试。
    macOSDriver: 用于 macOS 平台,支持通过 macOSDriver 进行桌面应用的自动化测试。
  • 应用
    驱动程序与设备或模拟器上的应用进行交互,执行各种操作,如启动应用、查找元素、点击、输入等。

Appium使用

安装平台驱动

驱动平台适用场景
uiautomator2Android原生 Android 应用自动化
xcuitestiOS原生 iOS 应用自动化
espressoAndroid适用于使用 espresso 框架的 Android 应用
mac2macOSmacOS 应用自动化(桌面应用)
windowsWindowsWindows 应用自动化(桌面应用)
safariiOSiOS Safari 浏览器自动化
geckoAndroid, iOSFirefox 浏览器自动化
chromiumAndroid, macOS, WindowsChromium 浏览器自动化(包括 Chrome)

比如:
安装mac驱动

appium driver install mac2

安装后可以用如下命令看是否安装成功。

appium driver list --installed

实战

玩了2天,发现Appium对mac和win上的桌面应用支持的并不是太好,至少兼容性一般。后面我就转到测试android手机上的应用。写了小demo玩玩,打开QQ,找到某某人,发送特定消息。

import timefrom appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy# Desired Capabilities 配置
desired_caps = dict(platformName="Android",platformVersion="14",deviceName="RFCT20EGLNJ",automationName="UiAutomator2",appPackage="com.tencent.mobileqq",appActivity="com.tencent.mobileqq.activity.SplashActivity",enforceXPath1=True,noReset=True
)# 连接 Appium Server
driver = webdriver.Remote("http://127.0.0.1:4723", options = UiAutomator2Options().load_capabilities(desired_caps))try:# 等待并点击搜索按钮search_button = driver.find_element(AppiumBy.ID, "com.tencent.mobileqq:id/wwk")search_button.click()time.sleep(2)# 等待搜索输入框并输入 QQ 号码search_layout = driver.find_element(AppiumBy.ID, "com.tencent.mobileqq:id/jo9")location = search_layout.locationsize = search_layout.size# 点击搜索框(聚焦)driver.tap([(location['x'] + size['width'] / 2, location['y'] + size['height'] / 2)], 500)# 输入qq号码driver.press_keycode(16)driver.press_keycode(10)driver.press_keycode(9)driver.press_keycode(16)# 点击qq用户user_list_layout = driver.find_element(AppiumBy.XPATH, '(//android.widget.LinearLayout[@resource-id="com.tencent.mobileqq:id/ecl"])[1]')# user_button = user_list_layout.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'text("freedom-studio")')   # 可以定位,精确匹配user_button = user_list_layout.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("freedom")') # 可以定位,模糊匹配# user_button = user_list_layout.find_element(AppiumBy.XPATH, './/*[contains(text(), "freedom")]')     # 无法定位user_button.click()time.sleep(2)# 找到聊天输入框msg_input = driver.find_element(AppiumBy.ID, 'com.tencent.mobileqq:id/input')msg_input.send_keys("hello")# 找到发送按钮send_button = driver.find_element(AppiumBy.ID, 'com.tencent.mobileqq:id/send_btn')send_button.click()print("Message sent successfully!")finally:# 退出会话driver.quit()

期间要用到Appium Inspector这个工具,就是用来定位元素的。
在这里插入图片描述
记得要先安装adb工具哦。
其中,
appium:deviceName(设备名)可以通过adb devices获取。

adb devices

appium:appPackage(应用包名)和appium:appActivity(应用界面)可以通过如下adb命令获取。

adb shell dumpsys window | grep "mCurrentFocus"

在这里插入图片描述
最右边的Selected Element里就有xpath,id等信息。如果没有也别感到意外,那就是没有,只能通过其他方法定位元素了。

比如代码里的

# user_button = user_list_layout.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'text("freedom-studio")')   # 可以定位,精确匹配
user_button = user_list_layout.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("freedom")') # 可以定位,模糊匹配
# user_button = user_list_layout.find_element(AppiumBy.XPATH, './/*[contains(text(), "freedom")]')     # 无法定位

理论上,从Appium Inspector里看结构很清晰,XPath肯定是可以定位到的,但是实际就是不行,那只能试试其他方法了,比如AppiumBy.ANDROID_UIAUTOMATOR。

玩Appium也遇到不少坑。

  1. Appium对桌面应用程序支持的并不是非常好,主要还是用来做手机应用的自动化。比如在mac或者win上Appium Inspector经常无法定位特定元素。
  2. 我们一般使用ID, XPath来定位元素,但是有时候定位不到,那也只能另辟蹊径,比如用位置坐标来定位,再比如用ANDROID_UIAUTOMATOR等等。
  3. 测试设备是要和启动appium server的机器连接在一起的。
http://www.lryc.cn/news/519139.html

相关文章:

  • 网络安全测评技术与标准
  • 【经典神经网络架构解析篇】【1】LeNet网络详解:模型结构解析、优点、实现代码
  • KGA:AGeneral Machine Unlearning Framework Based on Knowledge Gap Alignment
  • GelSight Mini视触觉传感器凝胶触头升级:增加40%耐用性,拓展机器人与触觉AI 应用边界
  • springboot整合admin
  • OS--常见的网络模型(包含IO多路复用的原理)
  • LCE(Local Cascade Ensemble)预测模型和LSTM(Long Short-Term Memory)模型在效果和特点上存在显著差异
  • 【mysql】约束的基本使用
  • EasyExcel(二)导出Excel表自动换行和样式设置
  • 农产品直播带货方案拆解
  • “**H5**” 和 “**响应式**” 是前端开发中常见的术语,但它们的概念和使用场景有所不同
  • 基于EasyExcel实现通用版一对一、一对多、多层嵌套结构数据导出并支持自动合并单元格
  • Java堆内存分析
  • maven高级(day15)
  • 计算机组成原理(九):乘法器
  • python【输入和输出】
  • 2024年华为OD机试真题-判断一组不等式是否满足约束并输出最大差-Python-OD统一考试(E卷)
  • 【json】
  • 基于单片机的无线智能窗帘控制器的设计
  • 磁盘满造成业务异常问题排查
  • C++例程:使用I/O模拟IIC接口(6)
  • 58.在 Vue 3 中使用 OpenLayers 绘制点、线、圆、多边形
  • 如何快速上手一个鸿蒙工程
  • c++入门之 命名空间与输入输出
  • GRE技术的详细解释
  • Mysql--基础篇--多表查询(JOIN,笛卡尔积)
  • Java 泛型的用法
  • 人工智能与物联网:智慧城市的未来
  • Python标准库之SQLite3
  • 力扣 二叉树的最大深度