how to get text from textview using espresso

前端 未结 2 1501
执笔经年
执笔经年 2020-12-31 01:26

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

2条回答
  •  天命终不由人
    2020-12-31 01:48

    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 checkConversion(final float value){
        return new TypeSafeMatcher() {
    
            @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");
            }
        };
    }
    

提交回复
热议问题