安卓测试.如何使用 Espresso 更改 TextView 的文本 [英] Android testing. How to change text of a TextView using Espresso

查看:26
本文介绍了安卓测试.如何使用 Espresso 更改 TextView 的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Espresso 更新 EditText 很容易,但我找不到更改文本的方法(例如使用 TextView.setText("someText"); 方法)在测试过程中.

It is easy to update an EditText with Espresso, but I can not find a way to change a text (like with a TextView.setText("someText"); method) during the testing process.

ViewAction.replaceText(stringToBeSet);

不起作用,因为它应该是一个 EditText

Is not working, cos it should be an EditText

推荐答案

您可以考虑实现自己的 ViewAction.

You can look into implementing your own ViewAction.

这是来自 espresso 库的 replaceText viewaction 的修改版本,旨在用于 TextView.

Here is the modified version of the replaceText viewaction from espresso library that is meant to work on the TextView.

 public static ViewAction setTextInTextView(final String value){
            return new ViewAction() {
                @SuppressWarnings("unchecked")
                @Override
                public Matcher<View> getConstraints() {
                    return allOf(isDisplayed(), isAssignableFrom(TextView.class));
                }

                @Override
                public void perform(UiController uiController, View view) {
                    ((TextView) view).setText(value);
                }

                @Override
                public String getDescription() {
                    return "replace text";
                }
            };
    }

这篇关于安卓测试.如何使用 Espresso 更改 TextView 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆