Appium 1.0.0 版本 find_elements_by_android_uiautomator()方法求助

LW · 2014年05月03日 · 最后由 LW 回复于 2014年05月10日 · 1125 次阅读

import os
import unittest
from appium import webdriver
from time import sleep

Returns abs path relative to this file and not cwd

PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
)

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.2.2'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['app'] = PATH(
'D:/Python/appium/ContactManager.apk'
)
desired_caps['appPackage'] = 'com.example.android.contactmanager'
desired_caps['appActivity'] = '.ContactManager'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_name("Add Contact")
els = driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
print els
driver.quit()

===code 部分

info: Welcome to Appium v1.0.0 (REV f0a00fab2335fa88cb355ab4dc43a9cd3f3236c0)
info: Appium REST http interface listener started on 127.0.0.1:4723
info: socket.io started
info: Non-default server args: {"udid":"HC3BGW9E0305","address":"127.0.0.1","log":"D:\Python\appium\log\2014-05-03.log"}
debug: Appium request initiated at /wd/hub/session
debug: Request received with params: {"sessionId":null,"desiredCapabilities":{"deviceName":"Android Emulator","app":"D:\Python\appium\ContactManager.apk","platformVersion":"4.2.2","appPackage":"com.example.android.contactmanager","platformName":"Android","appActivity":".ContactManager"}}
info: Using local app from desired caps: D:\Python\appium\ContactManager.apk
info: Creating new appium session 96c07e38-8897-4abb-8ff8-d6b0ecb5b552
info: Starting android appium
debug: Using fast reset? true
info: Preparing device for session
info: Checking whether app is actually present
info: Checking whether adb is present
info: [ADB] Using adb from D:\Python\eclipse\sdk\platform-tools\adb.exe
info: Trying to find a connected android device
info: [ADB] Getting connected devices...
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" devices
info: [ADB] 1 device(s) connected
info: Setting device id to HC3BGW9E0305
info: [ADB] Waiting for device to be ready and to respond to shell commands (timeout = 5)
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 wait-for-device
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "echo 'ready'"
info: Starting logcat capture
info: Getting device API level
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "getprop ro.build.version.sdk"
info: Device is at API Level 17
debug: java -jar "C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\lib\devices\android\helpers\strings_from_apk.jar" "D:\Python\appium\ContactManager.apk" "C:\Windows\Temp\com.example.android.contactmanager"
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 push "C:\Windows\Temp\com.example.android.contactmanager\strings.json" /data/local/tmp
info: Checking whether aapt is present
info: [ADB] Using aapt from D:\Python\eclipse\sdk\build-tools\17.0.0\aapt.exe
debug: processFromManifest: "D:\Python\eclipse\sdk\build-tools\17.0.0\aapt.exe" dump xmltree "D:\Python\appium\ContactManager.apk" AndroidManifest.xml
debug: Set app process to: .contactmanager
debug: Not uninstalling app since server not started with --full-reset
debug: Checking app cert for D:\Python\appium\ContactManager.apk: java -jar "C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\lib\devices\android\helpers\verify.jar" "D:\Python\appium\ContactManager.apk"
debug: App already signed.
info: Zip-aligning D:\Python\appium\ContactManager.apk
info: Checking whether zipalign is present
info: [ADB] Using zipalign from D:\Python\eclipse\sdk\tools\zipalign.exe
debug: zipAlignApk: "D:\Python\eclipse\sdk\tools\zipalign.exe" -f 4 "D:\Python\appium\ContactManager.apk" "E:\Temp\appium11443-5860-16vs21k.tmp"
info: MD5 for app is b2d2916bb5388e1dc281ec3e71ef1234
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "ls /data/local/tmp/b2d2916bb5388e1dc281ec3e71ef1234.apk"
debug: Getting install status for com.example.android.contactmanager
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "pm list packages -3 com.example.android.contactmanager"
info: [ADB] App is installed
info: Running fast reset (stop and clear)
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "am force-stop com.example.android.contactmanager"
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "pm clear com.example.android.contactmanager"
info: [ADB] Forwarding system:4724 to device:4724
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 forward tcp:4724 tcp:4724
debug: Pushing appium bootstrap to device...
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 push "C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\build\android_bootstrap\AppiumBootstrap.jar" /data/local/tmp/
debug: Pushing unlock helper app to device...
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 install "C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium\build\unlock_apk\unlock_apk-debug.apk"
info: Attempting to kill all 'uiautomator' processes
info: Getting all processes with 'uiautomator'
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "ps 'uiautomator'"
info: No matching processes found
info: Running bootstrap
debug: spawning: D:\Python\eclipse\sdk\platform-tools\adb.exe -s HC3BGW9E0305 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [BOOTSTRAP] [info] Socket opened on port 4724
info: [BOOTSTRAP] [info] Appium Socket Server Ready
info: [BOOTSTRAP] [info] Loading json...
debug: Waking up device if it's not alive
info: Pushing command to appium work queue: ["wake",{}]
info: [BOOTSTRAP] [info] json loading complete.
info: [BOOTSTRAP] [info] Registered crash watchers.
info: [BOOTSTRAP] [info] Client connected
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"wake","params":{}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: wake
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "dumpsys window"
info: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
info: Writing dumpsys output to C:\Users\Administrator\AppData\Roaming\npm\node_modules\appium.dumpsys.log
debug: Screen already unlocked, continuing.
info: Pushing command to appium work queue: ["getDataDir",{}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"getDataDir","params":{}}
debug: dataDir set to: /data/local/tmp
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "am start -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000 -n com.example.android.contactmanager/.ContactManager"
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: getDataDir
info: [BOOTSTRAP] [info] Returning result: {"value":"\/data\/local\/tmp","status":0}
info: Waiting for activity to be focused
info: Getting focused package and activity
debug: executing: "D:\Python\eclipse\sdk\platform-tools\adb.exe" -s HC3BGW9E0305 shell "dumpsys window windows"
info: Device launched! Ready for commands
info: Setting command timeout to the default of 60 secs
info: Appium session started with sessionId 96c07e38-8897-4abb-8ff8-d6b0ecb5b552
debug: 127.0.0.1 - - "POST /wd/hub/session HTTP/1.1" - 9 "-" "Python-urllib/2.7"
debug: Appium request initiated at /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552
debug: Request received with params: {}
info: Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"Android","platformVersion":"4.2.2","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"warnings":{},"desired":{"deviceName":"Android Emulator","app":"D:\Python\appium\ContactManager.apk","platformVersion":"4.2.2","appPackage":"com.example.android.contactmanager","platformName":"Android","appActivity":".ContactManager"},"deviceName":"Android Emulator","app":"D:\Python\appium\ContactManager.apk","appPackage":"com.example.android.contactmanager","platformName":"Android","appActivity":".ContactManager"},"sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552"}
debug: 127.0.0.1 - - "GET /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552 HTTP/1.1" - 817 "-" "Python-urllib/2.7"
debug: Appium request initiated at /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552/element
debug: Request received with params: {"using":"name","sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552","value":"Add Contact"}
warn: [DEPRECATED] The "name" Locator strategy has been deprecated and will be removed. Please use the "accessibility id" Locator strategy instead.
info: Pushing command to appium work queue: ["find",{"strategy":"name","selector":"Add Contact","context":"","multiple":false}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"name","selector":"Add Contact","context":"","multiple":false}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding Add Contact using NAME with the contextId:
info: [BOOTSTRAP] [info] Returning result: {"value":{"ELEMENT":"1"},"status":0}
info: Responding to client with success: {"status":0,"value":{"ELEMENT":"1"},"sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552"}
debug: 127.0.0.1 - - "POST /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552/element HTTP/1.1" - 109 "-" "Python-urllib/2.7"
debug: Appium request initiated at /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552/element/1/click
debug: Request received with params: {"sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552","id":"1"}
info: Pushing command to appium work queue: ["element:click",{"elementId":"1"}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"1"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: click
info: [BOOTSTRAP] [info] Returning result: {"value":true,"status":0}
info: Responding to client with success: {"status":0,"value":true,"sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552"}
debug: 127.0.0.1 - - "POST /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552/element/1/click HTTP/1.1" - 89 "-" "Python-urllib/2.7"
debug: Appium request initiated at /wd/hub/session/96c07e38-8897-4abb-8ff8-d6b0ecb5b552/elements
debug: Request received with params: {"using":"-android uiautomator","sessionId":"96c07e38-8897-4abb-8ff8-d6b0ecb5b552","value":"new UiSelector().clickable(true)"}
info: Pushing command to appium work queue: ["find",{"strategy":"-android uiautomator","selector":"new UiSelector().clickable(true)","context":"","multiple":true}]
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"-android uiautomator","selector":"new UiSelector().clickable(true)","context":"","multiple":true}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: find
info: [BOOTSTRAP] [debug] Finding new UiSelector().clickable(true) using ANDROID_UIAUTOMATOR with the contextId:
info: [BOOTSTRAP] [info] Element[] is null: (0)
info: [BOOTSTRAP] [info] Element[] is null: (1)
info: [BOOTSTRAP] [info] Element[] is null: (2)
info: [BOOTSTRAP] [info] Element[] is null: (3)
info: [BOOTSTRAP] [info] Element[] is null: (4)
info: [BOOTSTRAP] [info] Element[] is null: (5)
info: [BOOTSTRAP] [info] Element[] is null: (6)
info: [BOOTSTRAP] [info] Element[] is null: (7)
info: [BOOTSTRAP] [info] Element[] is null: (8)
info: [BOOTSTRAP] [info] Element[] is null: (9)
info: [BOOTSTRAP] [info] Element[] is null: (10)
info: [BOOTSTRAP] [info] Element[] is null: (11)
info: [BOOTSTRAP] [info] Element[] is null: (12)
info: [BOOTSTRAP] [info] Element[] is null: (13)
info: [BOOTSTRAP] [info] Element[] is null: (14)
info: [BOOTSTRAP] [info] Element[] is null: (15)
info: [BOOTSTRAP] [info] Element[] is null: (16)
info: [BOOTSTRAP] [info] Element[] is null: (17)
info: [BOOTSTRAP] [info] Element[] is null: (18)
info: [BOOTSTRAP] [info] Element[] is null: (19)
info: [BOOTSTRAP] [info] Element[] is null: (20)
info: [BOOTSTRAP] [info] Element[] is null: (21)
info: [BOOTSTRAP] [info] Element[] is null: (22)
info: [BOOTSTRAP] [info] Element[] is null: (23)
info: [BOOTSTRAP] [info] Element[] is null: (24)
info: [BOOTSTRAP] [info] Element[] is null: (25)
info: [BOOTSTRAP] [info] Element[] is null: (26)
info: [BOOTSTRAP] [info] Element[] is null: (27)
info: [BOOTSTRAP] [info] Element[] is null: (28)
info: [BOOTSTRAP] [info] Element[] is null: (29)
info: [BOOTSTRAP] [info] Element[] is null: (30)
info: [BOOTSTRAP] [info] Element[] is null: (31)
info: [BOOTSTRAP] [info] Element[] is null: (32)
info: [BOOTSTRAP] [info] Element[] is null: (33)
info: [BOOTSTRAP] [info] Element[] is null: (34)
info: [BOOTSTRAP] [info] Element[] is null: (35)
info: [BOOTSTRAP] [info] Element[] is null: (36)
info: [BOOTSTRAP] [info] Element[] is null: (37)
info: [BOOTSTRAP] [info] Element[] is null: (38)
info: [BOOTSTRAP] [info] Element[] is null: (39)
info: [BOOTSTRAP] [info] Element[] is null: (40)
info: [BOOTSTRAP] [info] Element[] is null: (41)
info: [BOOTSTRAP] [info] Element[] is null: (42)
===日志部分

求助为什么后面都是 element[] is null
这个方法找不到界面上可点击的目标控件么

共收到 8 条回复 时间 点赞
LW #8 · 2014年05月04日 Author
import os
import unittest
from appium import webdriver
from time import sleep

Returns abs path relative to this file and not cwd

PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(file), p)
)

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.2.2'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['app'] = PATH(
'D:/Python/appium/ContactManager.apk'
)
desired_caps['appPackage'] = 'com.example.android.contactmanager'
desired_caps['appActivity'] = '.ContactManager'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_name("Add Contact")
els = driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
print els
driver.quit()

clickable 这个特性比较特殊, 有一些控件看起来是可点击的, 单其实 clickable 是 false, 只是上层容器的 clickable 是 true. 你最好借助于 uiautomatorviewer 看下

#2 楼 @seveniruby
请问您有没有碰到过这种现象:
imageButton 可以定位到,click() 方法后却没有效果。。
这种问题应该如何解决?

#3 楼 @tomdejia 我以前测试过, 是 ok 的.

#4 楼 @seveniruby
我可能没有描述清楚问题。我的这个问题跟楼主的情况不一样,我是采用 0.18 版本的,Android+window 系统。
现象是:app 中某个页面里的 imageButton 通过 find_element_by_id 可以找到,click 方法执行后也没有报错,但是在真机上观察,并没有实现点击的真正效果。
通过 monitor 查看该 button 是 clickable=true

这种现象是什么原因,如何解决?

你这个日志是正常的,可能是他内部的一个 BUG,不过你这日志说明已经获取相关的对象了。我去获取 ImageView 对象列表的时候,日志也是这样的,但是可以继续操作。如果没有获取到对象,提示的是 index= 0 ;size = 0。
你不要用打印,你可以直接去点击试试,应该能点击的到某个对象的。

LW #2 · 2014年05月10日 Author
import unittest

from appium import webdriver
import desired_capabilities


class FindByUIAutomatorTests(unittest.TestCase):
    def setUp(self):
        desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

    def tearDown(self):
        self.driver.quit()

    #def test_find_single_element(self):
        #el = self.driver.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
        #self.assertIsNotNone(el)

    def test_find_multiple_elements(self):
        els = self.driver.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
        for e in els:
            e.click()
        #self.assertTrue(len(els) > 11)

    #def test_element_find_single_element(self):
        #el = self.driver.find_element_by_class_name('android.widget.ListView')

        #sub_el = el.find_element_by_android_uiautomator('new UiSelector().description("Animation")')
        #self.assertIsNotNone(sub_el)

    #def test_element_find_multiple_elements(self):
        #el = self.driver.find_element_by_class_name('android.widget.ListView')

        #sub_els = el.find_elements_by_android_uiautomator('new UiSelector().clickable(true)')
        #self.assertTrue(len(sub_els) > 11)


if __name__ == "__main__":
    unittest.main()

===用官方的代码点击没有效果

#7 楼 @fzuliwei @eric 有可能获取到了对象, 但是点击的位置不多, 你是这用 getevent 命令来监听下点击事件. 或者 uiautomator events. 看看点击的位置在什么地方

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