今天学习了一下MonkeyRunner的基础环境创建。感觉MonkeyRunner可操作性更高于UiAutomator,可能是我使用的不多的原因吧。哈哈。
先记录下用MonkeyRunner录制和回放的脚本。
录制:
#!/usr/bin/env monkeyrunner
# Copyright 2010, The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "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.
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder
device = mr.waitForConnection()
recorder.start(device)
回放脚本:
import sys
from com.android.monkeyrunner import MonkeyRunner
CMD = {
'TOUCH': lambda dev, arg: dev.touch(**arg),
'DRAG': lambda dev, arg: dev.drag(**arg),
'PRESS': lambda dev, arg: dev.press(**arg),
'TYPE': lambda dev, arg: dev.type(**arg),
'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)
}
# Process a single file for the specified device.
def process_file(fp, device):
for line in fp:
(cmd, rest) = line.split('|')
try:
# Parse the pydict
rest = eval(rest)
except:
print 'unable to parse options'
continue
if cmd not in CMD:
print 'unknown command: ' + cmd
continue
CMD_MAP[cmd](device, rest)
def main():
file = sys.argv[1]
fp = open(file, 'r')
device = MonkeyRunner.waitForConnection()
process_file(fp, device)
fp.close();
if __name__ == '__main__':
main()
用这个东西可以去实现一些基本的自动操作,比如你已经十分讨厌的新手教程。。。录制一次,以后就不用在跑新手了。
下面是我自己练习写的一个脚本。安装CCA.APK,然后截了个图片。
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
device = MonkeyRunner.waitForConnection()
device.installPackage('CCA.apk')
MonkeyRunner.sleep(3)
MonkeyRunner.alert("CCA install ok","","")
package = 'com.bonbon.cca'
activity = 'com.unity3d.player.UnityPlayerActivity'
runComponent = package + '/' + activity
device.startActivity(component = runComponent)
MonkeyRunner.sleep(3)
MonkeyRunner.alert("START OK","","")
device.press('KEYCODE_MENU',MonkeyDevice.DOWN_AND_UP)
result = device.takeSnapshot()
result.writeToFile('shot1.png','png')
/**
*
*/
MonkeyRunner主要提供了以下几个接口
MonkeyRunner
void |
alert (
string message,
string title,
string okTitle)
Displays an alert dialog to the process running the current program.
|
integer |
choice (
string message,
iterable choices,
string title)
Displays a dialog with a list of choices to the process running the current program.
|
void |
help (
string format)
Displays the monkeyrunner API reference in a style similar to that of Python's
pydoc tool, using the specified format.
|
string |
input (
string message,
string initialValue,
string title,
string okTitle,
string cancelTitle)
Displays a dialog that accepts input.
|
void |
sleep (
float seconds)
Pauses the current program for the specified number of seconds.
|
MonkeyDevice
|
waitForConnection (
float timeout,
string deviceId)
Tries to make a connection between the
monkeyrunner backend and the specified device or emulator.
|
MonkeyDevice
string | DOWN | Use this with the type argument of press() or touch() to send a DOWN event. |
string | UP | Use this with the type argument of press() or touch() to send an UP event. |
string | DOWN_AND_UP | Use this with the type argument of press() or touch() to send a DOWN event immediately followed by an UP event. |
Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
broadcastIntent (
string uri,
string action,
string data,
string mimetype,
iterable categories
dictionary extras,
component component,
iterable flags)
Broadcasts an Intent to this device, as if the Intent were coming from an application.
| ||||||||||
void |
drag (
tuple start,
tuple end,
float duration,
integer steps)
Simulates a drag gesture (touch, hold, and move) on this device's screen.
| ||||||||||
object |
getProperty (
string key)
Given the name of a system environment variable, returns its value for this device. The available variable names are listed in the
detailed description of this method.
| ||||||||||
object |
getSystemProperty (
string key)
. The API equivalent of
adb shell getprop <key>. This is provided for use by platform developers.
| ||||||||||
void |
installPackage (
string path)
Installs the Android application or test package contained in packageFile onto this device. If the application or test package is already installed, it is replaced.
| ||||||||||
dictionary |
instrument (
string className,
dictionary args)
Runs the specified component under Android instrumentation, and returns the results in a dictionary whose exact format is dictated by the component being run. The component must already be present on this device.
| ||||||||||
void |
press (
string name,
dictionary type)
Sends the key event specified by type to the key specified by keycode.
| ||||||||||
void |
reboot (
string into)
Reboots this device into the bootloader specified by bootloadType.
| ||||||||||
void |
removePackage (
string package)
Deletes the specified package from this device, including its data and cache.
| ||||||||||
object |
shell (
string cmd)
Executes an
adb shell command and returns the result, if any.
| ||||||||||
void |
startActivity (
string uri,
string action,
string data,
string mimetype,
iterable categories
dictionary extras,
component component,
flags)
Starts an Activity on this device by sending an Intent constructed from the supplied arguments.
| ||||||||||
MonkeyImage
|
takeSnapshot()
Captures the entire screen buffer of this device, yielding a
MonkeyImage object containing a screen capture of the current display.
| ||||||||||
void |
touch (
integer x,
integer y,
integer type)
Sends a touch event specified by type to the screen location specified by x and y.
| ||||||||||
void |
type (
string message)
Sends the characters contained in message to this device, as if they had been typed on the device's keyboard. This is equivalent to calling
press() for each keycode in
message using the key event type
DOWN_AND_UP .
| ||||||||||
void |
wake ()
Wakes the screen of this device.
|
MonkeyImage
Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
string |
convertToBytes (
string format)
Converts the current image to a particular format and returns it as a
string that you can then access as an
iterable of binary bytes.
| ||||||||||
tuple |
getRawPixel (
integer x,
integer y)
Returns the single pixel at the image location (x,y), as an a
tuple of
integer, in the form (a,r,g,b).
| ||||||||||
integer |
getRawPixelInt (
integer x,
integer y)
Returns the single pixel at the image location (x,y), as a 32-bit
integer.
| ||||||||||
MonkeyImage
|
getSubImage (
tuple rect)
Creates a new
MonkeyImage object from a rectangular selection of the current image.
| ||||||||||
boolean |
sameAs (
MonkeyImage other,
float percent)
Compares this
MonkeyImage object to another and returns the result of the comparison. The
percent argument specifies the percentage difference that is allowed for the two images to be "equal".
| ||||||||||
void |
writeToFile (
string path,
string format)
Writes the current image to the file specified by
filename , in the format specified by
format .
|