Long press on a view using AndroidViewClient

2019-08-29 10:33发布

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))

2条回答
叛逆
2楼-- · 2019-08-29 11:08

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)
查看更多
淡お忘
3楼-- · 2019-08-29 11:20

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.

查看更多
登录 后发表回答