Appium 使用 automationName 框架为 uiautomator2 识别元素失败。切换 automationName 框架为 Appium 识别元素成功,求助!

lei___ · 2018年05月25日 · 最后由 mc_stone 回复于 2018年09月06日 · 3830 次阅读

问题:
使用 https://testerhome.com/topics/11720 的框架,通过 uiautomator2 识别元素,失败。

经过简单的查看日志,在页面更新后,需要刷新页面缓存才对,实际上切换 uiautomator2 框架没有这个操作。

操作
工程代码: https://github.com/leila-wu/pythonAppium 。执行 Runner 下的 runner.py,调用 HomeTest.py 执行用例。

日志与描述:
切换 desired_caps['automationName'] = 'Appium'。识别元素成功。 重点看到Failed to locate element. Clearing Accessibility cache and retrying.。取值失败,有清除缓存处理

该场景的其他元素获取正常,需要重新识别的元素,是页面小范围刷新。比如点击 A 按钮新增的 B 展示后,元素 B 获取失败

[debug] [W3C] Calling AppiumDriver.findElement() with args: ["id","com.thinkive.future.dev.standard:id/tv_optional_edit","6757fcaa-b7b8-4ccf-8486-2d538f86d617"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [AndroidBootstrap] Sending command to android: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}}
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: find
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.thinkive.future.dev.standard:id/tv_optional_edit' using 'ID' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.thinkive.future.dev.standard:id/tv_optional_edit]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding 'com.thinkive.future.dev.standard:id/tv_optional_edit' using 'ID' with the contextId: '' multiple: false
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.thinkive.future.dev.standard:id/tv_optional_edit]
[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"status":0,"value":{"ELEMENT":"13"}}
[debug] [AndroidBootstrap] Received command result from bootstrap
[debug] [W3C] Responding to client with driver.findElement() result: {"ELEMENT":"13"}
[HTTP] <-- POST /wd/hub/session/6757fcaa-b7b8-4ccf-8486-2d538f86d617/element 200 214 ms - 88

切换 desired_caps['automationName'] = 'uiautomator2'。元素识别失败(端口随机处理。)

[debug] [W3C] Calling AppiumDriver.findElement() with args: ["id","com.thinkive.future.dev.standard:id/tv_optional_edit","8c510efb-4426-45bc-84eb-971441239f1e"]
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, class name, accessibility id, -android uiautomator
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [JSONWP Proxy] Proxying [POST /element] to [POST http://localhost:4754/wd/hub/session/b6d92bbf-10c9-475b-b382-ffb975645773/element] with body: {"strategy":"id","selector":"com.thinkive.future.dev.standard:id/tv_optional_edit","context":"","multiple":false}
[HTTP] <-- POST /wd/hub/session/8c510efb-4426-45bc-84eb-971441239f1e/element 404 28 ms - 1119
[HTTP] 

检查代码

WebDriverWait(self.driver, t).until(lambda x: self.elements_by(mOperate))  # 操作元素是否存在
# 封装常用的标签
    def elements_by(self, mOperate):
        elements = {
            BE.find_element_by_id: lambda: self.driver.find_element_by_id(mOperate["element_info"]),
            BE.find_element_by_xpath: lambda: self.driver.find_element_by_xpath(mOperate["element_info"]),
            BE.find_element_by_css_selector: lambda: self.driver.find_element_by_css_selector(mOperate['element_info']),
            BE.find_element_by_class_name: lambda: self.driver.find_element_by_class_name(mOperate['element_info']),
            BE.find_elements_by_id: lambda: self.driver.find_elements_by_id(mOperate['element_info']),

        }
        return elements[mOperate["find_type"]]()

脚本配置:

- element_info: com.thinkive.future.dev.standard:id/tv_optional_edit
  find_type: id
  operate_type: click
  info: 点击编辑按钮
共收到 1 条回复 时间 点赞
lei___ Appium 开源分享优化版 中提及了此贴 05月30日 20:12

使用 automationName 框架为 uiautomator2 报错了,大佬帮帮忙

[Appium] Welcome to Appium v1.7.2
[Appium] Non-default server args:
[Appium] address: 127.0.0.1
[Appium] Appium REST http interface listener started on 127.0.0.1:4723
[HTTP] --> POST /wd/hub/session
[HTTP] {"capabilities":{"firstMatch":[{}],"alwaysMatch":{"platformName":"Android"}},"desiredCapabilities":{"deviceName":"127.0.0.1:21503","appActivity":"com.tal.kaoyan.ui.activity.SplashActivity","appPackage":"com.tal.kaoyan","automationName":"uiautomator2","platformName":"Android","platforVersion":"5.1.1"}}
[debug] [MJSONWP] Calling AppiumDriver.createSession() with args: [{"deviceName":"127.0.0.1:21503","appActivity":"com.tal.kaoyan.ui.activity.SplashActivity","appPackage":"com.tal.kaoyan","automationName":"uiautomator2","platformName":"Android","platforVersion":"5.1.1"},null,{"firstMatch":[{}],"alwaysMatch":{"platformName":"Android"}}]
[debug] [BaseDriver] Event 'newSessionRequested' logged at 1536205623359 (11:47:03 GMT+0800 (中国标准时间))
[Appium] Merged W3C capabilities {"firstMatch":[{}],"alwaysMatch":{"platformName... into desiredCapabilities object {"deviceName":"127.0.0.1:21503","appActivity":"...
[Appium] Creating new AndroidUiautomator2Driver (v0.11.0) session
[Appium] Capabilities:
[Appium] deviceName: 127.0.0.1:21503
[Appium] appActivity: com.tal.kaoyan.ui.activity.SplashActivity
[Appium] appPackage: com.tal.kaoyan
[Appium] automationName: uiautomator2
[Appium] platformName: Android
[Appium] platforVersion: 5.1.1
[debug] [BaseDriver] W3C capabilities [] and MJSONWP desired capabilities were provided
[BaseDriver] Falling back to MJSONWP desired capabilities
[BaseDriver] The following capabilities were provided, but are not recognized by appium: platforVersion.
[BaseDriver] Session created with session id: 9d54b724-68d0-46b6-960e-41f17672e99d
[debug] [AndroidDriver] Getting Java version
[AndroidDriver] Java version is: 1.8.0_131
[ADB] Checking whether adb is present
[ADB] Found 5 'build-tools' folders under 'D:\appium_test\sdk' (newest first):
[ADB] D:/appium_test/sdk/build-tools/28.0.0
[ADB] D:/appium_test/sdk/build-tools/22.0.1
[ADB] D:/appium_test/sdk/build-tools/21.1.2
[ADB] D:/appium_test/sdk/build-tools/20.0.0
[ADB] D:/appium_test/sdk/build-tools/19.1.0
[ADB] Using adb.exe from D:\appium_test\sdk\platform-tools\adb.exe
[AndroidDriver] Retrieving device list
[debug] [ADB] Trying to find a connected android device
[debug] [ADB] Getting connected devices...
[debug] [ADB] 1 device(s) connected
[AndroidDriver] Using device: 127.0.0.1:21503
[debug] [ADB] Setting device id to 127.0.0.1:21503
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell getprop ro.build.version.sdk'
[debug] [ADB] Current device property 'ro.build.version.sdk': 22
[debug] [ADB] Device API level: 22
[AndroidDriver] No app sent in, not parsing package/activity
[ADB] Getting device platform version
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell getprop ro.build.version.release'
[debug] [ADB] Current device property 'ro.build.version.release': 5.1.1
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell wm size'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell wm density'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell getprop ro.product.model'
[debug] [ADB] Current device property 'ro.product.model': SM-G9350
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell getprop ro.product.manufacturer'
[debug] [ADB] Current device property 'ro.product.manufacturer': samsung
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell am force-stop io.appium.uiautomator2.server'
[ADB] Checking whether aapt is present
[ADB] Using aapt.exe from D:\appium_test\sdk\build-tools\28.0.0\aapt.exe
[debug] [ADB] Getting install status for io.appium.uiautomator2.server
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell pm list packages io.appium.uiautomator2.server'
[debug] [ADB] App is installed
[debug] [ADB] Getting install status for io.appium.uiautomator2.server.test
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell pm list packages io.appium.uiautomator2.server.test'
[debug] [ADB] App is not installed
[debug] [ADB] Getting package info for 'io.appium.uiautomator2.server'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell dumpsys package io.appium.uiautomator2.server'
[debug] [UiAutomator2] Server installed but version 0.1.9 instead of 0.3.0. Re-installing
[debug] [ADB] Uninstalling io.appium.uiautomator2.server
[debug] [ADB] Getting install status for io.appium.uiautomator2.server
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell pm list packages io.appium.uiautomator2.server'
[debug] [ADB] App is installed
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell am force-stop io.appium.uiautomator2.server'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 uninstall io.appium.uiautomator2.server'
[debug] [ADB] 'adb uninstall io.appium.uiautomator2.server' command output: Success
[ADB] io.appium.uiautomator2.server was successfully uninstalled
[debug] [ADB] Uninstalling io.appium.uiautomator2.server.test
[debug] [ADB] Getting install status for io.appium.uiautomator2.server.test
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell pm list packages io.appium.uiautomator2.server.test'
[debug] [ADB] App is installed
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell am force-stop io.appium.uiautomator2.server.test'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 uninstall io.appium.uiautomator2.server.test'
[debug] [ADB] 'adb uninstall io.appium.uiautomator2.server.test' command output: Success
[ADB] io.appium.uiautomator2.server.test was successfully uninstalled
[debug] [ADB] Checking app cert for C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_appium-uiautomator2-driver@0.11.0@appium-uiautomator2-driver\uiautomator2\appium-uiautomator2-server-v0.3.0.apk
[ADB] Using apksigner.bat from D:\appium_test\sdk\build-tools\28.0.0\apksigner.bat
[debug] [ADB] Starting 'D:\appium_test\sdk\build-tools\28.0.0\apksigner.bat' with args '["verify","--print-certs","C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules\_appium-uiautomator2-driver@0.11.0@appium-uiautomator2-driver\uiautomator2\appium-uiautomator2-server-v0.3.0.apk"]'
[debug] [ADB] apksigner stdout: Signer #1 certificate DN: EMAILADDRESS=android@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
[debug] [ADB] Signer #1 certificate SHA-256 digest: a40da80a59d170caa950cf15c18c454d47a39b26989d8b640ecd745ba71bf5dc
[debug] [ADB] Signer #1 certificate SHA-1 digest: 61ed377e85d386a8dfee6b864bd85b0bfaa5af81
[debug] [ADB] Signer #1 certificate MD5 digest: e89b158e4bcf988ebd09eb83f5378e87
[debug] [ADB] WARNING: META-INF/INDEX.LIST not protected by signature. Unauthorized modifications to this JAR entry will not be detected. Delete or move the entry outside of META-INF/.
[debug] [ADB] WARNING: META-INF/io.netty.versions.properties not protected by signature. Unauthorized modifications to this JAR entry will not be detected. Delete or move the entry outside of META-INF/.
[debug] [ADB]
[debug] [ADB] 'C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_appium-uiautomator2-driver@0.11.0@appium-uiautomator2-driver\uiautomator2\appium-uiautomator2-server-v0.3.0.apk' is already signed.
[debug] [UiAutomator2] Deleting UiAutomator2 session
[debug] [UiAutomator2] Deleting UiAutomator2 server session
[UiAutomator2] Did not get confirmation UiAutomator2 deleteSession worked; Error was: UnknownError: An unknown server-side error occurred while processing the command. Original error: Trying to proxy a session command without session id
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 shell am force-stop com.tal.kaoyan'
[debug] [ADB] Removing forwarded port socket connection: 8200
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 forward --remove tcp:8200'
[debug] [ADB] Running 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 forward --remove tcp:8200'
[UiAutomator2] Unable to remove port forward 'Error executing adbExec. Original error: 'Command 'D:\appium_test\sdk\platform-tools\adb.exe -P 5037 -s 127.0.0.1:21503 forward --remove tcp:8200' exited with code 1'; Stderr: 'error: listener 'tcp:8200' not found'; Code: '1''
[MJSONWP] Encountered internal error running command: TypeError: Cannot create property 'replace' on boolean 'true'
[MJSONWP] at ADB.callee$0$0$ (../../../lib/tools/apk-utils.js:367:20)
[MJSONWP] at tryCatch (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
[MJSONWP] at GeneratorFunctionPrototype.invoke as _invoke
[MJSONWP] at GeneratorFunctionPrototype.prototype.(anonymous function) as next
[MJSONWP] at invoke (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
[MJSONWP] at enqueueResult (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:185:17)
[MJSONWP] at Promise.F (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_core-js@1.2.7@core-js\library\modules\$.export.js:30:36)
[MJSONWP] at AsyncIterator.enqueue (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:184:12)
[MJSONWP] at AsyncIterator.prototype.(anonymous function) as next
[MJSONWP] at Object.runtime.async (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:209:12)
[MJSONWP] at ADB.callee$0$0 as install
[MJSONWP] at UiAutomator2Server.signAndInstall$ (../../lib/uiautomator2.js:109:20)
[MJSONWP] at tryCatch (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:67:40)
[MJSONWP] at GeneratorFunctionPrototype.invoke as _invoke
[MJSONWP] at GeneratorFunctionPrototype.prototype.(anonymous function) as next
[MJSONWP] at GeneratorFunctionPrototype.invoke (C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\node_modules_babel-runtime@5.8.24@babel-runtime\regenerator\runtime.js:136:37)
[HTTP] <-- POST /wd/hub/session 500 6872 ms - 187
[HTTP]

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册