Search code examples
androidpythoninheritancemonkeyrunner

How to inherit from MonkeyDevice?


I would like to extend the MonkeyDevice class of the monkeyrunner API. My derived class looks like this.

from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner

class TestDevice(MonkeyDevice):
    def __init__(self, serial=None):
        MonkeyDevice.__init__(self)
        self = MonkeyRunner.waitForConnection(deviceId=serial) 
        self.serial = serial

When I call test_dev = TestDevice(serial) from another module I get the following error:

    test_dev = TestDevice(serial)
TypeError: _new_impl(): 1st arg can't be coerced to com.android.monkeyrunner.core.IMonkeyDevice

What am I doing wrong?

Thanks in advance!


Solution

  • It appears you cannot directly initialize a MonkeyDevice instance without a call to a factory function waitForConnection. So instead you need to assign self in your __new__() function so that MonkeyDevice recognizes the instance as inheriting from IMonkeyDevice before you call it's __init__

    Example:

    class TestDevice(MonkeyDevice):
        def __new__(self, serial=None):
            return MonkeyRunner.waitForConnection(deviceId=serial) 
        def __init__(self):
            MonkeyDevice.__init__(self)