- 检查=MonkeyRunner.loadImageFromFile(chk)
- 检查=MonkeyRunner.loadFromFile(chk)
- 检查=MonkeyImage.loadFromFile(chk)
以上所有错误
回溯(最后一次调用):文件“stdin”,第 1 行,在 AttributeError 中:类型对象 'com.android.monkeyrunner.MonkeyRunner' 没有属性 'loadFromFile'
以上所有错误
回溯(最后一次调用):文件“stdin”,第 1 行,在 AttributeError 中:类型对象 'com.android.monkeyrunner.MonkeyRunner' 没有属性 'loadFromFile'
要查看 monkeyrunner 中的内容,请运行以下脚本:
#! /opt/android-sdk/tools/monkeyrunner
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
for m in [MonkeyRunner, MonkeyDevice, MonkeyImage]:
print "%s:\n %s\n" % (m.__name__, dir(m))
您将看到定义的内容和位置。例如SDK中的monkeyrunner返回
MonkeyRunner:
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'alert', 'choice', 'help', 'input', 'sleep', 'waitForConnection']
MonkeyDevice:
['DOWN', 'DOWN_AND_UP', 'UP', '__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'broadcastIntent', 'drag', 'getProperty', 'getSystemProperty', 'installPackage', 'instrument', 'press', 'reboot', 'removePackage', 'shell', 'startActivity', 'takeSnapshot', 'touch', 'type', 'wake']
MonkeyImage:
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'convertToBytes', 'getRawPixel', 'getRawPixelInt', 'getSubImage', 'sameAs', 'writeToFile']
如果不符合您的预期,请从源代码构建。
在 Android SDK 中使用最新的 Monkeyrunner(目前是r13)
MonkeyRunner模块中的使用方法:
MonkeyRunner.loadImageFromFile(imgFullPath)
我宁愿使用 python 的 PIL 库来完成这项工作。
您必须单独安装 PIL。
将此操作分为两部分。首先取当前图像
使用 PIL 库编写一个 python 脚本,如下所示。
from PIL import Image
from PIL import ImageChops
def equal(im1, im2):
return ImageChops.difference(im1, im2).getbbox() is None
im1 = Image.open("current.png")
im2 = Image.open("reference.jpg")
equal(im1, im2)
注意:必须安装 python 和 PIL 库才能正常工作。
此功能检查两个图像之间的差异。
current.png 是从设备捕获的,reference.png 是参考图像。
将此代码编写在单独的文件中并从 monkeyrunner 脚本中调用。
PS如果您无法破解 monkeyrunner 代码或 monkeyrunner 在以后的版本中不提供此功能,请使用它。