0

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))
HaMi
  • 539
  • 6
  • 23

2 Answers2

2

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)
HaMi
  • 539
  • 6
  • 23
1

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.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Thanks dtmilano. I think that this method is not applicable for buttons and other views. How can I simulate long-pressing on buttons using this? – HaMi Jun 12 '14 at 05:48