5

我正在使用一个 Android 模拟器运行一个自动化测试,该模拟器驱动一个带有用 Python 编写的 Monkey 脚本的应用程序。该脚本将文件复制到模拟器上,单击应用程序中的按钮并根据软件在运行期间触发的活动做出反应。该脚本应该循环运行几千次,所以我在一个循环中运行 adb 工具来复制文件,启动活动并通过调用设备上的 getProperty 方法来查看软件的反应参数“am.current.comp.class”。所以这是我的脚本的一个非常简化的版本:

for target in targets:
    androidSDK.copyFile(emulatorName, target, '/mnt/sdcard')

    # Runs the component
    device.startActivity(component='com.myPackage/com.myPackage.myactivity')

    while 1:
        if device.getProperty('am.current.comp.class') == 'com.myPackage.anotheractivity':
            time.sleep(1) # to allow the scree to display the new activity before I click on it
            device.touch(100, 100, 'DOWN_AND_UP')
            # Log the result of the operation somewhere
            break

        time.sleep(0.1)

(androidSDK 是我编写的一个小类,它封装了一些实用程序函数来使用 adb 工具复制和删除文件)。

例如,有时脚本会因许多异常之一而崩溃(我忽略了完整的堆栈跟踪)

[com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.ShellCommandUnresponsiveException

或者

[com.android.chimpchat.adb.AdbChimpDevice] Unable to get variable: am.current.comp.class
[com.android.chimpchat.adb.AdbChimpDevice]java.net.SocketException: Software caused connectionabort: socket write error

我读过有时与设备的套接字连接变得不稳定,可能需要重新启动(adb start-server 和 adb kill-server 很有用)。

我遇到的问题是这些工具正在抛出 Java 异常(Monkey 在 Jython 中运行),但我不确定如何从我的 Python 脚本中捕获这些异常。我希望能够确定脚本内失败的确切原因并恢复情况,以便我可以继续我的迭代(例如,重新建立连接?例如,用另一个电话重新初始化我的设备到 MonkeyRunner.waitForConnection 就够了吗?)。

有任何想法吗?

非常感谢,阿尔贝托

编辑。我想我会提到我发现可以在 Jython 脚本中捕获特定于 Java 的异常,如果有人需要的话:

from java.net import SocketException

...

try:
    ...

except(SocketException):
    ...
4

2 回答 2

0

可以在 Jython 脚本中捕获特定于 Java 的异常:

from java.net import SocketException

...

try:
    ...

except(SocketException):
    ...

(取自OP对他的问题的编辑)

于 2013-10-07T20:59:35.030 回答
0

这对我有用: device.shell('exit')# Exit the shell

于 2015-11-19T21:45:04.353 回答