Monday 10 October 2016

How to Test AutoComplete Text Box with Espresso

This is the most common test scenario now a days because many apps are using autocomplete textbox / autofill textbox. If you have not yet come across this then you can check out your google map application when you start typing inside location fill then the drop-down list of matching results are displayed and you can select any one from the list to continue.This post is about handing test cases where you need to select a result from autocomplete textbox.

Let us see the below example of google map and let us write Espresso Test Case to handle Autocomplete Textbox value selection.

AutoComplete Textbox Test Automation
AutoComplete Textbox Test Automation

Test Scenario -

1. Type the location name.
2. Check that list of matching results are displayed.
3. Click on any one of the matching results.

Test Case -

1. In the below sample test case we are using Junit Testing Framework.
2. We are defning the main activity of the application by using @Rule Annotation.
3. We are using inRoot() and decor View to select a window inside a Main activity window.
4. First we are typing "Ban" in the location field and then we are checking the first two suggestions are displayed or not.
5. Then we are clicking on the Top suggestion.


@RunWith(AndroidJUnit4.class)
@LargeTest
public class MultiWindowTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    private MainActivity mActivity = null;

    @Before
    public void setActivity() {
        mActivity = mActivityRule.getActivity();
    }

    @Test
    public void testAutoCompleteTextView() {
        // Type "ban" to trigger two suggestions.
        onView(withId(R.id.auto_complete_text_view))
                .perform(typeText("ban"), closeSoftKeyboard());

        // Check that both suggestions are displayed.
        onView(withText("Bangalore"))
                .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView()))))
                .check(matches(isDisplayed()));
        onView(withText("bangalore central mall"))
                .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView()))))
                .check(matches(isDisplayed()));
    }

        // Tap on a suggestion.
        onView(withText("Bangalore"))
                .inRoot(withDecorView(not(is(mActivity.getWindow().getDecorView()))))
                .perform(click());

        // By clicking on the auto complete term, the text should be filled in.
        onView(withId(R.id.auto_complete_text_view))
                .check(matches(withText("Bangalore")));
    }

    

}

I hope this post helps you to resolve your autocomplete textbox test issue. If you find it useful then please share with your friends and give me feedback or suggestions in the comment section given below.

2 comments:

  1. Thank you for this post, it was the only code that worked for me.

    ReplyDelete
  2. The only code that works in the World Wide Web. Thank You So Much! I was struggling on this for a couple of hours and finally got the simple and easy solution from you.

    ReplyDelete