uiautomator获取不到动态界面的缘由

这几天查看了下源码发现,uiautomatorviewer在获取界面布局信息的时候用的是启动一个脚本,该脚本在/system/bin/uiautomator。这个命令也可以在命令行下启动。

默认情况下,获取的控件信息保存在/storage/emulated/legacy/window_dump.xml文件中,你也可以改变它保存的目录,例如保存在data/local/tmp下

这是正常情况下的,,但我进入秒表界面,将秒表开启。然后执行上面的命令:

报了could not get idle state的错。

说明uiautomator在获取界面状态信息时,首先要等界面处于idle空闲状态才会做dump操作。这就是uiautomator死活拿不到动态界面信息的原因。

调出uiautomator这个脚本。

## Copyright (C) 2012 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at#### Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.## Script to start "uiautomator" on the device## The script does a couple of things:# * Use an alternative dalvik cache when running as non-root. Jar file needs# to be dexopt'd to run in Dalvik. For plain jar files, this is done at first# use. shell user does not have write permission to default system Dalvik# cache so we redirect to an alternative cache# * special processing for subcommand 'runtest':# * '–nohup' allows process continue to run even if parent process that#started it has already terminated. We parse for this parameter and set#signal trap. This is useful for testing with USB disconnected# * all jar files that the test classes resides in, or dependent on are#provided on command line and exported to CLASSPATH environment variable#before starting the Java code. This offloads the task of class loading#and resolving of cross jar class dependency to Dalvik# * all other subcommand or options are directly passed into Java code for#further parsingexport run_base=/data/local/tmpexport base=/system# if not running as root, trick dalvik into using an alternative dex cacheif [ ${USER_ID} -ne 0 ]; then tmp_cache=${run_base}/dalvik-cache if [ ! -d ${tmp_cache} ]; thenmkdir -p ${tmp_cache} fi export ANDROID_DATA=${run_base}fi# take first parameter as the commandcmd=${1}if [ -z "${1}" ]; then cmd="help"fi# strip the command parameterif [ -n "${1}" ]; then shiftfiCLASSPATH=/system/framework/android.test.runner.jar:${base}/framework/uiautomator.jar# eventually args will be what get passed down to Java codeargs=# we also pass the list of jar files, so we can extract class names for tests# if they are not explicitly specifiedjars=# special case pre-processing for 'runtest' commandif [ "${cmd}" == "runtest" ]; then # first parse the jar paths while [ true ]; doif [ -z "${1}" ] && [ -z "${jars}" ]; thenecho "Error: more parameters expected for runtest; please see usage for details"cmd="help"breakfiif [ -z "${1}" ]; thenbreakfijar=${1}if [ "${1:0:1}" = "-" ]; then# we are done with jars, starting with parameters nowbreakfi# if relative path, append the default path prefixif [ "${1:0:1}" != "/" ]; thenjar=${run_base}/${1}fi# about to add the file to class path, check if it's validif [ ! -f ${jar} ]; thenecho "Error: ${jar} does not exist"# force to print help messagecmd="help"breakfijars=${jars}:${jar}# done processing current arg, moving onshift done # look for –nohup: if found, consume it and trap SIG_HUP, otherwise just # append the arg to args while [ -n "${1}" ]; doif [ "${1}" = "–nohup" ]; thentrap "" HUPshiftelseargs="${args} ${1}"shiftfi doneelse # if cmd is not 'runtest', just take the rest of the args args=${@}fiargs="${cmd} ${args}"if [ -n "${jars}" ]; then args="${args} -e jars ${jars}"fiCLASSPATH=${CLASSPATH}:${jars}export CLASSPATHexec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}

看到最后一句话是去执行了com.android.commands.uiautomator.Launcher这个类。该类位于uiautomator.jar包里。该jar包在framework中。

看不见我将要去的地方,记不得我已经去过的地方。

uiautomator获取不到动态界面的缘由

相关文章:

你感兴趣的文章:

标签云: