Espresso test to check the max length in an editText

Espresso is a testing framework in android, which is used to test the UI elements in android like editText, textView, button, recyclerView and so on. One thing to note here is that Espresso synchronises all UI events. In case if we want to check network operations, we have to use IdlingResource, which I will explain in my upcoming tutorials.

Here I am verifying the mobile number (editText) can enter max 13 characters. I created a custom matcher, checkMaxLength which will check the max length of editText is 13 characters. We can pass any integer value as we wish.

import android.os.Build;
import android.os.SystemClock;
import android.text.InputFilter;
import android.view.View;
import android.widget.TextView;

import com.smartandroidians.myloci.activities.LoginActivity;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static;
import static;
import static;
import static;
import static;

 * Created by Androidians on 18/10/17.

public class LoginActivityTest {

    public ActivityTestRule activityTestRule = new ActivityTestRule(LoginActivity.class);

    public void verifyMobileNumber() {

            .perform(typeText("+919912345678"), closeSoftKeyboard())

    public static TypeSafeMatcher checkMaxLength(final int lines) {
        return new TypeSafeMatcher() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            protected boolean matchesSafely(View item) {
                InputFilter[] filters = ((TextView) item).getFilters();
                InputFilter.LengthFilter lengthFilter =(InputFilter.LengthFilter) filters[0];

                return lengthFilter.getMax() == lines;

            public void describeTo(Description description) {


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android