博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UII自动化之MonkeyRunner
阅读量:2242 次
发布时间:2019-05-09

本文共 2406 字,大约阅读时间需要 8 分钟。

1、MonkeyRunner

1.1、MonkeyRunner介绍

    MonkeyRunner是Android SDK中提供的一套API(预先编译.class文件中的函数),就是{sdk安装目录}\tools\lib\monkeyrunner.jar,通过{sdk安装目录}\tools\monkeyrunner.bat脚本对其进行管理和驱使工作。

1.2、MonkeyRunner 组件

    MonkeyRunner.jar中包含很多个包,每个包下都有若干个类,但主要API都集中在com.android. mo nkeyrunner包的三个模块中。

1.3、Monkey和Monkeyrunner的关系

a、相同点

   1、都是AndroidSDK中自带的测试工具,都是Java工具,名字类似;

   2、都可以通过对APP发送事件,对APP进行测试。

b、不同点

   1、Monkey只是一条命令,不能使用测试脚本,没有断言;而MonkeyRunner是一个API工具包,可以运行Python编写的测试脚本,支持断言。

   2、monkey工具直接运行在设备或模拟器的shell中,生成用户或系统的随机事件流,不带有任何主观性。而monkeyrunner工具则是通过预先编辑的API在Android代码之外发送特定的的事件控制控制设备或模拟器。

   3、Monkey测试一般是在产品功能稳定之后进行,专擅于对APP作长稳测试;而MonkeyRunner可以在开发过程中或产品功能尚未稳定的时候展开,对APP某模块的交互流程和数据做UI测试。

2、monkeyrunner程序实例

2.1、支持测试用例

  MonkeyRunner支持输入输出、流程控制。

  1、安装是一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。

  2、启动入口activity可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。

  3、触摸事件可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。

  4、截图事件可以设置为一个case,如果返回的是True就可以print此用例通过,如果返回的是False就可以print此用例失败。

2.2、方式一:交互式

第一步、Android测试开发环境

     使用的是集成式的ADT,步骤见《Android测试开发.docx》

第二步、启动模拟器或连接真机

     模拟机或真机都可以,只是两者都有些缺陷。

第三步、准备安装包,DOS里启动monkeyrunner交互窗口

2.3、方式二:测试脚本

 第一步、同上

 第二步、安装Python

第三步、同上第二步

第四步、准备安装包,准备Python测试脚本,DOS里通过monkeyrunner执行脚本

test.py脚本

# Imports the monkeyrunner modules used by this programfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage# Connects to the current device, returning a MonkeyDevice objectdevice = MonkeyRunner.waitForConnection()# Installs the Android package. Notice that this method returns a boolean, so you can test to see if the installation worked.device.installPackage('./jinritoutiao_513.apk')# Runs the componentdevice.startActivity(component='com.ss.android.article.news/.activity.SplashActivity')# stop the testing processsMonkeyRunner.sleep(30)# touch the buttondevice.touch(105,48,'DOWN_AND_UP')# stop the testing processsMonkeyRunner.sleep(10)# Presses the Menu buttondevice.press('KEYCODE_HOME','DOWN_AND_UP')# stop the testing processsMonkeyRunner.sleep(10)# Runs the componentdevice.startActivity(component='com.ss.android.article.news/.activity.SplashActivity')# Takes a screenshotresult = device.takeSnapshot()# Writes the screenshot to a fileresult.writeToFile('./jinritoutiao.png','png')# stop the testing processsMonkeyRunner.sleep(10)# Uninstalls the Android package.device.removePackage ('com.ss.android.article.news')#reboot the device device.reboot ()

你可能感兴趣的文章
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>
剑指offer 21.包含min函数的栈
查看>>
剑指offer 23.从上往下打印二叉树
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>