1

所以我最近开始在我现有的一个Android项目中使用Espresso。Espresso AutoCompleteTextView点击

一切都很体面,直到我在我的程序中找到AutoCompleteTextView。我似乎无法理解如何正确点击自动完成列表中的第一件事。实际上,我甚至不确定在这种情况下使用哪一个,onView()onData()

回答

1

所以,我终于找到它了,感谢这个前面的问题: Testing autocomplete textview using espresso tool

生病只是张贴我的版本的它谁可能会使用它的人未来。

onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click()); 
-2

您可以验证这个库的例子: Library

+0

我没有看到这与Espresso有什么关系 – user3050720

3

我想我找到了一个比接受的答案更清洁的方法!

onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());

细目:

  • onData(x)这将找到匹配呈现在下拉x向下所述数据对象的视图。数据由Adaptor提供给AutoCompleteTextView,因此它可以是Adaptor提供的任何类型的对象,它可能不会是View。您需要使用标准Hamcrest核心匹配器(equalTo,instanceOf等),而不是(withText,withId等)。尝试找出这是什么对象以及如何匹配它可能是一件痛苦的事情,但没有一个更好的方法:在适配器中有很多项目,有些视图甚至不会在层次结构中,所以onView不能工作! onData将确保加载与您的数据相匹配的视图。结帐here(这是什么onData回报)和here(此加载匹配数据)
  • inRoot(RootMatchers.isPlatformPopup())因此,原来的下拉菜单是在另一个窗口比你的活动运行在默认的窗口。因此,我们必须指定我们要搜索该窗口。接受的答案使用RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))),它似乎匹配任何不是默认窗口的窗口。

反正HTH别人。

相关问题