Espresso - 获取元素文本

问题描述 投票:0回答:5

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

onView(withId(R.id.allergies_text))

来自布局检查器:

有没有办法可以访问 Android Studio 中元素的文本? (访问其中的任何文本...不检查元素中是否存在某些文本)

我尝试这样做:

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

android android-espresso android-espresso-recorder
5个回答
7
投票

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

例如,您可以检查它的文本是否具有某些值:

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()));

5
投票

您可以通过以下函数获取

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)

0
投票

我遇到了类似的问题,这就是最终对我有用的问题:

如果您有活动规则

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

我认为这可能更符合原始海报所寻找的内容。


0
投票

当您确实想要文本而不只是将其与另一个值或空匹配时,我会根据 @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。


0
投票

针对较新版本的 Espresso (Java) 进行了更新:

         activityScenarioRule.getScenario().onActivity(activity -> {
             allergies[0] =((TextView) activity.findViewById(R.id.allergies_text)).getText().toString();
        });```
© www.soinside.com 2019 - 2024. All rights reserved.