Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get text from textview using espresso

I want get text string shown in a textview in LinearLayout. can espresso do that? If not, is there other way to do that or can I use android api in espresso test case? I am using API 17 18 or newer, espresso 1.1(It should be the latest one.). I have no clue about this. Thanks.

like image 514
Nick Dong Avatar asked Apr 30 '14 06:04

Nick Dong


People also ask

How do you get text of an element in espresso?

TextHelpers. getText(Espresso. onView(withId(R. id.

How do I get TextView text?

String a = tv. getText(). toString(); int A = Integer. parseInt(a);

How do I assign text to TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method. The text is set via the android:text attribute. You can either set the text as attribute value directly, or reference a text defined in the strings.


2 Answers

The basic idea is to use a method with an internal ViewAction that retrieves the text in its perform method. Anonymous classes can only access final fields, so we cannot just let it set a local variable of getText(), but instead an array of String is used to get the string out of the ViewAction.

    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];     } 

Note: This kind of view data retrievers should be used with care. If you are constantly finding yourself writing this kind of methods, there is a good chance, you're doing something wrong from the get go. Also don't ever access the View outside of a ViewAssertion or ViewAction, because only there it is made sure, that interaction is safe, as it is run from UI thread, and before execution it is checked, that no other interactions meddle.

like image 110
haffax Avatar answered Sep 23 '22 03:09

haffax


If you want to check text value with another text, you can create Matcher. You can see my code to create your own method:

 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");         }     }; } 
like image 22
emaleavil Avatar answered Sep 24 '22 03:09

emaleavil