如何测试应用程序是否因 monkeyrunner 脚本而崩溃?

How to test if the app has crashed from a monkeyrunner script?

我使用 Android monkeyrunner 工具通过脚本测试我的应用程序,该脚本主要来自教程:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

device = MonkeyRunner.waitForConnection()
device.installPackage('the-app.apk')
device.startActivity(component="package.app.the/package.app.the.Main")

# then pilot the app

在此脚本的末尾,我想测试该应用程序是否仍在 运行 或是否已崩溃(即显示“应用程序已停止”消息)。

我试过 get the info by checking the PID of the app:

pid = device.shell('pidof package.app.the')

很遗憾,此命令返回了一个有效的 pid。我还以为是我检查的太早了,所以我试着在它前面放了一个MonkeyRunner.sleep(10)。结果是一样的,似乎应用程序仍然是 运行 而显示“应用程序已停止”弹出窗口。

我也尝试解析 device.shell('ps -A') 的输出,但结果是一样的:应用程序是 运行.

然后我尝试使用 device.press("BACK", MonkeyDevice.DOWN_AND_UP) 发送关键事件以试图关闭“应用程序已停止”对话框,但对话框仍然存在。

现在我没主意了。如何从脚本测试应用程序是否崩溃?

既然你使用的是Python,我的建议是使用AndroidViewClient and automate the process of detecting the string ids contained the the "App has stopped" dialog. An example might be this one

例如,“应用程序已停止”文本视图具有资源 ID android:id/alertTitle,而按钮(“再次打开应用程序”)具有资源 ID android:id/aerr_restart