Android MonkeyRunner Test.

今天学习了一下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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值