Skip to content
Diego Torres Milano edited this page Jul 12, 2013 · 78 revisions

AndroidViewClient

AndroidViewClient is an extension to monkeyrunner that simplifies test script creation and android test automation, providing higher level operations and the ability of obtaining the tree of Views present at any given moment on the device or emulator screen and perform operations on it.

Interaction with Views is completely device independent and you don't have to take into account different screen sizes, resolutions or densities as the operations can be specified by View instead as by (x,y) coordinates.

Test script creation can be even further simplified and automated by using Culebra.

Installation

AndroidViewClient does not require installation to be used. Downloading the required components is all you need.

IMPORTANT: AndroidViewClient and monkeyrunner are self contained tools so there is no need to download and install additional versions of python, jython or other components

Binary

Download the binary distribution from Github releases.

Previous versions were available through Google Drive.

Tools

This binary jar distribution includes the tools and a runner. This greatly simplifies the process of using AndroidViewClient.

These tools can be run using java.

NOTE: Replace <version> by the actual version number (i.e. 2.3.24) in next commands

$ java -jar androidviewclient-<version>.jar
usage: java -jar androidviewclient.jar COMMAND [OPTION]... [ARGS]...

Commands:
  dump
  culebra

dump

$ java -jar androidviewclient-<version>.jar dump

culebra

$ java -jar androidviewclient-<version>.jar culebra

If you receive

ERROR: monkeyrunner was not found and Windows 7 does not support shebang in scripts. Aborting.

it may be due to monkeyrunner not found in PATH.

Plugin

This jar file can be used with shebang command line adding the -plugin option to monkeyrunner. For example:

#! /usr/local/bin/shebang monkeyrunner -plugin $AVC_HOME/bin/androidviewclient-$AVC_VERSION.jar @!

Check the shebang page to see all the details for different platforms and OSs.

Alternatively, if your are suffering an operating system that does not support shebang you can use:

c:>\path\to\monkeyrunner -plugin \path\to\androidviewclient-<version>.jar myscript.py

Source

You only need to clone the repository or download the ZIP source distribution (which you have to unzip for AndroidViewClient to work) and set the environment variable ANDROID_VIEW_CLIENT_HOME or PYTHONPATH to allow the monkeyrunner interpreter to find the required modules.

You may also need to set ANDROID_HOME environment variable to locate Android SDK installation and tools.

Building androidviewclient library

Once you have the source downloaded you may want to create the androidviewclient library yourself, the one that is mentioned in Installation - Binary.

$ cd /path/to/androidviewclient
$ cd AndroidViewClient
$ ant

You will find the library in the bin directory.

Documentation

Documentation can also be downloaded from Github releases.

Previous versions were avaiable trough Google Drive.

Verifying environment setup

Before you begin, you can verify that your environment has been correctly setup after following the steps mentioned in Installation.

There is a simple script in examples that would help you do this verification.

$ cd /path/to/androidviewclient
$ cd AndroidViewClient
$ cd examples
$ monkeyrunner check-import.py

if everything is fine, you will receive

sys.path= ['/opt/android-sdk/tools/lib/monkeyrunner.jar', '/home/user/tmp/test/AndroidViewClient-master/AndroidViewClient/examples', '/opt/android-sdk-linux/tools/lib/Lib', '/opt/android-sdk-linux/tools/lib/jython-standalone-2.5.3.jar/Lib', '__classpath__', '__pyclasspath__/', '/home/user/tmp/test/AndroidViewClient-master/AndroidViewClient/src']
OK

however, if something went wrong you will receive the corresponding exception

130607 12:00:39.199:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130607 12:00:39.199:S [main] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
  File "/home/user/tmp/test/AndroidViewClient-master/AndroidViewClient/examples/check-import.py", line 25, in <module>
    import com.dtmilano
ImportError: No module named dtmilano

this is due to the fact that monkeyrunner was not able to find the requested modules. To solve the problem you must set ANDROID_VIEW_CLIENT_HOME environment variable pointing to the directory containing the modules.

$ export ANDROID_VIEW_CLIENT=/path/to/androidviewclient/AndroidViewClient

Also, as a quick check of having the right directory set in ANDROID_VIEW_CLIENT you can do

$ cd $ANDROID_VIEW_CLIENT
$ ls -l

and the directories and files in the folder should be similar to

total 40
-rw-rw-r-- 1 user group 1994 Jun  5 13:51 build.xml
drwxrwxr-x 2 user group 4096 Jun  5 13:51 doc
drwxrwxr-x 2 user group 4096 Jun  7 11:50 examples
-rw-rw-r-- 1 user group  206 Jun  5 13:51 project.properties
drwxrwxr-x 3 user group 4096 Jun  5 13:51 src
drwxrwxr-x 3 user group 4096 Jun  5 13:51 tests
drwxrwxr-x 2 user group 4096 Jun  5 13:51 tools
drwxrwxr-x 2 user group 4096 Jun  5 13:51 uml
-rw-rw-r-- 1 user group 4416 Jun  5 13:51 viewclient.epydoc

Alternatively, you can set PYTHONPATH environment variable instead of ANDROID_VIEW_CLIENT_HOME. The only advantage in using this is that Eclipse and PyDev set this variable automatically from your project configuration. so if you are running scripts from the IDE there are no extra steps.

However, if you are running scripts from the command line you can still use this variable. The only thing you should take into account is that now the /src directory must be part of the variable

$ export PYTHONPATH=/path/to/androidviewclient/AndroidViewClient/src

Select the correct implementation

Depending on various conditions some implementation may be the correct for your case.

View full size image

See also:

Example

This is just an illustrative quick example of AndoidViewClient capabilities. The complete set of examples can be found here.

Device

The device shows several shortcuts, like Email, Camera, Settings, Gallery and the Clock widget.

Source

dump.py has evolved from this simple script to a full fledged tool that will be soon moved to the tools directory.

See the dump.py for details.

We call this script dump.py

#! /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.
# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
        if not p in sys.path:
            sys.path.append(p)
except:
    pass
    
try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass
   
from com.dtmilano.android.viewclient import ViewClient

ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)

Result

Running the previous script as:

$ ./dump.py [serialno]

produces:

  ...
  com.android.launcher2.CellLayout id/cell3 None
              com.android.launcher2.ShortcutAndWidgetContainer NO_ID None
                 com.android.launcher2.BubbleTextView NO_ID Email
                 com.android.launcher2.LauncherAppWidgetHostView NO_ID None
                    android.widget.AnalogClock id/0x7f0f0014 None
                 com.android.launcher2.BubbleTextView NO_ID Camera
                 com.android.launcher2.BubbleTextView NO_ID Settings
                 com.android.launcher2.BubbleTextView NO_ID Gallery
                 com.android.launcher2.LauncherAppWidgetHostView NO_ID None
                    android.widget.LinearLayout id/0x7f080167 None
  ...
Clone this wiki locally