您的位置:首页 > 移动开发 > Android开发

Android: Using monkey from Java(三)

2012-01-07 09:26 204 查看
           昨天终于找到了一篇讲解在windows下用eclipse如何使用Monkey的教程,其实我是想使用MonkeyRunner的,带着好奇我试验了一把,发现还是有一些问题,现在共享出来与大家一起探讨。

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 project
Our 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.java
Our 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

listing all the properties in the device or emulator

shutting down the connection

Following, is the JavaMonkey class: 

/**

 * Copyright (C) 2011  Diego Torres Milano

 */

package com.example.javamonkey;

import java.util.TreeMap;

import com.android.chimpchat.ChimpChat;

import com.android.chimpchat.core.IChimpDevice;

/**

 * @author diego

 *

 */

public class JavaMonkey {

      //private static final String ADB = "/Users/diego/opt/android-sdk/platform-tools/adb";

     private static final String ADB = "E:/Android/androidSDKwindows/platform-tools/adb";

        private static final long TIMEOUT = 5000;

        private ChimpChat mChimpchat;

        private IChimpDevice mDevice;

        /**

         * Constructor

         */

        public JavaMonkey() {

                super();

        TreeMap<String, String> options = new TreeMap<String, String>();

        options.put("backend", "adb");

        options.put("adbLocation", ADB);

        mChimpchat = ChimpChat.getInstance(options);

        }

        /**

         * Initializes the JavaMonkey.

         */

        private void init() {

                mDevice = mChimpchat.waitForConnection(TIMEOUT, ".*");

                if ( mDevice == null ) {

                        throw new RuntimeException("Couldn't connect.");

                }

                mDevice.wake();

        }

        /**

         * List all properties.

         */

        private void listProperties() {

                if ( mDevice == null ) {

                        throw new IllegalStateException("init() must be called first.");

                }

                for (String prop: mDevice.getPropertyList()) {

                        System.out.println(prop + ": " + mDevice.getProperty(prop));

                }

        }

        /**

         * Terminates this JavaMonkey.

         */

        private void shutdown() {

                mChimpchat.shutdown();

                mDevice = null;

        }

        /**

         * @param args

         */

        public static void main(String[] args) {

                final JavaMonkey javaMonkey = new JavaMonkey();

                javaMonkey.init();

                javaMonkey.listProperties();

                javaMonkey.shutdown();

        }

}

Configuration
One of the important things you have to adapt to your environment is the location of the adb command. Otherwise if you don't set it you will receive:

E/adb: Failed to get the adb version: Cannot run program "adb": error=2, No such file or directory

Hope this helps you get started with chimpchat. As always, comments and questions are always welcome.

 

按照以上步骤我直接运行了JavaMonkey,此时未启动模拟器,出现错误如下:

Exception in thread "main" java.lang.RuntimeException: Couldn't connect.

 at com.example.javamonkey.JavaMonkey.init(JavaMonkey.java:40)

 at com.example.javamonkey.JavaMonkey.main(JavaMonkey.java:70)

 

模拟器启动后错误如下:

2012-1-7 9:25:02 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: wake.

2012-1-7 9:25:03 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: wake.

2012-1-7 9:25:04 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: wake.

2012-1-7 9:25:05 com.android.chimpchat.adb.AdbChimpDevice$1 run
信息: Error starting command: monkey --port 12345

com.android.ddmlib.ShellCommandUnresponsiveException

 at com.android.ddmlib.AdbHelper.executeRemoteCommand(Unknown Source)

 at com.android.ddmlib.Device.executeShellCommand(Unknown Source)

 at com.android.chimpchat.adb.AdbChimpDevice$1.run(Unknown Source)

 at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

 at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

 at java.util.concurrent.FutureTask.run(Unknown Source)

 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

 at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

 at java.lang.Thread.run(Unknown Source)
2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: wake.

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: wake.

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: listvar.

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.action.

am.current.action:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.categories.

am.current.categories:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.comp.class.

am.current.comp.class:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.comp.package.

am.current.comp.package:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.data.

am.current.data:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar am.current.package.

am.current.package:

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.board.

build.board: unknown

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.brand.

build.brand: 1277931480000

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.cpu_abi.

build.cpu_abi: armeabi

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.device.

build.device: generic

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.display.

build.display: sdk-eng 2.2 FRF91 43546 test-keys

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.fingerprint.

build.fingerprint: generic/sdk/generic/:2.2/FRF91/43546:eng/test-keys

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.host.

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.id.

build.host: android-test-25.mtv.corp.google.com

build.id: FRF91

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.manufacturer.

build.manufacturer: unknown

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.model.

build.model: sdk

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.product.

build.product: sdk

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.tags.

build.tags: test-keys

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.type.

2012-1-7 9:25:05 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.user.

build.type: eng

build.user: android-build

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.version.codename.

build.version.codename: REL

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.version.incremental.

build.version.incremental: 43546

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.version.release.

build.version.release: 2.2

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar build.version.sdk.

build.version.sdk: 8

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar clock.millis.

clock.millis: 1325899505918

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar clock.realtime.

clock.realtime: 198501

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar clock.uptime.

clock.uptime: 198517

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar display.density.

display.density: 0.75

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar display.height.

display.height: 400

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: getvar display.width.

display.width: 240

2012-1-7 9:25:06 com.android.chimpchat.ChimpManager sendMonkeyEventAndGetResponse

信息: Monkey Command: quit.

出现这个的错我可能是以下问题,请参考:

Diego Torres Milano said...
Perhaps money is not installed on the device you are using for testing. I've run it on my Nexus One with no problems.

Try opening a shell and running monkey from the command line to see if it's there.

 
测试Monkey结果如下:
# monkey --port 12345

monkey --port 12345

Error binding to network socket.

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息