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.
Post a Comment