2012年10月10日 星期三

Android Automation Test by Monkeyrunner

中文版
Preface

There're a lot of brand company used Android as their product operation system around the world in 2012. Every company add their value added feature and competition into the Android powered device. Since they've identical system base, they can be controlled by the same tool. That is monkeyrunner. The powerful tool - monkeyrunner is developed by Google Android team. Utilize the tool, you can save your cost on equipment and quantity of testing resource.

What does monkeyrunner stand for?

The monkeyrunner provide a set of command to control your Android device. The user can send the control command to Android powered device by computer such as touch screen, key pressed/hold, install/uninstall your App, limited Linux shell script, wake your device, inputing text message, reboot, sliding your screen, and save your device screen to image file on PNG or bitmap format...etc.

Where can we start to play it?

You need a Android emulator or Android powered device.

Install J2SDK into your computer. Either version 6 or 7 is applicable. Get it from following url in case you don't have it.
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install Android SDK. Get it from following url in case you don't have it.
http://developer.android.com/sdk/index.html

Google provides 3 kind of OS platform forAndroid SDK
1. Microsoft Windows
2. Linux (i386) (Ubuntu is Linux system and the best choice)
3. Mac OS X (intel)

Configure you ADB USB Driver

I'll introduce the content by using Microsoft Windows OS. Install your usb and adb interface driver by usb driver provided by Google Android SDK or Brand company's web site.

In case you are using Unbutu operation system for your Android device, you don't need a USB driver for ADB Interface.

I'll suggest add the path of folders of tools and platform-tools in Android SDK into environment variable - path.That's convenient when you open a command line windows and execute the commands for Android controlled.

Last step is to enable USB debug mode for Android device. Typically, this option is in Settings->Development->USB Debug

Finally, Check your computer recognize your Android powered device by command of adb devices as follows. The serial number of Android device is followed by words "device". That means Android device is connected to your computer and work properly.


After configuration is done, you can start to use monkeyrunner for controlling your Android powered devices.

Foundamental practice

The monkeyrunner command should be sent by computer and use adb connection. Let's have a basic example as follows.

Open a command window by command cmd at start->run on your computer and type monkeyrunner.bat.


At this moment, your can open another command window to check if your Android device is connected to computer successfully.

Type the following script to your monkeyrunner.bat command window. Note, you need to be careful for case sensitivity. The words can be different in meaning based on different use of uppercase and lowercase letters.

First line, the command of "from com.android.monkeyrunner import ..." is to loaded relative API from monkeyrunner.
Second line, assign the handler of Android device to variable - device.
Third line, send the menu key event to Android device.

Before sending press command.

After sending press command.

More benefit for you by monkeyrunner

Take a look on official Android development web site, monkeyrunner can help you to perform multiple Android device control, functional test, regression test, and extended automation. To be clear, monkeyrunner can help you to do the repeated operation or test and mitigate your requirement on human resource.

As we've talk about the benefit aforementioned, monkeyrunner can help you to touch screen, slide screen, monitor the variation of screen...etc. You can utilize monkeyrunner to record the scenario of specified operations and playback them for your testing purpose on Android powered device.

The monkeyrunner can help you to do the test over day and night without any complain. The tool is very  helpful for the product to meet the schedule of Time To Market.

Advanced practice

Let's have a example with more scenario for using monkeyrunner below. I'd like to introduce a test for Bluetooth on/off.

Let your Android powered device go into Launcher in advance and run the following script.
  monkeyrunner script
>>> device1.press('KEYCODE_MENU','DOWN_AND_UP',' ')
  Send menu key event, as figure-m2
>>> device1.touch ( 95, 417, "DOWN_AND_UP")
  Touch settings in menu list, as figure-m-3
>>> device1.touch ( 197, 96, "DOWN_AND_UP")
  Touch bluetooth On, as figure-m-4
>>> device1.touch ( 155, 95, "DOWN_AND_UP")
  Touch bluetooth Off, as figure-m-5


 figure-m1
 figure-m2
 figure-m3
 figure-m4
figure-m5


I'll list more useful examples for monkeyrunner to control Android device. Welcome to visit my blog again for the latest information in the future.

Reference
monkeyrunner, http://developer.android.com/tools/help/monkeyrunner_concepts.html , published by Google Inc.

10 則留言:

  1. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well iOS App Development Company

    回覆刪除
  2. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.

    PSD to Wordpress
    wordpress website development

    回覆刪除