• MonkeyRunner自动化测试


    一、环境搭建
    MonkeyRunner自动化测试同时需要Android SDK、JDK环境,分别需要搭建该配置环境。

    1.Android SDK环境搭建
    1). 到https://www.androiddevtools.cn/,下载Android SDK

    2). 通过此方式下载的sdk包,会缺少platform-tools平台工具,同步到https://developer.android.google.cn/studio/releases/platform-tools?hl=zh-cn下载工具

    3). platform-tools下载完成后,解压放入Android SDK根目录

    4). 配置Android SDK环境变量,打开电脑系统属性,点击环境变量,新建ANDROID_HOME,并输入Android SDK地址,完成之后双击path,把Android SDK变量添加进去

    5). 完成配置之后,打开cmd,输入adb,会显示版本信息,至此Android SDK环境配置成功。

    2.JDK环境搭建
    1). 到
    https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html官网下载JDK8版本,JDK8版本比较稳定

    2). 下载完成后,点击安装,并配置环境变量,可参考
    https://blog.csdn.net/u014282578/article/details/127833097,最后环境配置界面

    3). 配置完成后,打开cmd,输入java -version,会显示版本信息

    二、运行MonkeyRunner
    1.把手机通过数据线连接电脑,手机并打开开发者模式
    2.打开cmd,输入adb devices会显示手机设备信息

    3.接着输入monkeyrunner就会启动,启动显示该信息

    如启动失败,提示adb.exe失败,需要到Android SDK目录表修改monkeyrunner.bat

    4.输入指令,导入MonkeyRunner支持的类,并连接手机
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
    device = MonkeyRunner.waitForConnection()

    5.至此,手机连接成功,可以输入指令操作手机设备
    6.使用MonkeyRecorder录制回放脚本,新建monkey_recorder.py文件放入Android SDK目录F:/Downloads/android-sdk_r24.4.1-windows/android-sdk-windows/tools,并输入脚本指令
    #!/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)

    7.使用脚本方式启动MonkeyRecorder,重新打开cmd,输入monkeyrunner monkey_recorder.py,就可以打开MonkeyRecorder界面,录制回放脚本

    8.可以操作指定app,并在右边生成录制脚本

    9.点击Export Actions,导入脚本,命名为playback.mr

    10.退出MonkeyRecorder界面,新建monkey_playback.py回放脚本
    #!/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.

    import sys

    from com.android.monkeyrunner import MonkeyRunner

    The format of the file we are parsing is very carfeully constructed.

    Each line corresponds to a single command. The line is 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 into existence) that specifies the

    arguments for the command. In most cases, this directly maps to the

    keyword argument dictionary that could be passed to the underlying

    command.

    Lookup table to map command strings to functions that implement that

    command.

    CMD_MAP = {

    '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)
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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_MAP:
    
            print 'unknown command: ' + cmd
    
            continue
    
    
    
        CMD_MAP[cmd](device, rest)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    def main():

    file = sys.argv[1]
    
    fp = open(file, 'r')
    
    
    
    device = MonkeyRunner.waitForConnection()
    
    
    
    process_file(fp, device)
    
    fp.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    if name == ‘main’:

    main()

    11.使用回放脚本,回放创建的playback.mr,使用指令monkeyrunner monkey_playback.py playback.mr,即可实现指定触控功能自动化测试。

  • 相关阅读:
    [Spring Framework]注解开发③(依赖注入)
    安装VSCode,提升工作效率!iPad Pro生产力进阶之路
    ViT总结
    在OpenWRT上自动重拨号获取公网IP(手记)
    [附源码]SSM计算机毕业设计鲜花销售管理系统JAVA
    Nodejs搭建本地http服务器,通过【内网穿透】实现远程访问
    Chapter1 深度学习Pytorch环境搭建
    工作即将灭绝?AI大模型的入侵比你想象得还要快!
    MySQL进阶教程汇总
    电脑重装系统后如何给系统磁盘扩容空间
  • 原文地址:https://blog.csdn.net/lxy_tap/article/details/134459335