appium ConnectionRefusedError,urllib3.exceptions.NewConnectionError,urllib3.exceptions.MaxRetryError踩坑 报错关键信息
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x0000014A42376898>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=9904): Max retries exceeded with url: /wd/hub/session (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0000014A42376898>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。'))
报错的代码是
print(f"即将启动appium, 当前设备ID: {device_id}, 数据变量ID: {data_id}, 端口号: {port}")
print(f'appium -p {port}')
subprocess.Popen('appium -p %s' % port, shell=True)
# subprocess.call('appium -p %s' % port, shell=True)
# subprocess.call相当于subprocess.Popen().wait()
print('----appium server已启动----')
# time.sleep(5)
d_capabilities = {
"platformName": "Android",
"deviceName": "127.0.0.1:7555",
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI"
}
driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % port, d_capabilities)
先说报错原因 subprocess创建子进程来启动appium server,appium启动需要时间,这个可以在命令启动看一下,会有好多日志,也就是说启动appium其实做了很多事(后面会写一篇启动和连接日志解析),但是当前主进程不会停下来,执行完subprocess.Popen这一行不等待立马就往下走了,然后走到webdriver.Remote这一行去连接appium server,可是此时appium还没启动完成,然后就抛出上面的异常堆栈
预防办法 这里要补充一下subprocess库的知识点 subprocess.Popen是创建一个异步非阻塞的子进程然后就不管了,主子进程之间不需要交互通信,可以说是创建完子进程就任其自生自灭。上面的代码执行起来是这样的
D:\Python37\python.exe D:/myproject/yiyan_v/fs/case/t4.py
现在在执行t4脚本
即将启动appium, 当前设备ID: 3, 数据变量ID: 2, 端口号: 9903
appium -p 9903
----appium server已启动----
[Appium] Welcome to Appium v1.22.3
[Appium] Non-default server args:
[Appium] port: 9903
[Appium] Appium REST http interface listener started on 0.0.0.0:9903
[debug] [HTTP] Request idempotency key: ed2388e5-35fe-415f-b61b-0c41251ca71b
[HTTP] --> POST /wd/hub/session
[HTTP] {"capabilities":{"alwaysMatch":{"platformName":"Android","appium:deviceName":"127.0.0.1:7555","appium:appPackage":"com.tencent.mm","appium:appActivity":".ui.LauncherUI"},"firstMatch":[{}]},"desiredCapabilities":{"platformName":"Android","deviceName":"127.0.0.1:7555","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI"}}
[debug] [W3C] Calling AppiumDriver.createSession() with args: [{"platformName":"Android","deviceName":"127.0.0.1:7555","appPackage":"com.tencent.mm","appActivity":".ui.LauncherUI"},null,{"alwaysMatch":{"platformName":"Android","appium:deviceName":"127.0.0.1:7555","appium:appPackage":"com.tencent.mm","appium:appActivity":".ui.LauncherUI"},"firstMatch":[{}]}]
[debug] [BaseDriver] Event 'newSessionRequested' logged at 1649387378713 (11:09:38 GMT+0800 (GMT+08:00))
[Appium]
[Appium] ======================================================================
[Appium] DEPRECATION WARNING:
[Appium]
[Appium] The 'automationName' capability was not provided in the desired
[Appium] capabilities for this Android session
[Appium]
[Appium] Setting 'automationName=UiAutomator2' by default and using the
[Appium] UiAutomator2 Driver
[Appium]
[Appium] The next major version of Appium (2.x) will **require** the
[Appium] 'automationName' capability to be set for all sessions on all
[Appium] platforms
[Appium]
[Appium] In previous versions (Appium <= 1.13.x), the default was
[Appium] 'automationName=UiAutomator1'
[Appium]
[Appium] If you wish to use that automation instead of UiAutomator2, please
[Appium] add 'automationName=UiAutomator1' to your desired capabilities
[Appium]
[Appium] For more information about drivers, please visit
[Appium] http://appium.io/docs/en/about-appium/intro/ and explore the
[Appium] 'Drivers' menu
[Appium]
[Appium] ======================================================================
[Appium]
[Appium] Appium v1.22.3 creating new AndroidUiautomator2Driver (v1.70.1) session
[debug] [BaseDriver] W3C capabilities and MJSONWP desired capabilities were provided
[debug] [BaseDriver] Creating session with W3C capabilities: {
[debug] [BaseDriver] "alwaysMatch": {
[debug] [BaseDriver] "platformName": "Android",
[debug] [BaseDriver] "appium:deviceName": "127.0.0.1:7555",
[debug] [BaseDriver] "appium:appPackage": "com.tencent.mm",
[debug] [BaseDriver] "appium:appActivity": ".ui.LauncherUI"
[debug] [BaseDriver] },
[debug] [BaseDriver] "firstMatch": [
[debug] [BaseDriver] {}
[debug] [BaseDriver] ]
[debug] [BaseDriver] }
[BaseDriver] Session created with session id: 6e0647ae-0ed0-4c2e-ac05-5df81598102b
[UiAutomator2] Starting 'com.tencent.mm' directly on the device
[debug] [UiAutomator2] Deleting UiAutomator2 session
[debug] [BaseDriver] Event 'newSessionStarted' logged at 1649387382016 (11:09:42 GMT+0800 (GMT+08:00))
[debug] [W3C] Encountered internal error running command: Error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
[debug] [W3C] at requireSdkRoot (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-adb\lib\helpers.js:36:11)
[debug] [W3C] at Function.createADB (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-adb\lib\adb.js:57:23)
[debug] [W3C] at Object.createBaseADB (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-android-driver\lib\android-helpers.js:129:20)
[debug] [W3C] at Object.getDeviceInfoFromCaps (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-android-driver\lib\android-helpers.js:214:29)
[debug] [W3C] at AndroidUiautomator2Driver.startUiAutomator2Session (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-uiautomator2-driver\lib\driver.js:338:40)
[debug] [W3C] at AndroidUiautomator2Driver.createSession (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-uiautomator2-driver\lib\driver.js:229:18)
[debug] [W3C] at processTicksAndRejections (internal/process/task_queues.js:97:5)
[debug] [W3C] at AppiumDriver.createSession (D:\Program Files\nodejs\node_global\node_modules\appium\lib\appium.js:387:35)
Traceback (most recent call last):
File "D:/myproject/yiyan_v/fs/case/t4.py", line 36, in <module>
driver = webdriver.Remote('http://127.0.0.1:%s/wd/hub' % port, d_capabilities)
File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 275, in __init__
[HTTP] <-- POST /wd/hub/session 500 3557 ms - 875
[HTTP]
AppiumConnection(command_executor, keep_alive=keep_alive), desired_capabilities, browser_profile, proxy
File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 269, in __init__
self.start_session(capabilities, browser_profile)
File "D:\Python37\lib\site-packages\appium\webdriver\webdriver.py", line 369, in start_session
response = self.execute(RemoteCommand.NEW_SESSION, parameters)
File "D:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 425, in execute
self.error_handler.check_response(response)
File "D:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
Stacktrace:
UnknownError: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported. Read https://developer.android.com/studio/command-line/variables for more details
at getResponseForW3CError (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:804:9)
at asyncHandler (D:\Program Files\nodejs\node_global\node_modules\appium\node_modules\appium-base-driver\lib\protocol\protocol.js:380:37)
这里会看到一个关键信息
UnknownError: An unknown server-side error occurred while processing the command. Original error: Neither ANDROID_HOME nor ANDROID_SDK_ROOT environment variable was exported.
如果你在安装appium时有用appium-doctor来检查并且显示OK,那么你就不要找错方向,自信点, ANDROID_HOME 和 ANDROID_SDK_ROOT 环境都没问题! 我猜测可能是因为appium还没完成启动,android和java环境变量还没加载到内存,然后代码就执行到webdriver.Remote了,也就是appium还没完成启动,就有访问的请求进来了,然后appium接收到请求去处理的时候发现java和android环境变量还没找到,所以报了这个错!
subprocess.call是创建一个阻塞的子进程,主进程和子进程之间需要交互通信,主进程会等待子进程的返回(命令执行成功返回0,命令执行报错返回1),通过返回判断后面代码的执行逻辑,所以如果使用subprocess.call来执行,那么执行日志就会看到是这样
现在在执行t4脚本
即将启动appium, 当前设备ID: 3, 数据变量ID: 2, 端口号: 9903
appium -p 9903
[Appium] Welcome to Appium v1.22.3
[Appium] Non-default server args:
[Appium] port: 9903
[Appium] Appium REST http interface listener started on 0.0.0.0:9903
其实两个方式都没问题,如果单机可以直接使用call方法,可能你会问,那以后用例肯定要支持并发执行,所以并发的话还是要使用subprocess.Popen!
|