使用monkeyrunner在android.support.v7.widget.SearchView中键入文本

时间:2015-07-30 16:32:27

标签: android searchview monkeyrunner

我正在尝试编写一个monkeyrunner脚本,该脚本在工具栏中的SearchView中进行搜索。我有一个更小,更大的问题:

我找不到开始搜索的方法(点击搜索图标)。我想用某种id做这件事,类似于:

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By

device = MonkeyRunner.waitForConnection()
easy_device = EasyMonkeyDevice(device)

easy_device.touch(By.id('action_search'), MonkeyDevice.DOWN_AND_UP)

其中action_search是我的菜单xml:

中搜索“小部件”的ID
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".Cities" >
    <item android:id="@+id/action_search"
          android:title="@string/search_label"
          android:icon="@android:drawable/ic_menu_search"
          app:showAsAction="ifRoom"
          app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

但它似乎不起作用。我发现此视图中不存在action_search ID:

easy_device.touch(By.id('action_search'), MonkeyDevice.DOWN_AND_UP)
ValueError: View not found: <com.android.monkeyrunner.easy.By object at 0x2>

什么是正确的ID或正确的方法?

所以现在我用坐标点击它,我设法打开搜索文本字段,甚至键盘弹出,但是这是我的更大问题:我无法在搜索框中键入文本

# it doesn't work by id:
#easy_device.touch(By.id('action_search'), MonkeyDevice.DOWN_AND_UP)
# workaround: click on it by coordinates:
device.touch(int(deviceW*0.87), int(deviceH*0.07), 'DOWN_AND_UP')
MonkeyRunner.sleep(1)
# now the search box is active and the keyboard has popped up

# click on the middle of the search box doesn't help:
device.touch(int(deviceW*0.5), int(deviceH*0.07), 'DOWN_AND_UP')
MonkeyRunner.sleep(1)

# type doesn't work:
device.type('Bud')
# at this point search box lost the focus

# nor does pressing the keys work:
device.press('KEYCODE_B', MonkeyDevice.DOWN_AND_UP)
MonkeyRunner.sleep(1)
device.press('KEYCODE_u', MonkeyDevice.DOWN_AND_UP)

0 个答案:

没有答案