触摸,滑动或拖动在Android上模拟的事件无法正常工作

时间:2014-03-13 14:07:01

标签: android adb monkeyrunner androidviewclient

阅读帖子后,我想出了如何模拟触摸事件:

  

adb shell输入tap 100 100

我已经安装了MagicMarker以查看是否有任何内容被绘制,没有任何内容。

我还尝试过使用monkeyrunner / androidViewClient Touch功能:

  

device.touch(100,100,' DOWN_AND_UP');

我的整个AndroidViewClient代码:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import getopt, sys
import os

# Add android to path becayuse it seems to not appear on windows

sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/tools/lib")
sys.path.append("D:/Dev/adt-bundle-windows-x86_64-20131030/sdk/platform-tools")

# PyDev sets PYTHONPATH, use it
try:
    for p in os.environ['PYTHONPATH'].split(':'):
        if not p in sys.path:
            sys.path.append(p)
except:
    pass

try:
    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient, ViewNotFoundException
device, serialno = ViewClient.connectToDeviceOrExit()   
vc = ViewClient(device, serialno)
device.touch(100,100,"DOWN_AND_UP")

我成功使用了device.press(" KEYCODE_MENU"," DOWN_AND_UP")或者device.takeSnapshot(),我根本不明白为什么没有收到触摸事件通过我的手机。

顺便说一句,我使用的是真正的设备(4.3中的GS3和GS4)

不要犹豫,询问进一步的信息。

1 个答案:

答案 0 :(得分:4)

我使用这个简单的 AndroidViewClient 脚本测试了 MagicMarker 。 请注意,版本5.1.1中引入了adbclient.drag()的一些修复程序,因此请确保您拥有最新版本。

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2014  Diego Torres Milano
'''

__requires__ = ["androidviewclient >= 5.1.1"]
import pkg_resources
from com.dtmilano.android.adb.adbclient import AdbClient

AdbClient(serialno='.*').drag((100, 100), (400, 400), 1000)

这会产生:

enter image description here

另外请注意,在device脚本AdbClient中,drag()实例。

如果您查看AdbClientinput swipe的实施情况,您会看到根据相应的API级别使用带有参数的def drag(self, (x0, y0), (x1, y1), duration, steps=1): ''' Sends drag event (actually it's using C{input swipe} command. @param (x0, y0): starting point @param (x1, y1): ending point @param duration: duration of the event in ms @param steps: number of steps (currently ignored by @{input swipe} ''' version = int(self.getProperty('ro.build.version.sdk')) if version <= 15: raise RuntimeError('drag: API <= 15 not supported (version=%d)' % version) elif version <= 17: self.shell('input swipe %d %d %d %d' % (x0, y0, x1, y1)) else: self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration))

{{1}}