Friday, June 24, 2011

Using Android monkeyrunner to automate test steps

It is not unlikely that in writing your tests you discover that some simple steps are required by several tests.

 monkeyrunner is a great tool in this respect because it offers a tremendously powerful and complete language like python.
You can write your tests using python but also you can build libraries containing primitives belonging to the test domain.
This example shows how you can lock and unlock the device screen. Usually this methods should be in a different class or module but for the sake of simplicity we are including lockDevice() and unlockDevice() here in the main script.


#! /usr/bin/env monkeyrunner
'''
Created on Jun 22, 2011

@author: diego
'''

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice


def lockDevice(device):
    return device.press("POWER", MonkeyDevice.DOWN_AND_UP)


def unlockDevice(device):
    device.wake()
    device.drag((130, 620), (350, 620), 1.0, 120)

def main():
    device = MonkeyRunner.waitForConnection()
    if device:
        lockDevice(device)
        MonkeyRunner.sleep(5.0)
        unlockDevice(device)

if __name__ == '__main__':
    main()

Now, a demonstration of this script running on a Nexus One, locking, waking up, and unlocking the screen.



Hope this helps you start creating you monkeyrunner libraries.
Comments are welcome.

15 comments:

ermedian said...

i wonder how much i can test my project on emulator with monkeyruner. can i automatise all testing process? for example can i test login screen for both successful and failed cases. does it give error if i left empty input box or enter wrong type of input? thanks

Unknown said...
This comment has been removed by the author.
Unknown said...

Can we interact with 2 apps with the monkeyrnner.?
Example: I want to be test wiht offlinemode.so that i need to turnoff-wifi connection in settings.apk file..
so that i need to interact settings.apk and Myapp.apk
can we test with 2 apps?

Diego Torres Milano said...

Yes, you can interact with more than one activity at a time.
For example you can invoke startActivity(component="com.android.settings/.Settings"), interact with this activity, press HOME or BACK, startActivity(component="...MyApp") and interact.

Unknown said...

can we write asserts using monkey runner for functional testing?

Anonymous said...

Hello Diego,

I have been trying to start an activity and pass a parcelable to that activity from python via the extras param in device.startActivity(). Here's what the code looks like.

user = FbUser()
extraDic = {}
extraDic['userName'] = 'Test'
extraDic['userId'] = 'me'
extraDic['userInfo'] = user
d.startActivity(extras=extraDic, component=runComponent +
postActivity)

FbUser is the object in my java project that implements Parcelable. The bin of the project has been properly added to the sys.path in the Jython Monkeyrunner script. When I debug the script, it creates the FbUser object correctly, however, when I print out the extras in LogCat, the 'userInfo' extra is missing but the simple string extras are present... Do you know why this might be?

Anonymous said...

I think I just answered my own question. I just browsed the source code and when starting an activity they use this JythonUtils class to translate the objects in the intent to java objects. However, they use a map that only translates PyObjects to Strings, Doubles, and Integers... I hope they change this soon.

teju said...

i want clear example for monkey runner including mainapplication test application and monkey runner scripting .how to import test cases class file i mean how to plugin the test class to monkey runner .plz send me the solution wth clear steps

teju said...

am fresher am working on monkeyrunner tool am not getting anythng about monkey runner tool
plz i want about monkeyrunnner how to import modules and how to add plugins i.e alrdy created android junit class file

teju said...

reddy naku me number dorkthundha plz naku monkeyrunner gurinchi help kavali

Amey said...

well hello every one

i am trying to run a sample python script using eclipse..my python and jython paths set and tested..
but whne i try importing com.android.monkeyrunner import MonkeyRunner, MonkeyDevice;
i am getting an error ..what i underrstand by this is the connection between android and python program is not available..how should i get across this scenario

Diego Torres Milano said...

If you don't tell the error it's very difficult to help you. However, I'll guess that your problem is that you're not using monkeyrunner as the interpreter of the script and you are using python or jython instead.

Amey said...

Ok..here is my basic question..do we have to create a new python project to run monkeyrunner or an android project and add this ..py as an external code jus da way i do it in phonegap..??

because what i am doing currently is creating a new python project in eclipse ubuntu and trying to run the above code..and that is where i get an erroe importing packagesom.android.monkeyrunner import MonkeyRunner, MonkeyDevice; now where i think i am going wrong is connecting android and python script

Diego Torres Milano said...

It seems you haven't followed the steps described in Eclipse: working monkeyrunner configuration. Pay special attention to the Interpreter tab.

Palm Beach Vending Machine said...

I want clear example for monkey runner including mainapplication test application and monkey runner scripting.
Palm Beach Vending Machine