Thursday, November 10, 2011

Android: Using monkey from Java

The latest version of the Android SDK and tools include chimpchat, a library that facilitates the use of monkey from Java. This is equivalent to monkeyrunner, which is the bridge between monkey and the Python scripting language.While Python is an incredibly powerful and expressive scripting language and will permit you creating tests with just a few statements, there are some occasions when you don't want to introduce a new language to the project leaving your Java confort zone or you prefer to leverage the use of previously created libraries instead of writing new ones.In such cases, you can now have the same access to monkey running on the device with the help of chimpchat, as we are going to demonstrate.

Creating a Java projectOur first step will be to create a new Java project and we will add the required libraries to the Java Build Path as External Jars.We are naming the project JavaMonkey, for obvious reasons.

We are adding these libraries from Android SDK, which are used directly or indirectly by our project, to the Java Build Path:

chimpchat.jar

ddmlib.jar

guavalib.jar

sdklib.jar

JavaMonkey.javaOur intention is to create a simple class, serving the purpose of a simple example to get as started. We will be simply:

Creating a JavaMonkey object

initializing it, this implies creating the connection with any emulator or device found or throwing an exception is not connection was made before the timeout expires