Espresso AutoCompleteTextView请点击

use*_*720 3 android autocompletetextview android-testing android-espresso

因此,我最近开始在我现有的Android项目之一中搞混Espresso。

一切都进行得很顺利,直到我开始AutoCompleteTextView在程序中找到它。我似乎不明白如何正确单击自动完成列表中的第一件事。实际上,我甚至不能确定使用哪个,onView()或者onData()在这种情况下。

Akb*_*SSS 20

由于某些我不知道的原因,AStupidNoob 的解决方案不起作用。于是我又找了一个:

onView(withText("Spinner Item"))
            .inRoot(RootMatchers.isPlatformPopup())
            .perform(click());
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


ASt*_*oob 6

我认为我找到了比接受的答案更干净的方法!

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

细目:

  • onData(x)这将x在下拉列表中找到呈现数据对象匹配的视图。数据是由Adaptor给定的提供给的AutoCompleteTextView,因此它可以是提供的任何类型的对象Adaptor,它可能不是View。你要使用标准hamcrest核心的匹配这个(equalToinstanceOf,等...),而不是(withTextwithId,等...)。尝试查找这是什么对象以及如何将其匹配可能很痛苦,但是没有一种更整洁的方法:适配器中有很多项目,有些视图甚至还没有出现在层次结构中,所以onView不能工作!onData将确保加载与您的数据匹配的视图。在此处onData返回的内容)和此处(加载匹配的数据)签出
  • inRoot(RootMatchers.isPlatformPopup())因此,事实证明,下拉菜单位于活动运行所在的默认窗口之外的另一个窗口上。因此,我们必须指定我们要搜索该窗口。可接受的答案使用RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))似乎与非默认窗口匹配的任何窗口。

无论如何,还有其他人。


use*_*720 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());
Run Code Online (Sandbox Code Playgroud)