I hope that now you have an idea about what is monkeyrunner and what you can do with it. Let’s start doing them.
How to install?
Oh .. oo .. you don’t need to install it separately. It comes with Android SDK package. Inside your android-sdk-windows or linux package, inside tools directory, you can find this tool monkeyrunner.
How to run monkeyrunner?
If you want to run monkeyrunner from anywhere within the command line, you need to add the path of the monkeyrunner directory(\tools ) to your system PATH variable. changing path variable can be seen in this simple post .
once you have the PATH variable set, you are ready to go…. But wait .. you should have something to run with monkeyrunner. It is none other than a python script.
Let’s write a simple python script to test your application.
How to write your first script?
monkeyrunner API provides 3 modules which are located in com.android.monkeyrunner namespace. You will be using these modules to run your tests with monkeyrunner tool.
-
MonkeyRunner
: A class of utility methods for monkeyrunner programs. This class provides a method for connecting monkeyrunner to a device or emulator. It also provides methods for creating UIs for a monkeyrunner program and for displaying the built-in help. -
MonkeyDevice
: Represents a device or emulator. This class provides methods for installing and uninstalling packages, starting an Activity, and sending keyboard or touch events to an application. You also use this class to run test packages. -
MonkeyImage
: Represents a screen capture image. This class provides methods for capturing screens, converting bitmap images to various formats, comparing two MonkeyImage objects, and writing an image to a file.
If you can’t understand what these modules are for and what you are going to do with them, It’s OK. you will know about them as you read the next section.
-
First you need to import the modules which you are using in this script
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
-
Then you have to connect to the device which you are running your test
device = MonkeyRunner.waitForConnection()
-
Then you need to install the application package to the device
device.installPackage(‘Yamba/bin/Yamba.apk’)
(Here the folder name is given as a relative path. Yamba folder should be located in the same directory in which you are currently in. You can give the absolute path as well.
For Windows users,
you can give the path as (‘C:\\Program Files\\Yamba\\bin\\Yamba.apk’) as your absolute path.
-
set the package and the activity to be started
package = ‘com.marakana.android.yamba’
activity = ‘.MainActivity’
runComponent = package + ‘/’ + activity
-
Run the activity on the device
device.startActivity(component=runComponent)
-
Wait for 2 seconds before sending the next command
MonkeyRunner.sleep(2)
-
Send a menu button press event
device.press(‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)
-
Wait for 2 seconds before sending the next command
MonkeyRunner.sleep(2)
-
Send a touch event to a button located at bottom center of an HVGA device
device.touch(200, 390, ‘DOWN_AND_UP’)
-
Wait for 2 seconds before sending the next command
MonkeyRunner.sleep(2)
-
Take a screenshot of the resulting window
result = device.takeSnapshot()
-
Write the screenshot to a file
result.writeToFile(‘Yamba/status_update.png’,’png’)
Name this script as test.py and save it on your current directory. (You can save it anywhere…)
Now it looks like this
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
# Installs the Android package. Notice that this method returns a boolean, so
# you can test to see if the installation worked.
device.installPackage(‘Yamba/bin/Yamba.apk’)
# sets a variable with the package’s internal name
package = ‘com.marakana.android.yamba’
# sets a variable with the name of an Activity in the package
activity = ‘.MainActivity’
# sets the name of the component to start
runComponent = package + ‘/’ + activity
# Runs the component
device.startActivity(component=runComponent)
# Wait for few seconds
MonkeyRunner.sleep(2)
# Presses the Menu button
device.press(‘KEYCODE_MENU’, MonkeyDevice.DOWN_AND_UP)
# Wait for few seconds
MonkeyRunner.sleep(2)
#Touch the new status button
device.touch(200, 390, ‘DOWN_AND_UP’)
# Wait for few seconds
MonkeyRunner.sleep(2)
# Takes a screenshot
result = device.takeSnapshot()
# Wait for few seconds
MonkeyRunner.sleep(2)
# Writes the screenshot to a file
result.writeToFile(‘Yamba/status_update.png’,’png’)
Then on the command line or in a terminal window type this command
monkeyrunner -v ALL test.py
(wait … what is this -v ALL thing …
-v is for verbose mode
ALL is for printing all the messages from monkeyrunner tool )
now you can see that the test is running on your device(s) or emulator(s).
cheers !!!!!!!!!
Just wish to say your article is as surprising. The clearness for your post is simply spectacular and that i can suppose you’re knowledgeable on this subject. Fine together with your permission allow me to grasp your feed to stay up to date with forthcoming post. Thanks a million and please continue the enjoyable work.
Wonderful issues altogether, you simply received a new reader. What could you recommend in regards to your publish that you just made some days ago? Any certain?
Pingback: monkeyrunner tutorial – Running tests on multiple devices with monkeyrunner « chanakaudaya
awesome, very useful for beginers, please do continue the same
where to get this yamba.apk file please?
Yamba.apk is only a sample apk file. You can use your own apk files.
can you share the apk file ( I need to know the ” package = ‘com.marakana.android.yamba’
activity = ‘.MainActivity’
runComponent = package + ‘/’ + activity ” )
Currently i don’t have this yamba.apk file. It is not a big issue. You can use any apk file which is created during the build of your project. You need to identify the package name and the Activity you are going to test. That’s all you need.
Hi,
I dont have much info regarding monkeyrunner.
I have written test cases for my project using Robotium. but for couple of scripts i’m thinking of monkeyrunner. Can we combine robotium and monkeyrunner test case in one project?
Thanks,
Thanks for providing this article. It was very useful to me
Excellent web site. Lots of useful information here. I’m sending it to several friends ans also sharing in delicious. And naturally, thanks for your sweat!
Thanks for the comment. Hope to provide more information on android domain.
Thanks for the article. On thinking further with monkey runner, for providing voice inputs to the device, which method of MonkeyDevice, MonkeyRunner, MonkeyImage can be thought of.
Thanks in advance.
I’m gonna repost the comment I made here, which was a ripoff of your blog post:
http://androidjellybeans.blogspot.se/2012/08/android-testing-with-monkeyrunner.html
Thanks for this post! In OS X 10.8 paths can be found in /etc/paths.
And TouchPressType should be the same in touch() as press(). I.e MonkeyDevice.DOWN_AND_UP, not ‘DOWN_AND_UP’ which renders an error.
Nice article.
Is it possible to record the mouse click, touch events, coordinates(x,y) pixel points?
Monkeyruuner is recognized as internal command error displayed.
you’re in point of fact a just right webmaster. The site loading velocity is amazing. It seems that you are doing any unique trick. Also, The contents are masterwork. you have done a great task in this subject!
Superb site you have here but I was wanting to know if you knew
of any community forums that cover the same topics discussed in this article?
I’d really like to be a part of group where I can get responses from other experienced individuals that share the same interest. If you have any suggestions, please let me know. Thanks a lot!
Awesome tutorial
I was wondering if it is possible to import roboitum jar into monkeyRunner and use robotium library?
Good day! Would you mind if I share your blog with
my myspace group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Many thanks
I’m happy to share my knowledge with others. There’s no problem sharing this.
Thank you *so* much for this! Helped me a great deal in the early days of learning Monkeyrunner.
Question though — how do you verify a button press actually happened?
Thanks for such a detailed doc. however while trying to execute my python script I am getting an error on line 2(device = MonkeyRunner.waitForConnection()).
Error: Adb rejected adb port forwarding command: cannot bind socket.
I have tried manually killing and restarting ADB server, I also tried port forwarding but not sure if I was doing it right.I also restarted my m/c as well as the device. I tried the same code in an emulator as well. It is constantly giving the same error.
What other solution you would recommend?
Is it possible to access android api using Monkeyrunner?
If yes then how ?
I’ve been exploring for a bit for any high-quality articles or blog posts in this kind
of space . Exploring in Yahoo I at last stumbled upon
this site. Studying this info So i’m satisfied to express that I have a very excellent uncanny feeling I discovered exactly
what I needed. I most indubitably will make sure to do not overlook
this web site and give it a glance on a relentless basis.
Αɗmiring the Һarԁ work you put ntߋ your site andd in deƿth information youu provide.
It’s nice too comе achross a blog everу once in a while that isn’t the
same old rehasheԀ information. Εxcellent read!
Ӏ’ve saved yoսr site and I’m adding your RSS feeds to myy Gogle account.