How to Retrieve Text from a TextView Using Espresso

How to Retrieve Text from a TextView Using Espresso

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When working with Android UI tests, it is often necessary to validate the content of a TextView. Espresso, a powerful tool for Android UI testing, provides a way to interact with the UI elements. However, retrieving text from a TextView can be tricky. This article will guide you through the process of extracting text from a TextView using Espresso.

Step-by-Step Guide

To get the text from a TextView in Espresso, you can use a custom ViewAction. Below is a method that demonstrates how to achieve this:


    String getText(final Matcher<View> matcher) {
        final String[] stringHolder = { null };
        onView(matcher).perform(new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(TextView.class);
            }

            @Override
            public String getDescription() {
                return "getting text from a TextView";
            }

            @Override
            public void perform(UiController uiController, View view) {
                TextView tv = (TextView) view; // Save, because of check in getConstraints()
                stringHolder[0] = tv.getText().toString();
            }
        });
        return stringHolder[0];
    }
  

This method uses an internal ViewAction to retrieve the text from a TextView. The text is stored in a final array of strings, which allows the anonymous class to modify it.

Important Considerations

  • Use this kind of view data retriever with care. If you frequently find yourself writing such methods, reconsider your approach.
  • Ensure that you only access the view within a ViewAssertion or ViewAction to maintain safe interaction with the UI thread.

Alternative Approach: Using a Custom Matcher

If your goal is to verify the text value rather than retrieve it, you can create a custom Matcher. Below is an example:


    public static Matcher<View> checkConversion(final float value) {
        return new TypeSafeMatcher<View>() {

            @Override
            protected boolean matchesSafely(View item) {
                if (!(item instanceof TextView)) return false;

                float convertedValue = Float.valueOf(((TextView) item).getText().toString());
                float delta = Math.abs(convertedValue - value);

                return delta < 0.005f;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("Value expected is wrong");
            }
        };
    }
  

This matcher verifies that the text value of a TextView matches a given float value within a specified delta range.

Enhancing Your Testing Workflow

Automating UI tests can be challenging, especially when dealing with complex applications. Our product, Repeato, a no-code test automation tool for iOS and Android, can help streamline your testing process. Repeato offers a no-code interface and an intuitive test recorder, making it easy to create, run, and maintain automated tests for your apps.

With Repeato, you can quickly edit and run tests, thanks to its computer vision and AI capabilities. While it already supports testing websites inside an Android emulator or device, explicit web testing support will be released later this summer. This makes Repeato an excellent choice for both beginner and advanced testers looking to automate complex use cases.

For more information on how to get started with Repeato, visit our Getting Started guide.

Conclusion

Retrieving and verifying text from a TextView using Espresso can be done efficiently with the methods described above. Whether you choose to use a custom ViewAction or a custom Matcher, both approaches provide reliable ways to interact with your UI elements. For even more streamlined testing, consider using Repeato to enhance and simplify your automation process.

Like this article? there’s more where that came from!