2

我正在开发一种工具来识别 android 应用程序中的小部件,并在单击时显示它们的属性。我创建了一个可以成功执行此操作的独立工具,但是在将独立工具转换为 Eclipse 插件时遇到了问题,这对于打包到我们的最终产品中是必需的。

该工具的操作方式是首先在模拟器上运行透明覆盖应用程序。此应用程序收集用户按下屏幕的坐标并将其写入 Android 日志。然后,在模拟器上启动要测试的应用程序。

adb logcat 作为标准输入通过管道传输到处理小部件识别的 monkeyrunner 脚本中。实时,monkeyrunner 脚本解析 logcat 输入的坐标新闻信息。monkeyrunner 脚本使用 AndroidViewClient (https://github.com/dtmilano/AndroidViewClient),这是一个可以列出屏幕上所有 View 对象及其属性的工具。使用坐标印刷信息,从视图列表中选出具有正确坐标属性的视图,并打印出其属性。

我遇到的问题源于尝试在 eclipse/java 的上下文中处理 logcat 输出到 monkeyrunner 脚本的管道。如果我在单独的并发进程中运行 adb logcat 和 monkeyrunner,并将 logcat 的 InputStream 传输到 monkeyrunner 的 OutputStream,似乎存在一些锁定问题,因为 monkeyrunner 永远不会接收坐标信息作为输入。我也尝试将流处理程序放在单独的线程中,但这没有效果。

由于并发进程有时会成为问题的根源,我想直接在monkeyrunner中从logcat中读取,因此只有一个主进程。在 monkeyrunner 中,一旦与模拟器建立连接,就可以在其上打开 adb shell (https://developer.android.com/tools/help/MonkeyDevice.html#shell)。Logcat 是一个非常常见的 adb 命令,但是当我运行 device.shell("logcat") 时,它失败了

    [main] [com.android.chimpchat.adb.AdbChimpDevice] Error executing command: logcat

    [main] [com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.ShellCommandUnrespo‌​nsiveException – 

不幸的是,我的谷歌技能无法为我找到解决这个问题的方法。

4

1 回答 1

2

好吧,我想这就是你要找的东西:

#! /usr/bin/env monkeyrunner
import time
import subprocess

# Imports the monkeyrunner modules
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

p = subprocess.Popen([ "adb", "logcat", "TEST:V", "*:S" ], shell=False,
        stdout=subprocess.PIPE)
i = 0
while True:
    device.shell("log -t TEST This is line %d" % i)
    i += 1
    print p.stdout.readline()
    time.sleep(1)

连接到设备、写入和读取 logcat 的 monkeyrunner 脚本。

顺便说一句,我很高兴听到您发现AndroidViewClient很有用。

于 2012-08-04T05:19:35.437 回答