The time may come when you want your tests to interact with the Application Under Test (AUT) in a more clever way than just guessing the View's coordinates on the screen and sending the events.
Furthermore, this is sometimes not possible at all because in order to send the events you may need to obtain some View state. Let me give you an example to illustrate this and if you want you can try to solve it using monkeyrunner.
We can use Development Settings as our AUT and our intention could be to activate the Show running processes and Immediately destroy activities settings.
As we may accustom to do, we can obtain the coordinates of these Views on the screen and send the corresponding touch events using MonkeyDevice.touch() as usual. Sonner or later, maybe sooner, we will discover that because these settings are persistent we should know the state before sending the event, otherwise we will be changing its state other than just enabling this settings as is this example's intention.
We introduced a way of doing things like that in
Automated Android testing using Sikuli using visual comparison and obtaining properties like the text in EditText's in monkeyrunner: testing views properties (which depends on a patch to chimpchat that has not yet been approved) but now we are craving for a more general approach unless we had the intention to patch chimpchat to support all of the properties in the different Views.
This approach is AndroidViewClient that you can download and install from github. Still has its rough edges but I wanted to show its functionality here and be open to comments.
AndroidViewClient adds to monkeyrunner the ability of
- finding Views by ID, very much like you normally do in your Android Activity (using ViewClient.findViewById())
- obtaining the value for almost any of the Views properties (using for example View.isChecked() or View.mText())
- sending touch events to the Views by simply invoking View.touch()
The following script is a case of these abilities in action.
#! /usr/bin/env monkeyrunner ''' Copyright (C) 2012 Diego Torres Milano Created on Feb 3, 2012 @author: diego ''' import re import sys import os # this must be imported before MonkeyRunner and MonkeyDevice, # otherwise the import fails try: ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME'] except KeyError: print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" % __file__ sys.exit(1) sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src') from com.dtmilano.android.viewclient import ViewClient from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # Displayed com.android.development/.DevelopmentSettings: +379ms package = 'com.android.development' activity = '.DevelopmentSettings' componentName = package + "/" + activity device = MonkeyRunner.waitForConnection(60, "emulator-5554") if not device: raise Exception('Cannot connect to device') device.startActivity(component=componentName) MonkeyRunner.sleep(5) vc = ViewClient(device) vc.dump() showCpu = vc.findViewById("id/show_cpu") showLoad = vc.findViewById("id/show_load") alwaysFinish = vc.findViewById("id/always_finish") if not showLoad.isChecked(): showLoad.touch() if not alwaysFinish.isChecked(): alwaysFinish.touch() if not showCpu.isChecked(): # WARNING: Show CPU usage is de-activated as soon as it's activated, that's why it seems it # is never set showCpu.touch()
Once we run this script against a running emulator or device we will be able to see how the settings are enabled.
Moreover, if you run the script again you will see how the settings remain unchanged.
AndroidViewClient is a work in progress but it is already useful for many cases where the monkeyrunner scripts are not enough or where the complexity of achieving the desired goals is too high.
Give AndroidViewClient a try, share your comments, suggestions, patches and scripts here to help improve it.