1 关于appium driver
- 在之前的文章我们已经完整的安装了
appium
以及安装过程中相关问题的排查; - 我们启动下
appium
的服务看看:
python
C:\Users\Administrator>appium
[Appium] Welcome to Appium v2.2.3
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
[Appium] http://172.16.1.33:4723/
[Appium] http://127.0.0.1:4723/ (only accessible from the same host)
[Appium] http://172.23.16.1:4723/
[Appium] No drivers have been installed in C:\Users\Administrator\.appium. Use the "appium driver" command to install the one(s) you want to use.
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
- 发现提示
appium driver
没有安装,我们安装下吧; appium driver
测试驱动是分开管理的,默认不会安装任何的测试驱动,通过appium driver
子命令管理所有的驱动;- 官方可用的驱动如下:
python
C:\Users\Administrator>appium driver list
- Listing available drivers
✔ Listing available drivers
- uiautomator2 [not installed]
- xcuitest [not installed]
- mac2 [not installed]
- espresso [not installed]
- safari [not installed]
- gecko [not installed]
- chromium [not installed]
- 我们的环境是Android,所以需要安装下
uiautomator2
,其他的按照情况来安装即可。
2 安装appium driver
- 我们使用以下
appium driver install uiautomator2
命令安装,但是报错了:
python
C:\Windows\System32>appium driver install uiautomator2
- Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
× Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
Error: × Encountered an error when installing package: npm command 'install --save-dev --omit=peer --save-exact --global-style --no-package-lock appium-uiautomator2-driver --json' failed with code 1.
STDOUT:
{
"error": {
"code": "ETIMEDOUT",
"summary": "request to https://registry.npmjs.org/appium-uiautomator2-driver failed, reason: ",
"detail": "This is a problem related to network connectivity.\nIn most cases you are behind a proxy or have bad network settings.\n\nIf you are behind a proxy, please make sure that the\n'proxy' config is set properly. See: 'npm help config'"
- 解决方法是修改npm的镜像源:
python
# 1.查看npm镜像设置
npm config get registry
# 2.将npm设置为淘宝镜像
npm config set registry https://registry.npm.taobao.org
# 3.再次查看npm镜像设置
npm config get registry
- 再次安装就成功了:
python
C:\Windows\System32>appium driver install uiautomator2
- Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
√ Installing 'uiautomator2' using NPM install spec 'appium-uiautomator2-driver'
i Driver uiautomator2@2.34.2 successfully installed
- automationName: UiAutomator2
- platformNames: ["Android"]
3 安装Appium Python Client
Python Client
针对不同的语言有不同的安装方法,我们使用的python
版本的;- 关于appium的安装过程的步骤之前已经介绍,也可参考管放文档:Quickstart Intro;
- 安装
Appium Python Client
:
python
pip install Appium-Python-Client
python
Successfully built Appium-Python-Client
Installing collected packages: sortedcontainers, urllib3, sniffio, pysocks, pycparser, idna, h11, certifi, attrs, wsproto, outcome, cffi, trio, trio-websocket, selenium, Appium-Python-Client
Successfully installed Appium-Python-Client-3.1.0 attrs-23.1.0 certifi-2023.11.17 cffi-1.16.0 h11-0.14.0 idna-3.6 outcome-1.3.0.post0 pycparser-2.21 pysocks-1.7.1 selenium-4.16.0 sniffio-1.3.0 sortedcontainers-2.4.0 trio-0.23.1 trio-websocket-0.11.1 urllib3-2.1.0 wsproto-1.2.0
4 安装测试对象
- 下载一个支持安卓6.0的
扣扣
,因为我们模拟器是安卓6.0的; - 把下载的
扣扣
传到模拟器中,本地保存在如下:
python
F:\monkey_test\QQv5.apk
- 我们先卸载之前装的:
python
adb uninstall com.tencent.mobileqqi
python
F:\monkey_test>adb uninstall com.tencent.mobileqqi
Success
- 重新安装:
python
adb install QQv5.apk /data/temp
python
F:\monkey_test>adb install QQv5.apk /data/temp
Performing Push Install
QQv5.apk: 1 file pushed, 0 skipped. 13.1 MB/s (22506899 bytes in 1.634s)
pkg: /data/local/tmp/QQv5.apk
ver: /data/temp
Success
5 获取测试对象信息
- 以下有三种方式获取应用程序的信息;
- 我们需要获取的重要信息为:
当前app的包名和活动窗口名
; - 三种方式都可以,选择自己方便的即可。
5.1 使用dumpsys
- 前提是需要先打开需要测试的
app
; - 我们启动
APP
后在命令行输入:
python
adb shell dumpsys | find "mFocusedActivity"
- 可以看到如下:
python
mFocusedActivity: ActivityRecord{54035c5 u0 com.tencent.mobileqqi/com.tencent.mobileqq.activity.InstallActivity t4}
- 那么重要信息有两个:
python
# 包名:com.tencent.mobileqqi
# 活动窗口名:com.tencent.mobileqq.activity.LoginActivity
5.2 使用AndroidKiller
AndroidKiller
主要是对apk
进行反编译操作获取相关信息;- 这里不说了,自行搜索吧。
5.3 使用aapt
- 这个相对来说会方便点,直接找到安装包的路径,使用命令查看即可;
- 比如我的安装包在本地的
F:\monkey_test
下:
python
aapt dump badging QQv5.apk
- 查看的信息如下:
python
F:\monkey_test>aapt dump badging QQv5.apk
package: name='com.tencent.mobileqqi' versionCode='6062' versionName='5.1.1'
install-location:'auto'
sdkVersion:'7'
targetSdkVersion:'15'
uses-permission: name='com.android.launcher.permission.INSTALL_SHORTCUT'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.VIBRATE'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.CHANGE_CONFIGURATION'
uses-permission: name='android.permission.RECEIVE_BOOT_COMPLETED'
uses-permission: name='android.permission.WAKE_LOCK'
uses-permission: name='android.permission.SYSTEM_ALERT_WINDOW'
uses-permission: name='android.permission.RECORD_AUDIO'
uses-permission: name='com.tencent.msf.permission.account.sync'
uses-permission: name='android.permission.MODIFY_AUDIO_SETTINGS'
uses-permission: name='android.permission.CAMERA'
uses-permission: name='android.permission.CHANGE_WIFI_STATE'
uses-permission: name='android.permission.ACCESS_WIFI_STATE'
uses-permission: name='android.permission.READ_PHONE_STATE'
uses-permission: name='android.permission.KILL_BACKGROUND_PROCESSES'
uses-permission: name='android.permission.CALL_PHONE'
uses-permission: name='com.android.launcher.permission.READ_SETTINGS'
uses-permission: name='com.android.launcher.permission.UNINSTALL_SHORTCUT'
uses-permission: name='android.permission.PERSISTENT_ACTIVITY'
uses-permission: name='android.permission.WRITE_SETTINGS'
uses-permission: name='android.permission.SEND_SMS'
uses-permission: name='android.permission.READ_SMS'
uses-permission: name='android.permission.GET_TASKS'
uses-permission: name='com.tencent.permission.VIRUS_SCAN'
uses-permission: name='android.permission.READ_LOGS'
uses-permission: name='android.permission.READ_CONTACTS'
uses-permission: name='android.permission.FLASHLIGHT'
uses-permission: name='android.permission.BLUETOOTH'
uses-permission: name='android.permission.BLUETOOTH_ADMIN'
uses-permission: name='android.permission.BROADCAST_STICKY'
uses-permission: name='android.permission.WRITE_CONTACTS'
uses-permission: name='android.permission.WRITE_OWNER_DATA'
uses-permission: name='android.permission.SYSTEM_OVERLAY_WINDOW'
uses-permission: name='android.permission.CHANGE_NETWORK_STATE'
uses-permission: name='com.android.launcher.permission.WRITE_SETTINGS'
uses-permission: name='com.android.launcher3.permission.READ_SETTINGS'
uses-permission: name='com.android.launcher3.permission.WRITE_SETTINGS'
uses-permission: name='com.htc.launcher.permission.READ_SETTINGS'
uses-permission: name='com.htc.launcher.permission.WRITE_SETTINGS'
uses-permission: name='com.huawei.launcher3.permission.READ_SETTINGS'
uses-permission: name='com.google.android.launcher.permission.READ_SETTINGS'
uses-permission: name='com.google.android.launcher.permission.WRITE_SETTINGS'
uses-permission: name='android.permission.READ_CALENDAR'
uses-permission: name='android.permission.WRITE_CALENDAR'
uses-permission: name='com.sonyericsson.home.permission.BROADCAST_BADGE'
uses-permission: name='com.sec.android.provider.badge.permission.READ'
uses-permission: name='com.sec.android.provider.badge.permission.WRITE'
uses-permission: name='android.permission.GET_ACCOUNTS'
uses-permission: name='android.permission.MANAGE_ACCOUNTS'
uses-permission: name='android.permission.AUTHENTICATE_ACCOUNTS'
uses-permission: name='android.permission.WRITE_CONTACTS'
uses-permission: name='android.permission.READ_SYNC_SETTINGS'
uses-permission: name='android.permission.WRITE_SYNC_SETTINGS'
uses-permission: name='android.permission.DISABLE_KEYGUARD'
uses-permission: name='android.permission.CHANGE_WIFI_MULTICAST_STATE'
uses-permission: name='com.qq.qcloud.permission.ACCESS_ALBUM_BACKUP_LIST'
uses-permission: name='com.android.vending.BILLING'
uses-permission: name='android.permission.RESTART_PACKAGES'
uses-permission: name='android.permission.NFC'
application-label:'QQi'
application-label-de:'QQi'
application-label-es:'QQi'
application-label-fr:'QQi'
application-label-ja:'QQi'
application-label-ko:'QQi'
application-label-zh-CN:'QQi'
application-label-zh-TW:'QQi'
application-icon-120:'R/m/hcw.png'
application-icon-160:'R/m/hcw.png'
application-icon-240:'R/m/hcw.png'
application-icon-320:'R/m/hcw.png'
application-icon-480:'R/m/hcw.png'
application-icon-65535:'R/m/hcw.png'
application: label='QQi' icon='R/m/hcw.png'
launchable-activity: name='com.tencent.mobileqq.activity.SplashActivity' label='QQi' icon=''
uses-library-not-required:'com.google.android.media.effects'
uses-library-not-required:'com.motorola.hardware.frontcamera'
uses-permission: name='com.tencent.photos.permission.DATA'
uses-permission: name='com.tencent.msf.permission.account.sync'
uses-permission: name='com.tencent.music.data.permission'
uses-permission: name='com.tencent.msf.permission.ACCOUNT_NOTICE'
uses-permission: name='android.permission.CHANGE_WIFI_STATE'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.ACCESS_WIFI_STATE'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.ACCESS_FINE_LOCATION'
uses-permission: name='android.permission.ACCESS_COARSE_LOCATION'
uses-permission: name='android.permission.CAMERA'
uses-permission: name='android.permission.READ_PHONE_STATE'
uses-permission: name='android.permission.WAKE_LOCK'
uses-permission: name='com.android.launcher.permission.INSTALL_SHORTCUT'
uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.RECEIVE_BOOT_COMPLETED'
uses-permission: name='com.tencent.msg.permission.pushnotify'
uses-permission: name='com.tencent.msf.permission.account.sync'
uses-permission: name='android.permission.READ_EXTERNAL_STORAGE'
uses-implied-permission: name='android.permission.READ_EXTERNAL_STORAGE' reason='requested WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.READ_CALL_LOG'
uses-implied-permission: name='android.permission.READ_CALL_LOG' reason='targetSdkVersion < 16 and requested READ_CONTACTS'
uses-permission: name='android.permission.WRITE_CALL_LOG'
uses-implied-permission: name='android.permission.WRITE_CALL_LOG' reason='targetSdkVersion < 16 and requested WRITE_CONTACTS'
feature-group: label=''
uses-feature-not-required: name='android.hardware.camera'
uses-feature-not-required: name='android.hardware.camera.autofocus'
uses-feature-not-required: name='android.hardware.location'
uses-feature-not-required: name='android.hardware.location.gps'
uses-feature-not-required: name='android.hardware.location.network'
uses-feature-not-required: name='android.hardware.telephony'
uses-feature: name='android.hardware.bluetooth'
uses-implied-feature: name='android.hardware.bluetooth' reason='requested android.permission.BLUETOOTH permission, requested android.permission.BLUETOOTH_ADMIN permission, and targetSdkVersion > 4'
uses-feature: name='android.hardware.faketouch'
uses-implied-feature: name='android.hardware.faketouch' reason='default feature for all apps'
uses-feature: name='android.hardware.microphone'
uses-implied-feature: name='android.hardware.microphone' reason='requested android.permission.RECORD_AUDIO permission'
uses-feature: name='android.hardware.screen.portrait'
uses-implied-feature: name='android.hardware.screen.portrait' reason='one or more activities have specified a portrait orientation'
uses-feature: name='android.hardware.wifi'
uses-implied-feature: name='android.hardware.wifi' reason='requested android.permission.ACCESS_WIFI_STATE permission, requested android.permission.CHANGE_WIFI_MULTICAST_STATE permission, and requested android.permission.CHANGE_WIFI_STATE permission'
main
other-activities
other-receivers
other-services
supports-screens: 'small' 'normal' 'large' 'xlarge'
supports-any-density: 'true'
locales: '--_--' 'de' 'es' 'fr' 'ja' 'ko' 'zh-CN' 'zh-TW'
densities: '120' '160' '240' '320' '480' '65535'
native-code: 'armeabi'
- 需要的重要信息为:
python
name='com.tencent.mobileqqi'
launchable-activity: name='com.tencent.mobileqq.activity.SplashActivity'
- 如果
aapt
提示找不到命令,需要把它的安装路径加入到系统环境变量中,aapt
的路径在:
python
D:\android-sdk-windows\build-tools\29.0.3
6 Capabilities详解
6.1 Capabilities介绍
-
Capabilities
是启动appium
服务端的参数设置; -
关于
Capabilities
可以参考官方文档:Appium capabilities; -
以下重点了解几个
Capabilities
参数。
6.2 automationName
automationName
表示自动化测试引擎,好比测试驱动一样;- 可供选择的有
Appium、Selendriod、uiautomator2
等; - 本文及后续都使用的是
uiautomator2
。
6.3 platformName
platformName
表示手机或模拟器使用的操作系统;- 一般包括
iOS、Android
等; - 本文使用
Android
。
6.4 platformVersion
platformVersion
表示真机或模拟器操作系统的版本;- 本文为
Android6.0
。
6.5 deviceName
deviceName
表示使用的测试设备类型;- 可以使用
adb devices
查看设备类型名称:
python
C:\Windows\System32>adb devices
List of devices attached
emulator-5554 device
- 比如我的是
emulator-5554
。
6.6 app
app
表示安装包;- 如果指定这个参数就会把指定的
app
安装到指定的路径。
6.7 newCommandTimeout
newCommandTimeout
表示两条Appium
命令间的最长时间间隔;- 如果超时,会自动退出
app
。
6.8 noReset
noReset
表示不需要在会话前重置应用状态;- 默认为
false
。
6.9 fullReset
fullReset
可通过卸载而不清空数据来重置应用状态;- 默认为
false
。
6.10 appPackage和appActivity
- 启动待测试
app
的appPackage
和appActivity
; appPackage
是app的包名;appActivity
是app
的活动窗口名;- 这两个参数的获取已经在第五章节说明了。
还有几个参数,这个不一一说明了,直接看官方文档即可。
7 官方示例
- 以下这个是官方的一个示例,是
python
版本的;
python
import unittest
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
capabilities = dict(
platformName='Android',
automationName='uiautomator2',
deviceName='Android',
appPackage='com.android.settings',
appActivity='.Settings',
language='en',
locale='US'
)
appium_server_url = 'http://localhost:4723'
class TestAppium(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
def tearDown(self) -> None:
if self.driver:
self.driver.quit()
def test_find_battery(self) -> None:
el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
el.click()
if __name__ == '__main__':
unittest.main()
8 测试示例
- 这里我们写一个我们自己的测试示例;
- 之前我们已经安装了测试对象
扣扣
,且已经获取到了它的相关信息; - 我们启动
appium
服务,直接在命令行输入appium
即可: - 设计如下脚本:
python
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/12/12
# 文件名称:test_qq.py
# 作用:appium启动一个应用程序
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
import time
capabilities = dict(
platformName='Android',
automationName='uiautomator2',
deviceName='emulator-5554',
appPackage='com.tencent.mobileqqi',
appActivity='com.tencent.mobileqq.activity.SplashActivity',
language='en',
locale='US'
)
print("start....")
# 创建driver对象
appium_server_url = 'http://localhost:4723'
driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
# 启动应用程
print(driver.capabilities)
# driver.launch_app()
time.sleep(1)
print("已经连接到模拟器了~")
driver.quit()
- 执行脚本:
python
D:\Python37\python.exe F:/python_study/appium_test/test_qq.py
start....
{'platformName': 'Android',
'automationName': 'uiautomator2',
'deviceName': 'emulator-5554',
'appPackage': 'com.tencent.mobileqqi',
'appActivity': 'com.tencent.mobileqq.activity.SplashActivity',
'language': 'en',
'locale': 'US',
'platform': 'LINUX',
'webStorageEnabled': False,
'takesScreenshot': True,
'javascriptEnabled': True,
'databaseEnabled': False,
'networkConnectionEnabled': True,
'locationContextEnabled': False,
'warnings': {},
'desired': {'platformName': 'Android', 'automationName': 'uiautomator2', 'deviceName': 'emulator-5554', 'appPackage': 'com.tencent.mobileqqi', 'appActivity': 'com.tencent.mobileqq.activity.SplashActivity', 'language': 'en', 'locale': 'US'},
'deviceUDID': 'emulator-5554', 'pixelRatio': '2', 'statBarHeight': 48, 'viewportRect': {'left': 0, 'top': 48, 'width': 768, 'height': 1136},
'deviceApiLevel': 23, 'platformVersion': '6.0', 'deviceManufacturer': 'unknown', 'deviceModel': 'sdk_phone_armv7', 'deviceScreenSize': '768x1280', 'deviceScreenDensity': 320}
已经连接到模拟器了~
- 这时候
appium
的服务端也给出了启动信息:
python
[AndroidUiautomator2Driver@455c (f190abfc)] Proxying [DELETE /] to [DELETE http://127.0.0.1:8200/session/f9cc13f6-4c4e-4ab5-a2eb-8dfb44f05100] with no body
[AndroidUiautomator2Driver@455c (f190abfc)] Got response with status 200: {"sessionId":"f9cc13f6-4c4e-4ab5-a2eb-8dfb44f05100","value":null}
[ADB] Running 'D:\android-sdk-windows\platform-tools\adb.exe -P 5037 -s emulator-5554 shell am force-stop com.tencent.mobileqqi'
[Instrumentation] .
[Instrumentation] Time: 74.226
[Instrumentation]
[Instrumentation] OK (1 test)
- 同时模拟器中也打开了
扣扣
应用程序。
9 启动问题排查
9.1 urllib3 v2.0 only supports OpenSSL 1.1.1+
- 如果启动过程出现这个错误:
python
DEPRECATION: celery 5.0.5 has a non-standard dependency specifier pytz>dev. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of celery or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063
- 则说明
Python
环境使用的是OpenSSL 1.1.0h
,而urllib3 v2.0
需要使用OpenSSL 1.1.1
; - 解决方法是:
python
# 要么升级 OpenSSL 到 1.1.1 或更高版本。
# 要么降级 urllib3 到 v1.x 版本,可以通过命令 pip install urllib3==1.* 来实现。
- 本文使用降级
urllib3
来解决的,但是可能会导致其他依赖包问题。
python
pip install urllib3==1.26.15
9.2 UnknownError: An unknown server-side error occurred
- 如果出现以下错误:
python
UnknownError: An unknown server-side error occurred while processing the command. Original error: Error executing adbExec. Original error: 'Command 'D:\\android-sdk-windows\\platform-tools\\adb.exe -P 5037 -s emulator-5554 shell pm install -r /data/local/tmp/appium_cache/72200c7819db015d5717d05800401c19b35842d5.apk' timed out after 20000ms'. Try to increase the 20000ms adb execution timeout represented by 'uiautomator2ServerInstallTimeout' capability
- 说明
adb
安装appium
服务的时候报错; - 解决方法是先卸载:
python
adb uninstall /data/local/tmp/appium_cache/72200c7819db015d5717d05800401c19b35842d5.apk
- 再原样使用命令安装即可:
python
adb -P 5037 -s emulator-5554 shell pm install -r /data/local/tmp/appium_cache/72200c7819db015d5717d05800401c19b35842d5.apk