Monkeyrunner tool for Android Automatic Testing

Source: Internet
Author: User

MonkeyrunnerTools 

Preface:

Recently, I started to study Android automated testing methods, and made some simple arrangements for some of the tools, methods, and frameworks, this includes the android testing framework, CTS, Monkey, Monkeyrunner, benchmark, and other test tools. Due to the short contact time and shortcomings in many places, I hope to have more exchanges with you.


1. What is monkeyrunner?

The monkeyrunner tool provides an API to control Android devices and Simulators in addition to Android code. With monkeyrunner, you can write a Python program to install an Android Application or test package, run it, send a simulated press key to it, and capture its user interface image, and stored on the workstation. The primary purpose of the monkeyrunner tool is to test applications and devices at the functional/framework level, or to run the unit test suite, but you can also use it for other purposes.

Ii. Differences between the monkeyrunner tool and the Monkey Tool

Monkey:

The Monkey tool runs directly in the adb shell of the device or simulator to generate a pseudo-random event stream of the user or system.

Monkeyrunner:

The monkeyrunner tool is a specific command and event control device or simulator defined on the workstation through APIS.

Iii. monkeyrunner test Type

1. Multi-Device Control: monkeyrunner
APIS can implement Test suites across multiple devices or simulators. You can connect to all devices at the same time or start all simulators (or all together), connect to each device in sequence based on programs, and then run one or more tests. You can also use a program to start a configured simulator, run one or more tests, and then close the simulator.

2. function test: monkeyrunner can automatically perform a function test for an application. You can provide the input values of buttons or touch events, and then observe the screenshots of the output results.

3. regression testing: monkeyrunner can run an application and compare the result screenshots with the results screenshots that are known to be correct to test the stability of the application.

4. scalable automation: Since monkeyrunner is an API toolkit, you can develop a complete set of systems based on Python modules and programs to control Android devices. Besides using monkeyrunner
In addition to APIs, you can use standard Python OS and subprocess modules to call Android
Android tools such as Debug Bridge.

4. Run monkeyrunner

You can directly use a code file to run monkeyrunner, or enter the monkeyrunner statement in the Interactive dialog box. Either way, you need to call the monkeyrunner command under the tools subdirectory of the SDK directory. If you provide a file name as a running parameter, monkeyrunner regards the file content as a Python program and runs it. Otherwise, it provides an Interactive dialog environment.

The command syntax of monkeyrunner is:

Monkeyrunner
-Plugin <plugin_jar> <program_filename> <program_options>

V. Instances

Take apidemos in sampleas an example to generate apidemos.apk first.

Premise: A device is connected.

1. Place apidemos.apk under $ Android_Root \ tools.

2. Create a monkeyrunnerprogram. py file under $ Android_Root \ tools with the following content:

 1 # Imports the monkeyrunner modules used by this program
2  
3  from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
4
5  # Connects to the current device, returning a MonkeyDevice object
6  
7 device = MonkeyRunner.waitForConnection()
8
9  # Installs the Android package. Notice that this method returns a boolean, so you can test
10  
11  # to see if the installation worked.
12  
13 device.installPackage('./ApiDemos.apk')
14
15
16  # Runs the component
17  
18 device.startActivity(component='com.example.android.apis/.ApiDemos')
19
20
21  # Presses the Menu button
22  
23 device.press('KEYCODE_MENU','DOWN_AND_UP')
24
25
26  # Takes a screenshot
27  
28 result = device.takeSnapshot()
29
30
31  # Writes the screenshot to a file
32  
33 result.writeToFile('./shot1.png','png')

Note: Some examples on the SDK are incorrect and cannot be copied directly. Otherwise, an error occurs during command execution. You can compare it with the above Code.
 

3. Open the command line and go to the Android_Root \ tools directory to run the following command:

Monkeyrunner
Monkeyrunnerprogram. py

110307
15:33:19. 625: I [main] [com. android. monkeyrunner. MonkeyManager] Monkey Command:
Wake.

110307
15:33:20. 625: I [main] [com. android. monkeyrunner. MonkeyManager] Monkey Command:
Wake.

110307
15:33:21. 625: I [main] [com. android. monkeyrunner. MonkeyManager] Monkey Command:
Wake.

110307
15:33:22. 718: S [pool-1-thread-1] [com. android. monkeyrunner. adb. AdbMonkeyDevice]
Error starting command: monkey -- port 12345

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice] com. android. ddmlib. ShellCommandUnresponsiveException

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At com.android.ddmlib.AdbHelper.exe cuteRemoteCommand (AdbHelper. java: 408)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At com.android.ddmlib.Device.exe cuteShellCommand (Device. java: 276)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At com. android. monkeyrunner. adb. AdbMonkeyDevice $ 1.run( AdbMonkeyDevice. java: 89)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At java. util. concurrent. Executors $ RunnableAdapter. call (UnknownSource)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At java. util. concurrent. FutureTask $ Sync. innerRun (Unknown Source)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At java. util. concurrent. FutureTask. run (Unknown Source)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At java. util. concurrent. ThreadPoolExecutor $ Worker. runTask (Unknown
Source)

110307
15:33:22. 718: S [pool-1-thread-1] [com. android. monkeyrunner. adb. AdbMonkeyDevice]
Java. util. concurrent. ThreadPoolExecutor $ Worker. run (UnknownSource)

110307
15:33:22. 718: S [pool-1-thread-1]
[Com. android. monkeyrunner. adb. AdbMonkeyDevice]
At java. lang. Thread. run (UnknownSource)

110307
15:33:57. 437: I [main] [com. android. monkeyrunner. MonkeyManager] Monkey Command: press KEYCODE_MENU.

110307
15:33:59. 171: I [main] [com. android. monkeyrunner. MonkeyManager] Monkey Command: quit.

Note: we can ignore the exception prompt, because we can see that the Monkey Command: press KEYCODE_MENU has been successfully executed.

4. shot1.png can be generated under Android_Root \ tools.

Vi. instance Scaling

Because no MENU appears when you press the MENU key on the ApiDemos homepage, you can continue the experiment on the basis of instance 5 for better visualization:

1. Create a monkeyrunnerprogram1.py file under $ Android_Root \ tools with the following content:

 1 # Imports the monkeyrunner modules used by this program
2  
3  from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
4
5 # Connects to the current device, returning a MonkeyDevice object
6
7 device = MonkeyRunner.waitForConnection()
8
9 # Takes a screenshot
10
11 result = device.takeSnapshot()
12
13 # Writes the screenshot to a file
14
15 result.writeToFile('./shotbegin.png','png')
16
17 # Presses the Down button
18
19 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
20
21 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
22
23 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
24
25 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
26
27 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')
28
29 # Takes a screenshot
30
31 result = device.takeSnapshot()
32
33 # Writes the screenshot to a file
34
35 result.writeToFile('./shotend.png','png')

2. Locate the screen on the homepage of Apidemos and place the cursor on the first item.

3. Run the following command in the $ Android_Root \ tools directory:

Monkeyrunner
Monkeyrunnerprogram1.py

4. During the running process, we can see that the cursor keeps moving down and can customize the following in the current directory:

Before running: shotbegin.png

 

After running (five moves down): shotend.png

References:

Http://developer.android.com/guide/developing/tools/monkeyrunner_concepts.html
 

Next article: Introduction to benchmark test for Android Automatic Testing


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.