Is it possible to call Monkey tests from MonkeyRunner Script - android

At some point in my Monkeyrunner I want to launch random Monkey tests (The ones we can get through a command adb shell monkey -p my.package -v 500), So I added this line device.shell('monkey -p my.package -v 500') to my python script. But nothing happens, Any Ideas?

Your command should start the monkey tests. However, you need to pause the script execution during there random monkey tests by adding MonkeyRunner.sleep(no_of_seconds) statements. This will give time for the random tests to complete.

Yes normally it should start it, but it didn't.
Alternatively I imported os import os,
then I called monkey through this command os.system('adb shell monkey -p mypackage -v 500').
Sadly it relaunches the my application. Not the best outcome.

Related

Save Android Monkey random run to script format for later replay

Is there a way to save the random run of Android Monkey into a script with the proper format to later replay it by running:
adb shell monkey -p <package_name> -f script_file 1
EDIT:
I know there is a seed flag (-s), but that's not what I want. I have to be able to work with the generated script before feeding it back to the Monkey.
Not an easy way, but you could do a reverse engineering on the monkey script source to create a script that takes the output of the monkey command and generates the monkey script.
So you could run:
adb shell monkey -p <package_name> -v -v 1 > monkey-logs.txt
And then*:
convert-to-monkey-script.sh monkey-logs.txt
For example, one output of the monkey call:
Replaying 11 events with speed 1.0
:Sending Touch (ACTION_DOWN): 0:(450.0,450.0)
:Sending Touch (ACTION_UP): 0:(450.0,450.0)
Sleeping for 45 milliseconds
...
Becomes the following monkey script (read the monkey source to understand better the arguments):
type= raw events
count= 2
speed= 1.0
start data >>
DispatchPointer(6934862,6934862,0,450.0,450.0,0.0,0.0,0,1.0,1.0,0,0)
DispatchPointer(6934862,6934862,1,450.0,450.0,0.0,0.0,0,1.0,1.0,0,0)
Which can be run with (with the content above in the monkey.script file and after a adb push):
adb shell monkey -p <package_name> -f monkey.script 1
I've made a simple gist for myself that convert adb taps commands into monkey script format (because they're faster) here, so I think that is possible to make a general script for that.
*Note: convert-to-monkey-script.sh doesn't exist. As I said, someone COULD do it
There is no option for saving script, but you can use seed that acts like a seed in random number generator so same seed leads to same events. Here is an example:
adb shell monkey -p com.package -s 123 500
This will run Monkey on package 'com.package' with seed value of '123' and produce 500 events.

Call adb monkey from Java Application

I'm making a Java application that I want to call the adb monkey tester for an android package. From the cmd, I was managed to call it but from the Java application, I wasn't. Any ideas? Find my code below.
Cmd command that works:
adb -s 079d35918ec8c84b shell monkey -p com.zte.bamachaye -v 1000
In my Java code, i have the code below, which when I run the process never finish. It stuck there and the app is still on the smartphone
ProcessBuilder pb2 = new ProcessBuilder("adb","-s", "079d35918ec8c84b","shell monkey","-p", packageName, "-v", "500");
Process pc2 = pb2.start();
pc2.waitFor();
Thank you in advance.

How to detect adb monkey crashed the app?

I'm setting up a Jenkins job in order to run adb monkey on my app. I run the following command:
adb shell monkey -p nl.tmg.telegraaf -v 500
In some cases it succeeds and sometimes it fails. However, the exit code is always 0. Hence jenkins treats it as a successful job. Anybody know how this can be prevented?
You could use the Android Emulator Plugin for Jenkins, which can run Monkey for you, parse the output, and change the build result accordingly.

Stopping Robotium test execution

I have a Jenkins job that starts a Robotium test from command line:
adb shell am instrument -w com.foo.tests/android.test.InstrumentationTestRunner
Sometimes the test run gets stuck. When I abort the Jenkins job, it does not stop the Robotium test run. I may have to cancel the test execution manually from the device before running another Jenkins job.
How do I stop the test execution? I cannot just uninstall the application under test with adb uninstall since it has active device admin.
You can cancel the previous Robotium test by starting another test one with a made-up test name:
adb shell am instrument -e class com.foo.tests#dummyTestName com.foo.tests/android.test.InstrumentationTestRunner
What about this:
adb shell am force-stop <PACKAGE>
You can clear the app data,like this
adb shell pm clear com.foo.tests

How do I stop the monkey madness?

I'm using the monkey tool to run a test of my Android application. For example, I might do a run like the following:
adb shell monkey -p com.myapp -v 10000
However, if I change my mind and need to cancel the test, there doesn't seem to be a way to do so that doesn't involve waiting multiple minutes for the damned monkey to finish most or all of its run.
Killing the adb shell process on my mac doesn't solve the problem. Killing the com.myapp process on my phone using ddms doesn't work. Unplugging my phone doesn't work.
How do I cancel the monkey madness?
You can kill the monkey process just doing this:
$ adb shell ps | awk '/com\.android\.commands\.monkey/ { system("adb shell kill " $2) }'
[Nitpick] You're confusing monkeyrunner with monkey.
The monkeyrunner tool is not related to the UI/Application Exerciser
Monkey, also known as the monkey tool. The monkey tool runs in an adb
shell directly on the device or emulator and generates pseudo-random
streams of user and system events. In comparison, the monkeyrunner
tool controls devices and emulators from a workstation by sending
specific commands and events from an API.
[/Nitpick]
On my Android 2.2 device when I start monkey, I see a process started in DDMS by the name "?" (just a question mark). When I killed that process, the monkey madness stopped.
adb shell
ps | grep monkey
kill process_id
adb shell kill $(adb shell pgrep monkey)
kudo to #deadfish
For what it's worth, I use Android Studio 3.1.4 on a Mac in 2018 and I had to alter the accepted answer like so:
./adb shell ps | awk '/com\.android\.commands\.monkey/ { system("./adb shell kill " $2) }'
Hope that help prevent some hair-pulling and pencil snapping out there!
Also... when it comes to the monkey, always be sure to pin your app!!! Otherwise you might accidentally send all your selfies to a random email in China like I did. ¯\_(ツ)_/¯
Kill the monkey by shell will cause a small problem, the IActivityController in ActivityTaskManagerService will not be set to null, which it should. And the ActivityManager.isUserAMonkey() still return true.
If monkey stop automatically, it will reset the Controller properly:
Monkey.java{
private int run(String[] args) {
...
try {
mAm.setActivityController(null, true);
mNetworkMonitor.unregister(mAm);
}
...
}
}

Categories

Resources