Search This Blog

Monday, December 10, 2012

Trying Monkeyrunner

Testing is something we have to do but no one really want to do it. Fortunately, Android provides tool for doing automatic testing called "Monkeyrunner"

I just tried it and it works very well. First, we need to prepare all below files stored in the same folder...
 1. your application .apk file (ex: yourapplication.apk)
 2. your test python script (ex: testing.py)
 3. screen captured folder(ex: screenshot/)

In your test python script file, write down testing script ... Some kind like this one below.

# 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('yourapplication.apk')

# sets a variable with the package's internal name
package = 'your.application.package'

# sets a variable with the name of an Activity in the package
activity = 'your.application.package.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

#wait 2 sec
MonkeyRunner.sleep(2)

#touch some button
device.touch(65, 215, 'DOWN_AND_UP')

#wait 2 sec
MonkeyRunner.sleep(2)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('screenshot/shot1.png','png')
Connect your device with your pc and execute testing.py with below shell command ...

     $/usr/local/android-sdk-linux_XXXXX/tools/monkeyrunner testing.py

If everything alright then your will have shot1.png file stored in screenshot folder.

No comments:

Post a Comment