Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monkeyrunner problem. java.lang.ClassCastException: when use sameAs() method

Tags:

monkeyrunner

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.

like image 451
Alexander Avatar asked Jan 26 '26 06:01

Alexander


2 Answers

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)
like image 168
cdhabecker Avatar answered Jan 28 '26 18:01

cdhabecker


Had the same problem with SDKTools Revision 12.

Update to SDKTools Revision 15 solved the issue for me.

like image 45
nilfalse Avatar answered Jan 28 '26 20:01

nilfalse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!