Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - get text of element

If I have an "AppCompatTextView" element that I can access by:

onView(withId(R.id.allergies_text))

From Layout Inspector:

enter image description here

Is there a way I can access the text of the element in Android Studio? (to access whatever text is there... not check if some text exists in the element)

I tried to do:

val tv = onView(withId(R.id.medical_summary_text_view)) as TextView
val text = text.text.toString()
print(text)

But I get the error:

android.support.test.espresso.ViewInteraction cannot be cast to android.widget.TextView

like image 958
reutsey Avatar asked Aug 09 '17 17:08

reutsey


People also ask

Which is the class used in espresso framework to identify mobile test objects?

Espresso testing library extends the necessary JUnit classes to support the Android based instrumentation testing.


2 Answers

You should create a matcher to access to that element value.

For instance, you can check if it's text has some value:

Matcher<View> hasValueEqualTo(final String content) {

    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("Has EditText/TextView the value:  " + content);
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof TextView) && !(view instanceof EditText)) {
                    return false;
            }
            if (view != null) {
                String text;
                if (view instanceof TextView) {
                    text = ((TextView) view).getText().toString();
                } else {
                    text = ((EditText) view).getText().toString();
                }

                return (text.equalsIgnoreCase(content));
            }
            return false;
        }
    };
}

And call it this way:

onView(withId(R.id.medical_summary_text_view))
    .check(matches(hasValueEqualTo(value)));

or you can edit this matcher to return just whether the text is empty or not:

Matcher<View> textViewHasValue() {

    return new TypeSafeMatcher<View>() {

        @Override
        public void describeTo(Description description) {
            description.appendText("The TextView/EditText has value");
        }

        @Override
        public boolean matchesSafely(View view) {
            if (!(view instanceof TextView) && !(view instanceof EditText)) {
                    return false;
            }
            if (view != null) {
                String text;
                if (view instanceof TextView) {
                    text = ((TextView) view).getText().toString();
                } else {
                    text = ((EditText) view).getText().toString();
                }

                return (!TextUtils.isEmpty(text));
            }
            return false;
        }
    };
}

And call it this way:

onView(withId(R.id.medical_summary_text_view))
    .check(matches(textViewHasValue()));
like image 129
jeprubio Avatar answered Nov 05 '22 16:11

jeprubio


You can get the text of the ViewInteraction by the following function:

fun getText(matcher: ViewInteraction): String {
    var text = String()
    matcher.perform(object : ViewAction {
        override fun getConstraints(): Matcher<View> {
            return isAssignableFrom(TextView::class.java)
        }

        override fun getDescription(): String {
            return "Text of the view"
        }

        override fun perform(uiController: UiController, view: View) {
            val tv = view as TextView
            text = tv.text.toString()
        }
    })

    return text
}
val numberResult: ViewInteraction = onView(withId(R.id.txNumberResult))
var searchText = getText(numberResult)
like image 35
Mesut GUNES Avatar answered Nov 05 '22 15:11

Mesut GUNES