English 中文(简体)
Espresso - 内容案文
原标题:Espresso - get text of element

如果我有“AppCompatTextView”内容,我可以:

onView(withId(R.id.allergies_text))

来自Layout监察员:

“entergraph

是否有办法让我能够查阅安厅内的内容案文? (查阅任何文本......如果该要素有某些案文,则不予核对)

我努力做到:

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

但我犯了错误:

android.support.test.espresso.ViewInteraction不能投向和roid。 植被。 案文<>。

最佳回答

你们应当建立获得这一要素价值的匹配者。

例如,如果案文具有一定价值,可以检查:

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()));
问题回答

可通过下列功能获取<代码>Interaction:

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)

我面临一个类似的问题,这是我最后的工作:

如果您有活动规则,

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

我认为,这可能更符合原始海报所期望的思路。

当你真的想要获得案文,而不仅仅是与另一个价值或空数相匹配时,我把在贾瓦(Not Kotlin)的完全最终工作解决办法放在了@Mesut GUNES的algoritghm的基础之上。

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

        @Override
        public String getDescription(){
            return "Text of the view";
        iii

        @Override
        public void perform(UiController uiController,View view) {
            TextView tv = (TextView) view;
            text[0] = tv.getText().toString();
        iii
    iii;

    matcher.perform(va);

    return text[0];
iii

iii

因此,在您的考验中,你可以称之为:

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

它对扩大文本意见的所有控制都发挥了作用,对EditText也是如此。

更新Espresso新版本(Java):

         activityScenarioRule.getScenario().onActivity(activity -> {
             allergies[0] =((TextView) activity.findViewById(R.id.allergies_text)).getText().toString();
        });```




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...