androidadbmonkeyrunner

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


I use the Android monkeyrunner tool to test my app with a script that is mostly the one from the tutorial:

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

At the end of this script I would like to test if the app is still running or if it has crashed (i.e. the "App has stopped" message is displayed).

I tried to get the info by checking the PID of the app:

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

Unfortunately there is a valid pid returned by this command. I thought it was because I checked too soon so I tried to put a MonkeyRunner.sleep(10) before it. The result is the same, it seems that the app is still running while the "App has stopped" popup is displayed.

I also tried to parse the output of device.shell('ps -A') but the result is the same: the app is running.

Then I tried to send key events with device.press("BACK", MonkeyDevice.DOWN_AND_UP) in an attempt to dismiss the "App has stopped" dialog but the dialog stayed.

Now I'm out of ideas. How to test if the app has crashed from the script?


Solution

  • Since you are using Python, my suggestion is to use AndroidViewClient and automate the process of detecting the string ids contained the the "App has stopped" dialog. An example might be this one.

    For instance, the "App has stopped" textview has resource-id android:id/alertTitle whereas the button ("Open the app again") has resource-id android:id/aerr_restart.