代码之家  ›  专栏  ›  技术社区  ›  BountyHunter

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

  •  1
  • BountyHunter  · 技术社区  · 8 年前

    我正在尝试为android应用程序编写浓缩咖啡测试,需要知道如何自动化使用 自动补全文本框 . 我的应用程序使用的代码如下所述:

    activity\u 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>
    

    主要活动。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 回复  |  直到 8 年前
        1
  •  3
  •   trin94    8 年前

    我的情况与此类似:我与一个 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());
    

    当做

        2
  •  1
  •   BountyHunter    8 年前

    经过数小时的斗争,终于找到了解决方案:

    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());