【问题标题】:How can a SearchView in the ActionBar be tested?如何测试 ActionBar 中的 SearchView?
【发布时间】:2012-07-01 12:58:53
【问题描述】:

我在 JavaDocs 中找不到关于如何使用 Android 测试在菜单中的 SearchView 进行测试的任何示例或帮助。有办法吗?

我想做一个.setQuery("Foo", true) 或等效于在我的应用中测试搜索。怎么可能?

【问题讨论】:

    标签: java android testing junit searchview


    【解决方案1】:

    According to this documentation

    注意:虽然折叠您的操作视图是可选的,但我们建议 如果包含 SearchView,您总是折叠您的操作视图。 另请注意,某些设备提供专用的 SEARCH 按钮和 如果用户按下 搜索按钮。只需覆盖您的活动的 onKeyUp() 回调 方法,监听 KEYCODE_SEARCH 事件,然后调用 展开ActionView()。

    如果您已扩展 KEYCODE_SEARCH 事件的搜索视图,则应该可以通过使用仪器注入事件进行测试。

    public void testSearch() { 
       Instrumentation instrumentation = getInstrumentation(); 
       instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_SEARCH); 
       instrumentation.sendCharacterSync(KeyEvent.KEYCODE_F); 
       instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O); 
       instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O);
       //Assert here for whatever you want. 
    }
    

    确保您在清单中具有注入权限。

    【讨论】:

    【解决方案2】:

    我认为你可以使用 monkeyrunner 来做到这一点:http://developer.android.com/tools/help/monkeyrunner_concepts.html

    【讨论】:

    • 你能说得更具体点吗?我在 Android 开发者网站上看到的文档在这个主题上似乎有点含糊。我已经看到其他几个网站提到了一些使用它的工具,但没有确定的。我还没有看到任何清楚地解释如何使用它的东西,而且我对 Python 了解不多。如果需要,我可以了解更多信息,但我没有足够的信息来确定这种时间投资是否会取得成果。
    • 我从来没有,我想你可以在谷歌上找到一些教程
    猜你喜欢
    • 2015-10-10
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-01-18
    • 2014-08-11
    • 2012-03-01
    相关资源
    最近更新 更多