文章总结: Appium是一个支持iOS、Android和Windows平台的自动化开源工具,可用于原生、移动Web和混合应用的自动化测试。文章详细介绍了Appium的服务端配置、AndroidUiAutomator2和iOSxcuitest驱动程序的安装与配置步骤,以及使用appium-inspector图形界面和Appium-Python-Client进行测试的方法。对于移动应用安全测试人员,建议按照文中步骤配置环境,使用appium-inspector快速定位元素并生成测试代码,提高移动应用安全测试效率。
综合评分: 87
文章分类: 移动安全,安全工具,应用安全
使用appium模拟移动设备操作
原创
initsec
初始安全
2024年5月10日 09:33
中国香港
-
介绍
-
服务端配置
-
Driver
-
【Android】UiAutomator2
-
【IOS】xcuitest
-
客户端
-
appium-inspector
-
Appium-Python-Client
介绍
Appium 看名字就知道和 selenium 是一类的,让机器模拟人去操作,这里介绍常见的 Android 和 IOS 配置和踩坑。
建议看官方英文文档,网上的很多文章太老了现在完全不适用,而且官方中文文档很多没翻译过来还是 TODO 状态…
Appium 是一个自动化开源工具,支持 iOS、Android 和 Windows 桌面平台上的原生、移动 Web 和混合应用的自动化。Appium 是跨平台的:它允许你用同样的 API 对多平台(iOS、Android、Windows)写测试。做到在 iOS、Android 和 Windows 测试套件之间复用代码。
官方文档:https://appium.io/docs/en/latest/
服务端配置
-
nodejs
^14.17.0 || ^16.13.0 || >=18.0.0 -
npm
>=8 -
Appium 服务端
-
“`
npm i -g appium
npm update -g appium # 更新
配置好后,直接运行 `appium` 就可以启动服务端。
## Driver
### 【Android】UiAutomator2
安卓的配置比较简单方便,使用UiAutomator2[1],需要的环境和步骤如下:
1. Android SDK[2]
2. ```
export ANDROID_HOME=/Users/d4m1ts/Library/Android/sdk/platform-tools
- Java8+ SDK[3]
- “`
export JAVAHOME=/Library/Java/JavaVirtualMachines/jdk1.8.0261.jdk/Contents/Home
5. 在真机上启动 USB 调试[4]
6. ```
appium driver install uiautomator2
【IOS】xcuitest
iOS 上相对比较麻烦,driver 使用xcuitest[5],配置可参考官方文档[6]
appium driver install xcuitest
可使用如下命令来检测依赖是否正常,把缺失的依赖安装上即可(最好还是看文档[7],里面有很多可选的依赖都可以装上。
appium driver doctor xcuitest
依赖解决后,一些在手机上的配置项:
- 设置 -> 通用 -> 键盘 -> 自动改正(关闭,默认开启
- 设置 -> 通用 -> 键盘 -> 预测文本(关闭,默认开启
- 设置 -> 辅助功能 -> 缩放(关闭,默认关闭
- 设置 -> 辅助功能 -> 朗读内容 -> 朗读所选内容(关闭,默认关闭
- 设备连上 Mac 后选择信任设备
- 使用 iOS/iPadOS 16 或更高版本的设备需要启用开发者模式[8]:设置 -> 隐私与安全性 -> 开发者模式;重启后还需要配置:设置 -> 开发者 -> 启用 UI 自动化
通过xcodebuild安装WebDriverAgentRunner-Runner (WDA) 与被测设备进行通信,运行如下命令直接在 Xcode 中打开 WebDriverAgent.xcodeproj
appium driver run xcuitest open-wda
因为没有付费开发者账户,所以必须手动进行签名配置 WDA,我这里采用的是完全手动配置[9]
先进入 WDA 的目录下,创建目录和文件:
mkdir -p Resources/WebDriverAgent.bundle
选中WebDriverAgentRunner项目后,再选择Build Settings,修改Bundle Identifier为可接受的内容
image-20240506下午24344053
然后在 Signing & Capabilities 中选择 Team,没有出现问题就 OK 了
image-20240506下午24548736
最后安装到手机上,步骤如下:
- Product -> Scheme -> WebDriverAgentRunner
- Product -> Destination
- Product -> Test
安装好后还需要信任,信任后再点一下刚才安装的 WDA 软件启动一次即可。
image-20240506下午51941530
客户端
主要推荐 2 个客户端,1 个是带图形化界面方便分析的,1 个是纯 py 脚本的
appium-inspector
A GUI inspector for mobile apps and more, powered by a (separately installed) Appium server
主要用于看各类元素说明啥的
可直接下载 appium-inspector[10]安装
Android
打开 Appium Inspector,在 JSON Representation 中填入如下数据:
{
"platformName": "Android",
"appium:automationName": "uiautomator2",
"appium:deviceName": "AOSP_on_blueline", // adb devices -l 中model后的内容
"appium:platformVersion": "10", // adb shell getprop ro.build.version.release
"appium:appPackage": "com.tencent.mm", // adb shell dumpsys activity | grep mResumedActivity
"appium:appActivity": ".ui.LauncherUI",
"appium:noReset": true
}
具体的字段和说明参考:https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#capabilities
配置后点击 Start Session
image-20240430下午53719069
出现如下的界面就 OK 了,可以看到各种元素
image-20240430下午54014959
在 Session Information 的面板中,也可以看到示例代码,可以直接复制过去用
image-20240430下午54455282
IOS
打开 Appium Inspector,在 JSON Representation 中填入如下数据:
{
"platformName": "ios",
"appium:automationName": "xcuitest",
"appium:deviceName": "00008120-00084DCC36F8A01E", // udid
"appium:platformVersion": "17.4.1",
"appium:udid": "00008120-00084DCC36F8A01E", // udid
"appium:noReset": true
}
具体的字段和说明参考:https://appium.github.io/appium-xcuitest-driver/latest/reference/capabilities/
其他操作就和 Android 一样了
image-20240506下午33647917
Appium-Python-Client
通过上方的 GUI 客户端可以快速的定位元素、生成示例代码,也可以录制操作自动变成代码,其中函数和 selenium 差不多,就不赘述了。
依赖安装:pip install Appium-Python-Client
一些 demo 可参考 functional test code[11],完整的文档可参考 Documentation[12]
# This sample code supports Appium Python client >=2.3.0
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python
from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
# For W3C actions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
options = AppiumOptions()
options.load_capabilities({
"platformName": "Android",
"appium:automationName": "uiautomator2",
"appium:deviceName": "AOSP_on_blueline",
"appium:platformVersion": "10",
"appium:appPackage": "com.tencent.mm",
"appium:appActivity": ".ui.LauncherUI",
"appium:noReset": True,
"appium:ensureWebviewsHavePages": True,
"appium:nativeWebScreenshot": True,
"appium:newCommandTimeout": 3600,
"appium:connectHardwareKeyboard": True
})
driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
driver.quit()
参考资料
[1]
UiAutomator2: https://github.com/appium/appium-uiautomator2-driver
[2]
Android SDK: https://developer.android.google.cn/studio#command-line-tools-only
[3]
Java8+ SDK: https://www.oracle.com/java/technologies/downloads/archive/
[4]
启动 USB 调试: https://developer.android.com/studio/debug/dev-options?hl=zh-cn
[5]
xcuitest: https://github.com/appium/appium-xcuitest-driver
[6]
官方文档: https://appium.github.io/appium-xcuitest-driver/latest/
[7]
文档: https://appium.github.io/appium-xcuitest-driver/latest/installation/requirements/
[8]
启用开发者模式: https://developer.apple.com/documentation/xcode/enabling-developer-mode-on-a-device
[9]
完全手动配置: https://appium.github.io/appium-xcuitest-driver/latest/preparation/prov-profile-full-manual/
[10]
appium-inspector: https://github.com/appium/appium-inspector
[11]
functional test code: https://github.com/appium/python-client/tree/master/test/functional
[12]
Documentation: https://appium.github.io/python-client-sphinx/
- END –
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:初始安全 initsec《使用appium模拟移动设备操作》