Chapter:--monkeyrunner Principle of automation basic article
NetEase Cloud Classroom:
http://study.163.com/course/courseLearn.htm?courseId=712011#/learn/video?lessonId=877115&courseId=712011
Main content and notes:
I. Theoretical knowledge and script presentation
The best way is the last officer Web document to see the introduction of Monkeyrunner, official online not to go to find a local Android 4.2 view, the basic content has not changed
First, what is Monkeyrunner?
The English documents are as follows:
The Monkeyrunner tool provides an API for writing programs, control an Android device or emulator from outside of Android OID code. With Monkeyrunner, the can write a Python program, the installs an Android application or test package, runs it, sends key Strokes to it, takes screenshots of their user interface, and stores screenshots on the workstation. The Monkeyrunner tool is primarily designed to test applications and devices at the functional/framework level and for run Ning Unit test suites, but is free to use it for other purposes.
The translation comes from:
This Monkeyrunner tool provides an API for everyone to program, to control an Android device or simulator, without having to know the Android source. With this monkeyrunner, we can write a Python program to install the Android program or test the package, run it, send it some keyboard control events, make the user interface, and store it in the workstation. The role of the Monkeyrunner tool is to test applications and devices on the function/frame layer, run unit test cases, and of course you can do other things with it, and we don't.
and Monkeyrunner and Monkey different Oh, monkey is using the adb shell command line, send a bunch of users or systems of random simulation events, mainly used for stress testing.
What can second,monkeyrunner do?
The English documents are as follows:
The Monkeyrunner tool provides these unique features for Android testing:
The translation comes from:
Monkeyrunner offers the unique features of some of the following Android tests:
1, multi-device control: That is to say can cross the device.
2, Functional testing: can be used on an Android app to run a test from start to finish, you need to provide keyboard or touch event value, by the screenshot to view the results.
3. Regression test: Monkeyrunner can test the stability of the application by running an application and comparing its output to the standard correct output set. Generally, when the function is relatively stable, the next time you return, it is necessary to do a basic functional regression, and it will save resources through automation.
4. Extensible automated Testing: Monkeyruner itself is an API gadget, and we can certainly develop a Python-based module or program to control Android devices. Of course, you can also use the standard Python OS or the Subprocess module to invoke Android tools such as ADB.
How to use Third,monkeyrunner?
The specific code examples available on the website are as follows:
1 #Imports the Monkeyrunner modules used by this program2 fromCom.android.monkeyrunnerImportMonkeyrunner, Monkeydevice3 4 #connects to the current device, returning a Monkeydevice object5device =monkeyrunner.waitforconnection ()6 7 #installs the Android package. Notice that this method returns a Boolean, so can test8 #To see if the installation worked.9Device.installpackage ('myproject/bin/myapplication.apk')Ten One #sets a variable with the package ' s internal name APackage ='com.example.android.myapplication' - - #sets a variable with the name of the Activity in the theActivity ='com.example.android.myapplication.MainActivity' - - #sets the name of the component to start -Runcomponent = package +'/'+Activity + - #Runs the component +Device.startactivity (component=runcomponent) A at #presses the Menu button -Device.press ('Keycode_menu', MONKEYDEVICE.DOWN_AND_UP) - - #Takes a screenshot -result =device.takesnapshot () - in #writes the screenshot to a file -Result.writetofile ('Myproject/shot1.png','PNG')
OK, the specific steps are as follows:
(1) First to import the Monkeyruner and Monkeydevice class
(2) Connect the device, devices = Monkeyrunner.waitforconnection (), wait for the device to be connected, and return an instance of one device
(3) Install the installation package (write the absolute path is OK), here will return the Boolean variable, you can use this value, write an If statement, determine whether the installation succeeded
(4) After installing the package name (configured in manifest.xml), get the name of the activity in the package, how is this obtained?
Here, plug in a word and use Python to get the name of the package and the main activity:
http://blog.csdn.net/pugongying1988/article/details/7349068
Here's how to get it, but you'll need to configure the Android environment on your phone or simulator, where Python for Android is used, and can be obtained through API interfaces of some threads, or you can write a Java program to get
(5) Run with package name plus activity name, run through StartActivity.
(6) Start the button, or touch the event.
(7) Cut the result (6) in a graph
(8) write to a directory, save it, then you can compare with the correct results. But the results of how the comparison is not yet known.?????
Own an APK program, write a test code in Python, run the automation:
1 # Coding=utf-8 2 3 from Import Monkeyrunner, Monkeydevice 4 device = monkeyrunner.waitforconnection ()5 Device.shell ("monkey-p Com.qihoo.linker-v")
Like the above, but
(1) There is a problem is a lot of pages, there will be a jump between the interface, this situation how to do monkey stress test it?
Because I am in the process of testing, some randomly generated events should be able to click the Home button or the return key, which may cause the application to quit, how to handle?
(2) For a single page, it can be down_and_up by coordinate point mode, or some other operation.
Second, package Monkeyrunner recording
To start the instance code:
Java calls Android Automation's Monkeyrunner, the specific program structure is:
First, because I'm a high-version jar package, I need to start with sdk/tools/ Under Lib, import the Chimpchat.jar package (used to define Adbbackend instances and Adbchimpdevice instances, establish ADB bridging and build device instances), and also import Monkeyrunner.jar (to define Monkeyrecorederframe instances, This is used for recording, and then enter the object you need to define directly below, you can import the corresponding class class
Source code Mytest.java as follows, the test process is as follows:
1 Packagecom.android.test;2 3 ImportCom.android.chimpchat.adb.AdbBackend;4 ImportCom.android.chimpchat.adb.AdbChimpDevice;5 ImportCom.android.monkeyrunner.recorder.MonkeyRecorderFrame;6 7 8 Public classMyTest {9 Ten /** One * @paramargs A */ - - Private Staticadbchimpdevice device; the Private Staticadbbackend adb; - - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub + - if(adb==NULL){ +ADB =Newadbbackend (); A //parameters are their own defined wait connection time and device ID, which can also be empty at //Here's the type of adb you need to look at -device =(Adbchimpdevice) adb.waitforconnection (); - - if(Device! =NULL) - { -Monkeyrecorderframe frame =Newmonkeyrecorderframe (device); inFrame.setvisible (true); - } to } + - } the *}
Next is the test process, right-mytest.java,run As->java application can, may start up slower (need to connect your own phone, you can use the cmd command under the ADB devices to see if the connection is successful, If there is a unauthorized problem, you can find the developer options, select "Recall USB error Authorization" Can, I all mobile phone for the Samsung Galaxy S3, other phones are similar; after reseating the phone, the connection will appear after the authorization prompt, click OK)
Then the program runs like this:
After clicking on the screen or selecting press a button, there will be a response that will see where the click is on the right.
Then click on the export Actions above, enter the file name such as a, do not want suffix name, save in their own can remember the directory can, and then open with notepad++, as follows:
press| {' name ': ' HOME ', ' type ': ' Downandup ',}
touch| {' x ': 571, ' y ': 370, ' type ': ' Downandup ',}
touch| {' X ': 438, ' y ':, ' type ': ' Downandup ',}
touch| {' X ': 162, ' Y ': 312, ' type ': ' Downandup ',}
touch| {' x ': +, ' y ': 509, ' type ': ' Downandup ',}
touch| {' X ': 227, ' Y ': 730, ' type ': ' Downandup ',}
touch| {' x ': 497, ' y ': 666, ' type ': ' Downandup ',}
touch| {' X ': 351, ' y ': 925, ' type ': ' Downandup ',}
touch| {' X ': ', ' y ': 960, ' type ': ' Downandup ',}
touch| {' x ': 535, ' y ': 968, ' type ': ' Downandup ',}
touch| {' x ': $, ' y ': 296, ' type ': ' Downandup ',}
touch| {' x ': 630, ' y ': 224, ' type ': ' Downandup ',}
press| {' name ': ' HOME ', ' type ': ' Downandup ',}
touch| {' x ': 848, ' y ': 514, ' type ': ' Downandup ',}
touch| {' X ': ', ' y ': 389, ' type ': ' Downandup ',}
Features of the A-mode (coordinate point) < note: There is also a control-based B-mode >
Here's how to deal with different resolutions:
If 320*480, how to transplant to the top of 480*600?
Device.touch (340,580, ' down_and_up ')
320/x = 480/newx, x = 340
480/y = 600/newy, y = 580
This will be able to obtain a proportional relationship, using this proportional relationship to do a similar macro substitution, the code is basically not moving, or even write a method, obtained after the pixel, and the basis of the calculation, the direct replacement can be.
In addition to the above, you can also use Hierarchyviewer to get controls and find coordinates through controls. Disadvantage: To open root,4.1.2 below to open root. So my cell phone is not showing! After that, you can do it with uiautomator, without having to root.
Iv. Follow-up continuous integration
Here is a robotium recorder tool that is used for scripting, with only a 30-day trial period.
1. Official website: www.goldensunshine.cc
2, Baidu Search: Golden Sunshine Test, find the golden Sun study Materials and official website, video.
3, the official Sina Weibo: Golden Sun Woody
4. Official qq:212260449
6, the official YY education channel: 74894998
7, the official CSDN technical information: http://blog.csdn.net/haorenmin2008