代码之家  ›  专栏  ›  技术社区  ›  reutsey

Espresso-获取元素文本

  •  7
  • reutsey  · 技术社区  · 8 年前

    如果我有一个“AppCompatTextView”元素,可以通过以下方式访问:

    onView(withId(R.id.allergies_text))
    

    从布局检查器:

    enter image description here

    我试着做:

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

    但我得到了一个错误:

    android.support.test.espresso。ViewInteraction无法转换为android.widget.TextView

    4 回复  |  直到 8 年前
        1
  •  7
  •   jeprubio    8 年前

    您应该创建一个匹配器来访问该元素值。

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

    可以这样说:

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

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

    可以这样说:

    onView(withId(R.id.medical_summary_text_view))
        .check(matches(textViewHasValue()));
    
        2
  •  3
  •   Mesut GUNES    6 年前

    ViewInteraction 通过以下功能:

    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)
    
        3
  •  0
  •   Tyler Turnbull    7 年前

    我遇到了一个类似的问题,这就是最终对我起作用的原因:

    如果您有活动规则

    var activityRule = ActivityTestRule(MainActivity::class.java, true, false)
    

    然后你可以这样做:

    activityRule.launchActivity(null)
    val textView: TextView = activityRule.activity.findViewById(R.id.some_text_view)
    val text = textView.text
    

    我认为这可能更符合原海报的要求。

        4
  •  0
  •   Dharman vijay    5 年前

    当你真的想要文本,而不是只将其与另一个值匹配或为空时,我会发布基于@Mesut GUNES算法的Java(而不是Kotlin)的完整最终工作解决方案

    public class TextHelpers {
    public static String getText(ViewInteraction matcher){
        final String[] text = new String[1];
        ViewAction va = new ViewAction() {
    
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(TextView.class);
            }
    
            @Override
            public String getDescription(){
                return "Text of the view";
            }
    
            @Override
            public void perform(UiController uiController,View view) {
                TextView tv = (TextView) view;
                text[0] = tv.getText().toString();
            }
        };
    
        matcher.perform(va);
    
        return text[0];
    }
    

    }

    所以,在你的测试中,你可以这样称呼它:

    TextHelpers.getText(Espresso.onView(withId(R.id.element)));
    

    它适用于扩展TextView的所有控件,在EditText上也是如此。