Skip to content Skip to sidebar Skip to footer

Long Press On A View Using Androidviewclient

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 ty

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

Solution 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.

Post a Comment for "Long Press On A View Using Androidviewclient"