使用monkeyrunner执行脚本录制脚本时为什么界面黑屏?

匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。关注51Testing
用python做自动化测试—Monkeyrunner问题的解决方案
发表于: 11:16 &作者:powerccna & 来源:51Testing软件测试网采编
推荐标签:
  为Andriod SDK 嫡亲的工具,功能还是很强大的,但Andriod的测试技术发展比较晚,所以目前国内的教程和例子比较少,很多功能没有发掘出来。  Monkeyrunner的例程google下可以有很多,这里就不在重复了,下面说说大家可能遇到的几个问题,及解决方案。  1. 点击button,选择输入焦点,只能靠坐标来定位,一旦换,坐标完全失效了。  解决方案a: 通过device.getProperty("display.width"),device.getProperty("display.height")得到你调试手机的像素,在新的手机中,通过这2个参数的比例关系,可以得到在新手机的X,Y坐标点。  解决方案b:通过view 查询ID来定位,这种方法是最好的,肯定不会错。但很多手机上view server不,即使被rooted了(可以通过hierarchyviewer.bat来确认view server是否工作). &破解过程有点下复杂,大家可以参考这里,还是可以破解的。/tag/viewserver/from com.android.chimpchat.hierarchyviewer import HierarchyViewerfrom com.android.monkeyrunner.easy import Byfrom com.android.hierarchyviewerlib.device import ViewNodeeasy_device.locate(By.id('id/main_button'))easy_device.touch(By.id('id/main_button'), 'downAndUp')  2. &测试结果的检查中,用图片来比较精确度不高。  很多的检查结果中,我们希望同时检查app的界面是不是我们期望的,但通知栏的电池或者弹出个通知消息不好比较,容易导致结果比较失败。  解决方案a:通过OCR技术,可以获取到图片里的文字。python里面有/p/pytesser/,当然这个是cpython的,monkeyrunner用的是jython, 无法直接调用cpython的模块,OCR里面的开源模块很多,jython直接调用java非常方便。  解决方案b:还 是通过view server得到界面上内容hierarchy_viewer = device.getHierarchyViewer()view_node = hierarchy_viewer.findViewById('id/prompt_text_view')text = view_node.namedProperties.get('mText').toString()
公益活动:
搜索风云榜
51Testing官方微信
51Testing官方微博
测试知识全知道MonkeyRunner中点击控件的方法_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
MonkeyRunner中点击控件的方法
上传于|0|0|暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
你可能喜欢【转】安卓自动化测试工具MonkeyRunner之录制回放
头一次写技术类博客,纯粹作为工作记录。近期开始学习移动平台安卓的自动化测试工具MonkeyRunner,以下简称MR。先介绍一下录制和回放功能。
1、MR是安卓软件包里自带的一个工具,存放目录在android-sdk-windows-new\tools目录下monkeyrunner.bat,与其说它是一个工具,倒不如说它提供了一系列的接口供调用。
2、准备录制和回放脚本:
将以下代码存储为monkey_recorder.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
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# distributed under the License is distributed on an "AS IS"
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# See the License for the specific language governing permissions
# limitations under the License.
from com.android.monkeyrunner import MonkeyRunner as mr
from com.android.monkeyrunner.recorder import MonkeyRecorder as
device = mr.waitForConnection()
recorder.start(device)
将以下代码存储为play_back.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
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing,
# distributed under the License is distributed on an "AS IS"
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# See the License for the specific language governing permissions
# 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
# 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)
# Process a single file for the specified device.
def process_file(fp, device):
&&& for line in
(cmd, rest) = line.split('|')
&&&&&&&&&&&
# Parse the pydict
&&&&&&&&&&&
rest = eval_r(rest)
&&&&&&&&&&&
print 'unable to parse options'
&&&&&&&&&&&
if cmd not in CMD_MAP:
&&&&&&&&&&&
print 'unknown command: ' + cmd
&&&&&&&&&&&
CMD_MAP[cmd](device, rest)
def main():
&&& file =
sys.argv[1]
open(file, 'r')
&&& device =
MonkeyRunner.waitForConnection()
process_file(fp, device)
fp.close();
if __name__ == '__main__':
&&& main()
3、启动安卓模拟器。
4、打开cmd,转到mr所在目录。输入命令monkeyrunner&
monkey_recorder.py
&5、现在可以开始录制操作了。
6、点击Export Actions按钮,将录制的信息保存成.mr的后缀文件。
7、关闭录制界面。在cmd中使用monkeyrunner play_back.py
tt.mr进行回放刚刚录制的操作。此时,会在模拟器上进行相应的操作。
以上即为一个简单的MR录制和回放过程,此功能的缺点是录制过程太慢,建议使用其他方式代替。
现在,MR支持使用坐标进行编码,也支持使用ID进行编码。敬请关注~@~
参考链接:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 monkeyrunner录制 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信