[Turn] Monkeyrunner use example of Android automated Test (iii)

Source: Internet
Author: User

First, use the cmd command to open the emulator

You must run the appropriate emulator or connect the device before running Monkeyrunner, or Monkeyrunner cannot connect the device.

1.1 Use Elipse to open the Android emulator or open the emulator with the android command in CMD.

1.2 Here's the point. Open simulator with android command in CMD

Command:EMULATOR-AVD test (Note: The name of the test for the virtual device--AVD is full: Android virtual device, which is an Android-run VM, as shown in:)

The test in the above command is the emulator name. You need to change the actual name when you use it.

If normal, the simulator should be able to start up.

If the result of the execution appears the following error content:

[Plain]View Plaincopy
    1. Panic:could not open:c:\documents and Settings\sadministrator\.android/avd/test.ini

As shown in the following:

The reason is that your environment variable is missing a configuration. Please add "android_sdk_home" to "System variables" and set its value to "C:\Documents and Settings\Administrator" (Note: The value here cannot be C:\Documents and Settings\ Administrator\.android) as shown in:

When you are sure, close the cmd window and reopen cmd. Execute the above command. The emulator will be enabled.

Second, enter the Monkeyrunner

2.1 When the simulator starts successfully, we still operate in the CMD environment. Now enter Monkeyrunner's shell command interaction mode.

Command: Monkeyrunner

2.2 After entering the shell command interaction mode, the first thing is to import the module to be used by Monkeyrunner. Enter directly under the shell command:

From com.android.monkeyrunner import monkeyrunner,monkeydevice Enter

OK, so we can use Monkeyrunner to do the testing work.

There are two types of scenarios:

One: Enter the following command directly under the shell command;

Command description

Device=monkeyrunner.waitforconnection () #连接手机设备

Device.installpackage (".. /samples/android-10/apidemos/bin/apidemos.apk ") #安装apk包到手机设备.

Start any activity in it, just pass in the package and activity name. The command is as follows:
Device.startactivity (component= "Com.example.android.apis/com.example.android.apis.apidemos")
The emulator will automatically open the home page of the Apidemos application.

Device.reboot () #手机设备重启

Device.touch (300,300, ' down_and_up ')

Monkeyrunner.alert ("Hello") #在emulator上会弹出消息提示

Device.press (' Keycode_home ', monkeydevice.down_and_up)

Device.type (' hello ') #向编辑区域输入文本 ' Hello '

Two: Write the following command into a python file, such as test.py, and then we can run it directly from the command line through Monkeyrunner. For example, we still use the above example, the syntax is as follows: Monkeyrunner test.py next Monkeyrunner will automatically call test.py, and execute the statements in it, quite convenient.

Example: test.py

[Python]View Plaincopy
    1. From Com.android.monkeyrunner import Monkeyrunner,monkeydevice
    2. Device=monkeyrunner.waitforconnection ()
    3. Device.startactivity (component="your.www.com/your.www.com.testactivity")

Execute in CMD

Monkeyrunner test.py

There may be an error "can ' t open specified script file" as shown in:

The reason is that the Python script file path is incorrect. You can have the following solutions:

1. Store the test.py file in the same directory as the Monkeyrunner file. Can do: Monkeyrunner test.py call

2. Specify the location of the Python file. If the test.py file is in the D packing directory, this can be done: Monkeyrunner d:\test.py

Third, Monkeyrunner grammar

3.1 Monkeyrunner

The

Monkeyrunner tool provides an API for programs written using the API to directly control Android devices and emulators without using Android code, and we can write a Python program to install, run, Send analog keystrokes to the user interface and will be stored in workstation for the fine operation. The Monkeyrunner tool is designed primarily to test applications and devices on the application/framework layer, or to run unit test suites, or for other purposes.

3.2  Monkeyrunner provides the following unique features for Android testing:
1, multi-device control: Monkeyrunner The API can implement test suites across multiple devices or emulators. You can pick up all the devices at the same time or start all the emulators at once, connect to each one by program, and then run one or more tests. You can also use a program to start a configured emulator, run one or more tests, and then close the emulator.
2, Functional testing: Monkeyrunner can automatically implement a functional test for an application. You provide input values for keystrokes or touch events, and then observe the screenshots of the output results.
4. Regression test: Monkeyrunner can run an app and compare its result screenshot to a given known good result screen to test the stability of the application.
4, extensible Automation: Since Monkeyrunner is an API toolkit, we can develop a complete set of systems based on Python modules and programs to control Android devices. In addition to using the Monkeyrunner API, we can also use the standard Python OS and ubprocess modules to invoke Android tools such as Android Debug Bridge. For Android tools such as ADB, you can also add your own classes to the Monkeyrunner API.

3.3   Run Monkeyrunner

You can run Monkeyrunner directly using one of the code files, or enter the Monkeyrunner statement in an interactive conversation. Either way, you need to invoke the Monkeyrunner command under the tools subdirectory of the SDK directory. If you provide a file name as a running parameter, Monkeyrunner will view the file content as a Python program and run it, otherwise it will provide an interactive dialog environment.

3.4 Monkeyrunner Command syntax

Monkeyrunner-plugin <plugin_jar> <programe_filename> <programe_option>

3.5 Monkeyrunner API

Consists of three main modules
1. Monkeyrunner: This class provides a way to connect monkeyrunner and devices or emulators, and it provides a way to create a user interface display.
2, Monkeydevice: Represents a device or simulator. This class provides a way to install and unload packages, turn on activity, send keystrokes and touch events, run test packages, and more.
3, Monkeyimage: This class provides a way to capture the screen. This class provides methods for converting bitmaps into various formats, comparing two Monkeyimage objects, saving an image to a file, and so on.
3.6 Importing the required modules

Way One:

Import Sys
From Com.android.monkeyrunner import Monkeyrunner as Mr
From Com.android.monkeyrunner import Monkeydevice as MD
From Com.android.monkeyrunner import monkeyimage as Mi
If you alias an imported module, you should use an alias instead of the original name, or an error will occur.
For example, to connect a device or simulator, the command should be as follows:
Device=mr.waitforconnection ()

Way two:
From Com.android.monkeyrunner import Monkeyrunner,monkeydevice,monkeyimage

Way three:
Import Com.android.monkeyrunner
But when it comes to use, it's especially troublesome.
Device=com.android.monkeyrunner.monkeyrunner.waitforconnection ()

Mode four:
We can also give it an alias
Import Com.android.monkeyrunner as Cam
But when it comes to use, it's especially troublesome.
Device=cam. Monkeyrunner.waitforconnection ()

#等待连接到设备, connected to the emulator, returns the Monkeydevice object that represents the connected device. If no error is indicated, the connection is successful.
Parameter 1: Time-out, unit seconds, floating-point number. The default is to wait indefinitely.
Parameter 2: String DeviceID, specifying the device name. It is assumed that the current device (mobile phone priority, such as the mobile phone via USB cable connected to the PC, followed by the simulator).
Default connection: Device=monkeyrunner.waitforconnection ()
Parameter connection: device = mr.waitforconnection (1.0, ' emulator-5554 ')

3.7 Install the APK to be tested to the device or simulator

Device.installpackage (' myproject/bin/myapplication.apk ') #参数是相对或绝对APK路径
Path level with "/", cannot use "\", such as d:\www\a.apk, but should be written d:/www/a.apk
The installation returns true at this point, we can see the APK icon installed on the idle interface.


3.8 Remove the specified package from the device, including its related data and adjust the cache

Device.removepackage (' myproject/bin/myapplication.apk ')
Delete succeeded returns true.


3.9 Activate any activity

Device.startactivity (component= "your.www.com/your.www.com.testactivity")
Or
Device.startactivity (component= "your.www.com/.testactivity")

At this point, you can send to the simulator, such as keystrokes, scrolling, storage and other operations.


Executes an adb shell command and returns the result, if any
Device.shell ("...")

3.10 Pause the number of seconds specified by a program that is currently running
Monkeyrunner.sleep (number of seconds, floating point)

3.11 Capture Screen Write file

Gets the screen buffer of the device, resulting in a masking capture of the entire display. ()
Result=device.takesnapshot ()
Return a Monkeyimage object (bitmap wrapper), we can save the diagram to a file using the following command
Result.writetofile (' takesnapshot\\result1.png ', ' png ')

Monkeyimage.writetofile (Parameter 1: Output file name, can also include path, Parameter 2: Destination format)
Write success returns true, otherwise false

3.12 Type
The type on the keyboard specifies the string, which is equivalent to requiring the characters in each string to be pressed (key code, DOWN_AND_UP).
String sent to keyboard
Device.type (' string ')

3.13 Wake-up device screen (wakes up on the device screen)

Device.wake ()

3.14 reboot to specified boot program specified device

Device.reboot ()

3.15 Touch, Press

Sends a touch event at a specified location (in pixels for x, y)

Device.touch (x,y,touchpresstype-touch Event type)

A key event that is sent to the specified key

Device.press (Parameter 1: Key code, Parameter 2: Touch event type)
Parameter 1: see android.view.KeyEvent
Parameter 2, if there are Touchpresstype () return type-touch event type, there are three kinds.
1, down sends a down event. Specifies that the down event type is sent to the device, corresponding to pressing a key or touching the screen.
2, up sends an up event. Specifies that the up event type is sent to the device, corresponding to releasing a key or lifting from the screen.
3, Down_and_up sends a down event, and then an up event. Corresponds to the input key or tap the screen.
The above three events are the press () or touch () parameters. The original English is as follows:
Use this with the type argument of press () or touch () to send a down event.


to simulate the input key, send down_and_up. Some specific content logic for


Parameter 1:

Press the HOME key device.press (' Keycode_home ', monkeydevice.down_and_up)  
Press the Back key device.press (' Keycode_back ', MONKEYDEVICE.DOWN_AND_UP)  
Press the Down navigation key device.press (' Keycode_dpad_down ', monkeydevice.down_and_up)  
Press the upper navigation key device.press (' keycode_dpad_up ', monkeydevice.down_and_up)  
Press OK device.press (' Keycode_dpad_center ', monkeydevice.down_and_up)


device.press (' Keycode_enter ', monkeydevice.down_and_up) #输入回车
Device.press (' Keycode_back ', monkeydevice.down_and_up) #点击返回

HOME key keycode_home 
Back key Keycode_back  
Send key keycode_call 
End key keycode_endcall 
Upper navigation key keycode_dpad_up 
Down navigation key Keycode_ dpad_down 
left navigation keycode_dpad_left 
Right navigation key keycode_dpad_right  
OK keycode_dpad_center 
Volume key keycode_volume_up  
Volume key keycode_volume_down 
POWER key keycode_power 
Camera key keycode_camera 
MENU key keycode_menu 


MORE: http://developer.android.com/reference/android/view/KeyEvent.html

[Turn] Monkeyrunner use example of Android automated Test (iii)

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.