一.MonkeyRunner基本知识
MonkeyRunner基本模块
- MonkeyRunner:提供了用于连接monkeyrunner至设备或模拟器的方法。它也提供了用于创建用户界面的monkeyrunner程序和用于显示内置帮助的方法。
- MonkeyDevice:这个类提供了安装和卸载程序包,启动一个活动以及发送键盘或触摸事件到应用程序的方法。您也可以使用这个类来运行测试包。
- MonkeyImage:表示一个屏幕捕获图像。这个类提供了捕捉屏幕,将位图图像,以各种不同的格式,比较两个MonkeyImage对象,写一个图像文件的方法。
1.接入设备API
#获取接入的设备,前面的为等待时间(s),后面正则或者设备的全名,返回一个device对象
#device=mr.WaitForConnection(1,'ss1z1')
device=mr.WaitForConnection()
2.安装卸载apk
device.installPackage('myproject/bin/MyApplication.apk')
device.removePackage ('com.example.android.notepad')
3.启动页面
device.startActivity (component='com.android.htccontacts/com.android.htccontacts.ContactsTabActivity')
4.基础操作
#等待时间(单位秒)
MonkeyRunner.sleep(2.0)
#唤醒屏幕
MonkeyRunner.wake()
#触摸屏幕x,y坐标 UP,DOWN,DOWN_AND_UP
device.touch(x,y,"DOWN_AND_UP")#点击通讯录界面+号
#设备屏幕上模拟拖曳
#MonkeyDevice.drag(start,end,duration,steps)
#start:拖曳开始坐标 - The starting point for the drag (a tuple (x,y) in pixels)
#end:拖曳结束坐标点- The end point for the drag (a tuple (x,y) in pixels
#duration:持续时间 - Duration of the drag in seconds (default is 1.0 seconds)
#steps:拖曳步骤- The number of steps to take when interpolating #points. (default is 10)
device.drag((472,1067),(482,560),5.0,2)
#输入一个字符串
device.type("你好")
#重启设备
device.reboot()
#图片相关操作
#获取截图
result1=device.takeSnapshot()
#将结果输出到文件,前面为路径,后面为图片类型,可写可不写
result1.writeToFile('D:/demo1.png','png')
#两种写法都可以
result1.writeToFile('D:\\demo1.png','png')
#将当前图像转换为一个特定的格式并将其作为字符串返回,然后你可以访问的iterable二进制字节。
result.convertToBytes()
#图片的对比,先获取第二张截图
result2=device.takeSnapshot()
result2.writeToFile('D:/demo2.png','png')
#判断图片相识度是否是为90%
if(result1.sameAs(result2,0.9)):
二.MonkeyRunner录制脚本
1.创建monkey_record.py文件,放到sdk-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)
2.命令窗口输入monkeyrunner monkey_record.py调用录制脚本工具
3.录制属性说明
- wait: 用来插入下一次操作的时间间隔,点击后即可设置时间,单位是秒
- Press a Button:用来确定需要点击的按钮,包括menu、home、search,以及对按钮的press、down、up属性
- Type Something:用来输入内容到输入框
- Fling:用来进行拖动操作,可以向上、下、左、右,以及操作的范围
- Export Actions:用来导出脚本
- Refresh Display:用来刷新手机界面,重新连接时才会用到
4.录制完成后可导出后执行脚本
三.实战
题目: monkeyrunner 电话本,新增联系人,要求有用户名,手机号,email。
1.手机打开通讯录,运行cmd窗口输入hierarchyviewer启动服务查看启动action
2.编写脚本
# encoding: utf-8
# -*- coding: UTF-8 -*-
# import sys
# reload(sys)
# sys.setdefaultencoding("utf-8")
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage#导入包
device = MonkeyRunner.waitForConnection() #连接手机设备
MonkeyRunner.sleep(2.0)#添加延时2秒
device.startActivity(component="com.htc.contacts/com.htc.contacts.BrowseLayerCarouselActivity")#启动action
MonkeyRunner.sleep(3.0)#添加延时3秒
device.touch(579,94,"DOWN_AND_UP")#点击通讯录界面+号
print("touch add")#打印点击添加按钮
MonkeyRunner.sleep(2.0)
device.touch(536,812,"DOWN_AND_UP")#htc手机添加通讯录会弹出一个提示是否保存在本地设备上,此为点击提示框确定按钮
MonkeyRunner.sleep(2.0)
device.type('huangyuping')# 输入名字
print("touch name")
MonkeyRunner.sleep(3.0)
device.touch(405,288,"DOWN_AND_UP")#点击手机号码输入框
MonkeyRunner.sleep(2.0)
device.type("xxxx")#输入手机号码xxx
print("touch phone number")
MonkeyRunner.sleep(2.0)
device.touch(132,478,"DOWN_AND_UP")#点击电子邮件输入框
MonkeyRunner.sleep(1.0)
device.type("hyp0313@163.com")#输入电子邮件hyp0313@163.com
print("touch mails")
MonkeyRunner.sleep(1.0)
device.touch(604,679,"DOWN_AND_UP")#点击保存
print("touch ok")
MonkeyRunner.sleep(2.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")#点击物理返回键
3.执行
题目:easyMonkeyDevice 启动message并发送
(获取ID可以使用uiautomatorviewer、hierarchyviewer)
1.连接设备获取启动页面
2.编写代码
# -*- coding: UTF-8 -*-
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
device = MonkeyRunner.waitForConnection()
easyMonkeyDevice = EasyMonkeyDevice(device)
easyMonkeyDevice.startActivity(component="com.android.mms/com.android.mms.ui.MmsTabActivity")
MonkeyRunner.sleep(3.0)
pic1=device .takeSnapshot()#截图
pic1.writeToFile('E:\\pic1.png','png')#保存路径
#easyMonkeyDevice.touch(By.id('id/split_action')
device.touch(320,1200,"DOWN_AND_UP")#点击【写短信】按钮因为没有ID所以写的坐标
MonkeyRunner.sleep(2.0)
device.type("10086")#输入收信人
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/embedded_text_editor'),MonkeyDevice.DOWN_AND_UP)#点击短信文本输入框
MonkeyRunner.sleep(2.0)
device.type("huangyuping")#输入文本内容
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/send_button'),MonkeyDevice.DOWN_AND_UP)#点击发送按钮
MonkeyRunner.sleep(2.0)
pic2 = device.takeSnapshot()#截图
pic2.writeToFile('E:\\pic2.png','png')#保存路径
if pic2.sameAs(pic1,0.9):#对比相似度百分之90
print("two pic is same")
else:
print("this two pic is not the same ")
device.press("KEYCODE_BACK",MonkeyDevice.DOWN_AND_UP)#点击物理返回键
3.执行
题目:APP登录及退出
# encoding: utf-8
# -*- coding: UTF-8 -*-
# import sys
# reload(sys)
# sys.setdefaultencoding("utf-8")
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
device=MonkeyRunner.waitForConnection()#连接设备
installResult = device.installPackage('C:\Users\zws\Downloads\xxx.apk'.decode('utf-8'))
print installResult#打印是否安装成功false true
MonkeyRunner.sleep(4.0)
easyMonkeyDevice = EasyMonkeyDevice(device)
#将device对象包装成EasyMonkeyDevice类对象
easyMonkeyDevice.startActivity(component="com.xxxx/.activity.SplashActivity")#启动界面action
easyMonkeyDevice.touch(By.id('id/menu_container'),MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(7.0)
easyMonkeyDevice.touch(By.id('id/to_login'),MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(2.0)
device.type("xxxxx")#输入账号
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/password'),MonkeyDevice.DOWN_AND_UP)#点击密码输入框
MonkeyRunner.sleep(2.0)
device.type("123456")#输入密码
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/ok'),MonkeyDevice.DOWN_AND_UP)#点击登陆按钮
MonkeyRunner.sleep(9.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")#点击返回按钮关闭引导页
easyMonkeyDevice.touch(By.id('id/home_message_icon'),MonkeyDevice.DOWN_AND_UP)#点击悄悄话tab
MonkeyRunner.sleep(6.0)
easyMonkeyDevice.touch(By.id('id/comment_content_layout'),MonkeyDevice.DOWN_AND_UP)#点击其中一条消息列表进入聊天界面
MonkeyRunner.sleep(8.0)
easyMonkeyDevice.touch(By.id('id/cancel'),MonkeyDevice.DOWN_AND_UP)#点击引导x按钮
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/publish'),MonkeyDevice.DOWN_AND_UP)#点击键盘输入框
device.type("nihao")#输入内容
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/msg_thread_evil'),MonkeyDevice.DOWN_AND_UP)#点击发送按钮
MonkeyRunner.sleep(2.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")#点击返回按钮
MonkeyRunner.sleep(1.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")
MonkeyRunner.sleep(1.0)
easyMonkeyDevice.touch(By.id('id/home_profile_icon'),MonkeyDevice.DOWN_AND_UP)#点击tab我的按钮
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/actionbar_compose'),MonkeyDevice.DOWN_AND_UP)#点击右上角的设置按钮
MonkeyRunner.sleep(2.0)
easyMonkeyDevice.touch(By.id('id/audio_mode_switch'),MonkeyDevice.DOWN_AND_UP)#点击勾选听筒模式
MonkeyRunner.sleep(2.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")#点击物理返回键退出app
MonkeyRunner.sleep(1.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")
MonkeyRunner.sleep(1.0)
device.press("KEYCODE_BACK","DOWN_AND_UP")
MonkeyRunner.sleep(1.0)
device.removePackage("com.xxxx")