Reputation: 1411
I am trying to write espresso tests for an android app and need to know how to automate the section that uses AutoCompleteTextView. The code my application uses is mentioned below:
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);
}
}
I have tried a few solutions with no luck. I was able to fetch the count using the following code
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));
}
}
However, I want to be able to click a particular option as well as get text for option at various positions
Tried Following solutions as well, but did not help:
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());
Upvotes: 2
Views: 833
Reputation: 212
My situation was similar: I had a custom dialog with a single 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>
My solution (found here):
onView(withText(containsString("Mister Dummy")))
.inRoot(isPlatformPopup())
.perform(click());
Regards
Upvotes: 6
Reputation: 1411
Finally found a solution after hours of struggle:
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());
Upvotes: 1