使用espresso处理由AutoCompleteTextView填充的选项

时间:2018-01-11 07:57:15

标签: android-espresso

我正在尝试为Android应用编写espresso测试,并且需要知道如何自动化使用 AutoCompleteTextView 的部分。我的应用程序使用的代码如下所示:

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="15dp"
    android:text="Name a fruit from (Apple Banana Cherry Date Grape Kiwi Mango Pear)" />

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView"
    android:layout_marginLeft="36dp"
    android:layout_marginTop="17dp"
    android:ems="10"
    android:text="">

    <requestFocus />
</AutoCompleteTextView>

MainActivity.java

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
String[] fruits = {"Apple", "Apples2", "Apples3", "Date", "Grape", "Kiwi", "Mango", "Pear"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Creating the instance of ArrayAdapter containing list of fruit names
    ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this, android.R.layout.select_dialog_item, fruits);
    //Getting the instance of AutoCompleteTextView
    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
    actv.setThreshold(1);//will start working from first character
    actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
    actv.setTextColor(Color.RED);

}
}

我尝试了一些没有运气的解决方案。我能够使用以下代码获取计数

public class AutoCompleteTextViewItemCountAssertion implements ViewAssertion {
private final int expectedCount;

public AutoCompleteTextViewItemCountAssertion(int expectedCount) {
    this.expectedCount = expectedCount;
}

@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
    if (noViewFoundException != null) {
        throw noViewFoundException;
    }
    AutoCompleteTextView tv = (AutoCompleteTextView) view;
    assertThat(tv.getAdapter().getCount(),is(expectedCount));
}
}

但是,我希望能够点击特定选项以及在不同位置获取选项文本

尝试了以下解决方案,但没有帮助:

onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());
onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());
onData(anything()).atPosition(1).perform(click());

2 个答案:

答案 0 :(得分:1)

经过数小时的努力后终于找到了解决方案:

MainActivity mActivity = null;
//activity defined under @Rule annotation
mActivity = mActivityRule.getActivity();

//Modify <Text to Click> with any other text displayed under AutoCompleteTextView
onView(withText("Text to Click")).inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView())))).perform(click());

答案 1 :(得分:0)

我的情况类似:我有一个带有AutoCompleteTextView的自定义对话框:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/custom_dialog_layout_design_user_input"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:padding="16dp">

    <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/userInputDialogAutoComplete"
                      android:inputType="textMultiLine|textVisiblePassword"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"/>
</LinearLayout>

我的解决方案(found here):

   onView(withText(containsString("Mister Dummy")))
               .inRoot(isPlatformPopup())
               .perform(click());

此致