Long press on a view using AndroidViewClient

和自甴很熟 提交于 2019-12-12 01:38:36

问题


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.



来源:https://stackoverflow.com/questions/24120502/long-press-on-a-view-using-androidviewclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!