Long press on a view using AndroidViewClient

2019-08-29 10:32发布

问题:

How can I simulate long press on a view (for example a button) using AndroidViewClient? The touch method of ViewClient always performs a simple press on its input (even if I set type argument to adbClient.DOWN)

Edit: The touch method in adbclient.py has a type argument, but it is not used in method body. In MonkeyRunner, when the type is DOWN, it performs longpress.

def touch(self, x, y, eventType=DOWN_AND_UP):
    self.shell('input tap %d %d' % (x, y))

回答1:

I find the answer for my question right now. We can use the drag method to simulate long pressing on views. The sample code is as follow:

buttonText = 'ClMe'
button = vc.findViewWithText(buttonText )
(x,y) = button.getXY()
button.device.drag((x,y), (x,y), 2000, 1)


回答2:

From adbclient.py:

    version = self.getSdkVersion()
    if version >= 19:
        cmd = 'input keyevent --longpress %s' % name
        if DEBUG:
            print >> sys.stderr, "longPress(%s)" % cmd
        self.shell(cmd)
    else:
        raise RuntimeError("longpress: not supported for API < 19 (version=%d)" % version)

Longpress is supported for API >= 19.