2017-07-15 56 views
2

我正在尝试编写一个Espresso测试,它将在自定义SearchView中键入Text。自定义搜索查看是在这里:Android Espresso如何从自定义视图中获取EditText,然后键入文本

https://github.com/MiguelCatalan/MaterialSearchView/tree/develop/library/src/main/res/layout

我在我的MainActivity使用它。如果我直接在这个SearchView上调用'typeText',它会抛出一个错误,因为我实际上需要从这个SearchBar获取EditText。

的EditText上是search_view.xml

我觉得咖啡命令应该是:

onView(get EditText from SearchView).perform(typeText("chicken")); 

我应该如何获得的EditText参考?

回答

1

没关系,我找到了解决办法。

我在单元测试中获得了'搜索'视图,然后我开始循环遍历它的子项。

当我找到EditText,然后我调用了onView。

这是代码。希望它能帮助别人:

search = (SearchBox) mActivityRule.getActivity().findViewById(R.id.searchbox); 

    for(int i = 0; i < search.getChildCount(); i++) 

     if(search.getChildAt(i) instanceof EditText) { 


     onView(withId(search.getChildAt(i).getId())).perform(typeText("soup" + '\n')); 

} 
相关问题