appium
以及安装过程中相关问题的排查;appium
的服务看看: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
子命令管理所有的驱动;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]
uiautomator2
,其他的按照情况来安装即可。appium driver install uiautomator2
命令安装,但是报错了: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'"
# 1.查看npm镜像设置
npm config get registry
# 2.将npm设置为淘宝镜像
npm config set registry https://registry.npm.taobao.org
# 3.再次查看npm镜像设置
npm config get registry
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"]
Python Client
针对不同的语言有不同的安装方法,我们使用的python
版本的;Appium Python Client
:pip install Appium-Python-Client
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
扣扣
,因为我们模拟器是安卓6.0的;扣扣
传到模拟器中,本地保存在如下:F:\monkey_test\QQv5.apk
adb uninstall com.tencent.mobileqqi
F:\monkey_test>adb uninstall com.tencent.mobileqqi
Success
adb install QQv5.apk /data/temp
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
当前app的包名和活动窗口名
;app
;APP
后在命令行输入:adb shell dumpsys | find "mFocusedActivity"
mFocusedActivity: ActivityRecord{54035c5 u0 com.tencent.mobileqqi/com.tencent.mobileqq.activity.InstallActivity t4}
# 包名:com.tencent.mobileqqi
# 活动窗口名:com.tencent.mobileqq.activity.LoginActivity
AndroidKiller
主要是对apk
进行反编译操作获取相关信息;F:\monkey_test
下:aapt dump badging QQv5.apk
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'
name='com.tencent.mobileqqi'
launchable-activity: name='com.tencent.mobileqq.activity.SplashActivity'
aapt
提示找不到命令,需要把它的安装路径加入到系统环境变量中,aapt
的路径在:D:\android-sdk-windows\build-tools\29.0.3
Capabilities
是启动appium
服务端的参数设置;
关于Capabilities
可以参考官方文档:Appium capabilities;
以下重点了解几个Capabilities
参数。
automationName
表示自动化测试引擎,好比测试驱动一样;Appium、Selendriod、uiautomator2
等;uiautomator2
。platformName
表示手机或模拟器使用的操作系统;iOS、Android
等;Android
。platformVersion
表示真机或模拟器操作系统的版本;Android6.0
。deviceName
表示使用的测试设备类型;adb devices
查看设备类型名称:C:\Windows\System32>adb devices
List of devices attached
emulator-5554 device
emulator-5554
。app
表示安装包;app
安装到指定的路径。newCommandTimeout
表示两条Appium
命令间的最长时间间隔;app
。noReset
表示不需要在会话前重置应用状态;false
。fullReset
可通过卸载而不清空数据来重置应用状态;false
。app
的appPackage
和appActivity
;appPackage
是app的包名;appActivity
是app
的活动窗口名;还有几个参数,这个不一一说明了,直接看官方文档即可。
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()
扣扣
,且已经获取到了它的相关信息;appium
服务,直接在命令行输入appium
即可:
# -*- 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()
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
的服务端也给出了启动信息:[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)
扣扣
应用程序。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
;# 要么升级 OpenSSL 到 1.1.1 或更高版本。
# 要么降级 urllib3 到 v1.x 版本,可以通过命令 pip install urllib3==1.* 来实现。
urllib3
来解决的,但是可能会导致其他依赖包问题。pip install urllib3==1.26.15
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
服务的时候报错;adb uninstall /data/local/tmp/appium_cache/72200c7819db015d5717d05800401c19b35842d5.apk
adb -P 5037 -s emulator-5554 shell pm install -r /data/local/tmp/appium_cache/72200c7819db015d5717d05800401c19b35842d5.apk