Recording and playback of Monkeyrunner scripts

Source: Internet
Author: User

Following the previous Monkeyrunner environment: http://www.cnblogs.com/zh-ya-jing/p/4351245.html, we can further study the Monkeyrunner.

I was just in touch with Monkeyrunner soon, I was interested in Monkeyrunner's script recording function, so I learned a bit. Did not expect to encounter a lot of problems in the middle, before the recording script does not pass, and then the phone is not connected, Monkeyrunner can not run up, in the final analysis of the problem of recording scripts, after the great God consulted, can be considered to be able to record the script successfully.

Don't know for what purpose, Google put Monkeyrunner's script recording function snow hidden, need to from the Android source code to dig it out. Monkey_recorder.py is used to record the action sickness generated script on the device, and monkey_playback.py the script back and forth.

Create a new monkey_recorder.py file with the following code:

#!/usr/bin/env Monkeyrunner#Copyright, the Android Open Source Project##Licensed under the Apache License, Version 2.0 (the "License");#You are not a use of this file except in compliance with the License.#obtain a copy of the License at##http://www.apache.org/licenses/LICENSE-2.0##unless required by applicable law or agreed to writing, software#distributed under the License is distributed on a "as is" BASIS,#without warranties or CONDITIONS of any KIND, either express or implied.#See the License for the specific language governing permissions and#limitations under the License. fromCom.android.monkeyrunnerImportMonkeyrunner as Mr fromCom.android.monkeyrunner.recorderImportMonkeyrecorder as Recorderdevice=mr.waitforconnection () recorder.start (device)

Create a new monkey_playback.py file with the following code:

#!/usr/bin/env Monkeyrunner#Copyright, the Android Open Source Project##Licensed under the Apache License, Version 2.0 (the "License");#You are not a use of this file except in compliance with the License.#obtain a copy of the License at##http://www.apache.org/licenses/LICENSE-2.0##unless required by applicable law or agreed to writing, software#distributed under the License is distributed on a "as is" BASIS,#without warranties or CONDITIONS of any KIND, either express or implied.#See the License for the specific language governing permissions and#limitations under the License.ImportSYS fromCom.android.monkeyrunnerImportMonkeyrunner#The format of the file we are parsing is very carfeully constructed.#Each line corresponds to a single command. the line was split into 2#parts with a | character. Text to the left of the pipe denotes#which command to run. The text to the right of the pipe is a python#Dictionary (it can be evaled to existence) that specifies the#arguments for the command. In most cases, this directly maps to the#keyword argument dictionary that could being passed to the underlying#command.#Lookup table to map command strings to functions this implement that#command.Cmd_map = {    'TOUCH':LambdaDev, Arg:dev.touch (* *Arg),'DRAG':LambdaDev, Arg:dev.drag (* *Arg),' Press':LambdaDev, arg:dev.press (* *Arg),'TYPE':LambdaDev, arg:dev.type (* *Arg),'WAIT':LambdaDev, arg:MonkeyRunner.sleep (* *Arg)}#Process A single file for the specified device.defprocess_file (FP, device): forLineinchfp: (cmd, rest)= Line.split ('|')        Try:            #Parse the PydictRest =eval (rest)except:            Print 'Unable to parse options'            Continue        ifCmd not inchCmd_map:Print 'Unknown Command:'+cmdContinueCmd_map[cmd] (device, rest)defMain (): File= Sys.argv[1] FP= Open (file,'R') Device=monkeyrunner.waitforconnection () process_file (FP, device) Fp.close (); if __name__=='__main__': Main ()

To connect a real machine or simulator, execute the following command:

$monkeyrunner monkey_recorder.py

After execution, Monkeyrunner opens a window, as shown below.

It will continue to crawl the latest interface from the device, you can directly on the left side of the screen to click on the icon to simulate touch operation, do not directly manipulate the real computer or simulator, otherwise you cannot record the script. The recording script is displayed in real time on the right side of the operation, click the "type something" button to enter the string, and click the "Fling" button to simulate the swipe gesture. When the action recording is complete, click the Export Actions button to save the script to the specified directory, such as TEST.MR, and close the Monkeyrunner run window.

Pass the recorded script to the monkey_playback.py file to play back, execute the following command:

$monkeyrunner monkey_playback.py test.mr

If the playback process is wrong, it is possible that the real machine or simulator reaction is slow, the interval between the two operations is too short, so it is recommended to add some wait between two operations, that is, after each operation click the "Wait" button, increase the waiting time .

It is highly recommended to put monkey_recorder.py,monkey_playback.py and recorded script test.mr in the same directory , which reduces the probability of error, and if the intermediate script does not work, or if the operation fails, It is recommended that you try the absolute path .

Recording and playback of Monkeyrunner scripts

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.