因此,我最近开始在我现有的一个Android项目中使用Espresso。
一切都进行得很顺利,直到我在我的程序中找到了AutoCompleteTextView。我似乎不明白如何正确地单击自动完成列表中的第一件事情。实际上,我甚至不确定在这种情况下使用哪种方法,onView()还是onData()。
发布于 2016-07-27 09:29:44
所以我终于想出了答案,多亏了前面的问题:使用espresso工具测试自动完成文本视图
我只是把我的版本寄给将来可能使用它的人。
onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());发布于 2019-01-22 11:45:28
由于一些我不知道的原因,AStupidNoob的解决方案行不通。所以我又找到了一个:
onView(withText("Spinner Item"))
.inRoot(RootMatchers.isPlatformPopup())
.perform(click());AutoCompleteTextView本身
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<AutoCompleteTextView
android:id="@+id/product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:hint="@string/product"
android:singleLine="true"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>发布于 2017-07-28 08:18:07
我想我找到了一个比公认的答案更干净的方法!
onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());
细目如下:
onData(x) --这将在下拉列表中找到呈现与x匹配的数据对象的视图。数据是由Adaptor提供给AutoCompleteTextView的,所以它可以是Adaptor提供的任何类型的对象,可能不是视图。您需要使用标准的hamcrest核心匹配器(equalTo、instanceOf等)。而不是(withText,withId等)。尝试找出它是什么对象以及如何匹配它可能会很痛苦,但是没有一种更整洁的方法:在适配器中有很多项,一些视图甚至还没有在层次结构中,所以onView无法工作!onData将确保加载与数据匹配的视图。签出这里 ( onData返回的内容)和这里 (加载匹配的数据)inRoot(RootMatchers.isPlatformPopup())显示下拉菜单位于另一个窗口上,而不是您的活动运行的默认窗口。所以我们必须指定我们要搜索那个窗口。接受的答案使用RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView()))),它似乎与任何不是默认窗口的窗口相匹配。不管怎么说其他人。
https://stackoverflow.com/questions/38562341
复制相似问题