跑猴问题。 java.lang.ClassCastException:使用 SameAs() 方法时

发布于 2024-11-09 18:26:37 字数 911 浏览 0 评论 0原文

我从源代码构建 SDK。 我想使用以下脚本:

img=MonkeyRunner.loadImageFromFile(path='/home/alsu/monkeyrunner/device.png')
img_1=device.takeSnapshot()

img_1.sameAs(img, 1)

但出现此错误:

File "/home/semc/monkey/out/host/linux-x86/sdk/android-sdk_eng.semc_linux-x86/tools/test.py", line 23, in <module>
    if img_1.sameAs(img,1):
    at com.android.monkeyrunner.MonkeyImage.sameAs(MonkeyImage.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

java.lang.ClassCastException: java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to com.android.monkeyrunner.core.IMonkeyImage

请帮忙。

I build SDK from source code.
And I want to use the following script:

img=MonkeyRunner.loadImageFromFile(path='/home/alsu/monkeyrunner/device.png')
img_1=device.takeSnapshot()

img_1.sameAs(img, 1)

But this error occurs:

File "/home/semc/monkey/out/host/linux-x86/sdk/android-sdk_eng.semc_linux-x86/tools/test.py", line 23, in <module>
    if img_1.sameAs(img,1):
    at com.android.monkeyrunner.MonkeyImage.sameAs(MonkeyImage.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

java.lang.ClassCastException: java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to com.android.monkeyrunner.core.IMonkeyImage

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

黯然#的苍凉 2024-11-16 18:26:37

MonkeyImage.sameAs() 被破坏了——无论你传递给它什么,你都会得到这个错误。

解决方法:使用convertToBytes():

new_snap = device.takeSnapshot()
old_snap = MonkeyRunner.loadImageFromFile(control_dir + '/' + test_name + '.png')
#if new_snap.sameAs(old_snap) == True:
new_bytes = new_snap.convertToBytes('png')
old_bytes = old_snap.convertToBytes('png')
if new_bytes == old_bytes:
    print 'Test ' + test_name + ' PASSED'
else:
    print 'Test ' + test_name + ' FAILED'

更新:2011年10月27日:比较快照的一部分

根据kaciula的评论,这是从快照中删除状态栏的代码:

device = MonkeyRunner.waitForConnection(emulator)
width = int(device.getProperty('display.width'))
height = int(device.getProperty('display.height'))
density = device.getProperty('display.density')
if density == .75:
    density_dir = 'ldpi'
    snap_rect = 0, 19, width, height - 19
elif density == 1.5:
    density_dir = 'hdpi'
    snap_rect = 0, 38, width, height - 38
elif density == 2.0:
    density_dir = 'xhdpi'
    snap_rect = 0, 50, width, height - 50
else:
    density_dir = 'mdpi'
    snap_rect = 0, 25, width, height - 25
new_snap = device.takeSnapshot()
new_snap = new_snap.getSubImage(snap_rect)

MonkeyImage.sameAs() is just broken -- it doesn't matter what you pass to it, you'll get that error.

Workaround: use convertToBytes():

new_snap = device.takeSnapshot()
old_snap = MonkeyRunner.loadImageFromFile(control_dir + '/' + test_name + '.png')
#if new_snap.sameAs(old_snap) == True:
new_bytes = new_snap.convertToBytes('png')
old_bytes = old_snap.convertToBytes('png')
if new_bytes == old_bytes:
    print 'Test ' + test_name + ' PASSED'
else:
    print 'Test ' + test_name + ' FAILED'

Update: Oct 27, 2011: comparing portion of snapshot

As per kaciula's comment, this is the code to remove the status bar from the snapshot:

device = MonkeyRunner.waitForConnection(emulator)
width = int(device.getProperty('display.width'))
height = int(device.getProperty('display.height'))
density = device.getProperty('display.density')
if density == .75:
    density_dir = 'ldpi'
    snap_rect = 0, 19, width, height - 19
elif density == 1.5:
    density_dir = 'hdpi'
    snap_rect = 0, 38, width, height - 38
elif density == 2.0:
    density_dir = 'xhdpi'
    snap_rect = 0, 50, width, height - 50
else:
    density_dir = 'mdpi'
    snap_rect = 0, 25, width, height - 25
new_snap = device.takeSnapshot()
new_snap = new_snap.getSubImage(snap_rect)
被你宠の有点坏 2024-11-16 18:26:37

SDKTools Revision 12 也有同样的问题。

更新到 SDKTools Revision 15 解决了我的问题。

Had the same problem with SDKTools Revision 12.

Update to SDKTools Revision 15 solved the issue for me.

请别遗忘我 2024-11-16 18:26:37

抱歉这个问题。此更改应该可以解决问题:

https://review.source.android.com/# /c/25618/

Sorry for the issue. This change should fix the problem:

https://review.source.android.com/#/c/25618/

寄居人 2024-11-16 18:26:37

我在其他地方也遇到了类似的问题。尝试使用 img_1.sameAs(img, 1.0) 代替 img_1.sameAs(img, 1)。 SameAs() 函数采用浮点值。这应该不重要,因为它是 python...但它是 Jython - python 和 java 中最糟糕的一切都集中在一个地方;-)

I had similar problem elsewhere. Instead of img_1.sameAs(img, 1) try img_1.sameAs(img, 1.0). The sameAs() function takes float value. It shouldn't matter since it is python... but it is Jython - everything the worst of python and java in one place ;-)

悍妇囚夫 2024-11-16 18:26:37

sameAs 工作得很好,直到您将图像文件移动到其他文件夹并再次加载为止。奇怪的错误...

稍后编辑:
我想我已经弄清楚了。问题似乎是当文件名无效时 MonkeyRunner.loadImageFromFile() 不会给出错误。

例如,像 MonkeyRunner.loadImageFromFile("d:\p.png") 这样的东西就可以正常工作,但是像 MonkeyRunner.loadImageFromFile("d:\t.png")< /code> 将不起作用。原因很简单:\t 是一个特殊的序列。

解决方案是使用 \\\/ 作为文件夹分隔符。

sameAs works just fine until you move the image file to a different folder and load it again. Strange bug...

Later edit:
I think I got to the bottom of this. The problem seems to be that MonkeyRunner.loadImageFromFile() is not giving an error when the file name is not a valid one.

For example, something like MonkeyRunner.loadImageFromFile("d:\p.png") will work just fine, but something like MonkeyRunner.loadImageFromFile("d:\t.png") will not work. The reason is quite simple: \t is a special sequence.

The solution is to use \\\ or / as folder separators.

栖迟 2024-11-16 18:26:37

当我使用sameAs()时,我也遇到了类似的问题。我的问题是由三个原因引起的:

  1. 两个图像的分辨率不同,因此 sameAs 函数在运行时中断。

  2. 图片路径错误。即使我使用了错误的路径,loadImageFromFile()也从未向我们提及过。所以我们应该判断这个路径是否正确。

  3. SDK 版本较差。就我而言,我使用了 SDK_r23。

I was also confronted with a similar problem when I used sameAs(). My problem is caused by three reasons:

  1. Two images are of different resolutions, so the sameAs function breaks at runtime.

  2. The images path is wrong. Even though I used the wrong path, loadImageFromFile() never mentions it to us. So we should determine whether this path is correct or not.

  3. Poor SDK version. In my case, I used SDK_r23.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文