Preface

The MonkeyRunner is a tool provided by Google Android Development Team and released with Android SDK. The MonkeyRunner is consisted of a set of command and use Jython script as its interpreting program language. The Jython script is process by Java VM rather than C/C++. We can add some logic statement into the script for more flexible control by  Jython script.

Before you start to read this article, you need to know how to set up the environment of monkeyrunner. If you don’t know how to do it, you can read my another article at http://android-test-tw.blogspot.com/2012/10/android-automation-test-by-monkeyrunner.html in this blog in advance.

Brief Jython:

Let’s brief Jython Script for monkeyrunner below before we go over next section.

The variable for Jython language, it can manipulate several type of variable such as Number, String, and List. The variable in Jython doesn’t need to be declared. All you have to do is to assign to value to it.

Let’s take a look some simple example and description below. You can copy the script to a text file and save  it to a file named example.py (file name can be any valid file name in your system). Open a cmd window and type monkeyrunner.bat example.py and press enter key on your computer.




Assign a number to a variable.

num = 1


Assign a string to a variable.

str = "Hello"

str = ‘Hello2′


Assign a list to a variable.

list = ['mail','inbox',100,200]

The logic statement and loop control are described as follows,

The if statement in Jython is as follows,


if_stmt ::=  "if" expression ":" suite


             ( "elif" expression ":" suite )*


             ["else" ":" suite]

Ex:

if num < 0:

         num = 0

         print(‘Negative changed to zero’)

    elif num == 0:

         print(‘Zero’)

    elif num == 1:

         print(‘Single’)

    else:

         print(‘More’)


The while statement in Jython is as follows,


while_stmt ::=  "while" expression ":" suite


                ["else" ":" suite]

Ex:


 

num = 100

go = True

while go:

    getNum = int(raw_input(‘Input a number : ‘))

    if getNum == num:

        print ‘That is right.’

        go = False

    else:

        print ‘Oh No~’


else:

    print ‘Game over.’

print ‘Done’

The for statement in Jython is as follows,


for_stmt ::=  "for" target_list "in" expression_list ":" suite





              ["else" ":" suite]


Ex:

for i in range(9):

print(i)

else:

 print ‘done’


A simple example:

First, you need to bring up a cmd window on windows operation system and attach your Android device to computer and check if the computer can detect you ADB interface of Android powered device.


 8D8EF1FDDA4F0EEFEC2BCE4DA12455  In this example we want to verify the result of run-time image and make decision for next action. At this case, we need to add logic statement in monkeyrunner. The logic script refers Jython Script. However we can refer to Python script tutorial for writing statement because Jython uses the same script with Python. Note, not all the Python script and components/class you can use on monkeyrunner. However you can try to use it.

Following example shows you how to turn on Bluetooth radio on your Android device and check the UI (user interface) if Radio icon has set to Bluetooth On. And turn off Bluetooth radio. The goal is to perform turn on and off Bluetooth Radio repeatedly and check the result by UI.

In this case we need to use a method of MonkeyRunner class that official web site doesn’t mention. That’s a API -  MonkeyRunner.loadImageFromFile(Full_File_Path);

In order to compare the screenshot for the testing result, we need to get a screenshot for pass criteria. We can use MonkeyRunner to get the screenshot by Jython script. The piece of script is as follows.

result = device1.takeSnapshot()


result.getSubImage ((133,85,85,28))


result.writeToFile(‘./capture1.png’,'PNG’)

The screenshot of Bluetooth On below was captured at settings/wireless screen on Android device and ran the above script. Note, the screen resolution is 240×432 and OS version is 4.1 in emulator.


 8E5977F770E95FA02B740C7B61C07F  capture1.png
Ex:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage


#Import class of MonkeyRunner, MonkeyDevice, and MonkeyImage.


device1 = MonkeyRunner.waitForConnection(10000,"emulator-5554")


print ‘connected’


#Connect Android device from computer and assign handler to variable -device1.


monkeyimage1 = MonkeyRunner.loadImageFromFile(‘./capture1.png’)


print ‘capture1.png loaded’


#Load a existing image file by load image API and assign handler to variable – monkeyimage1;


device1.press(‘KEYCODE_HOME’,'DOWN_AND_UP’,’ ‘)


print ‘home key are triggered’


#Send home key event to bring up Launcher


MonkeyRunner.sleep(3);


#Wait for reaction of Android device


device1.press(‘KEYCODE_MENU’,'DOWN_AND_UP’,’ ‘)


#Send menu key event


MonkeyRunner.sleep(3);


#Wait for reaction of Android device


device1.touch ( 95, 417, "DOWN_AND_UP")


#Touch system settings in menu list


MonkeyRunner.sleep(3);


#Wait for reaction of Android device


for i in range(1000):


   device1.touch ( 197, 96, "DOWN_AND_UP")


   #Touch bluetooth On


   MonkeyRunner.sleep(2);


   monkeyimage2 = device1.takeSnapshot()


   #Take a screenshot on Android device and assign handler to variable monkeyimage2


   monkeyimage2 = monkeyimage2.getSubImage ((133,85,85,28))


   #Get specified rectangle from screenshot got by last step for the result we want to check and assign 
handler to variable monkeyimage2 again.


   if monkeyimage2.sameAs ( monkeyimage1, 1 ):


      print str(i+1)+’:pass’


   else:


      print str(i+1)+’:fail’


   #Compare the result if both images of pre-loaded and run-time screenshot are the same. 


   device1.touch ( 155, 95, "DOWN_AND_UP")


   #Touch bluetooth Off


else:


   print ‘done’


Reference: Python Script Language Tutorial,  http://docs.python.org/tutorial/

Jython Script Lanuage Tutorial,  http://www.jython.org/docs/index.html

MonkeyRunner, http://developer.android.com/tools/help/monkeyrunner_concepts.html

Android KeyCode,  http://developer.android.com/reference/android/view/KeyEvent.html

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()