Android 测试。如何使用 Espresso 更改 TextView 的文本

Android testing. How to change text of a TextView using Espresso

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

ViewAction.replaceText(stringToBeSet);

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

您可以研究实现自己的 ViewAction。

这是来自 espresso 库的 replaceText viewaction 的修改版本,旨在用于 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";
                }
            };
    }

您只需在您声明的布局中添加您的布局 TextView

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
        <TextView
            android:textColor="#123456"
            android:textSize="20dp"
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

在测试中编写代码

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mMain = new ActivityTestRule<> (MainActivity.class);

   /*set text view in textView */

            public static ViewAction setTextInTextView(final String value){
                return new ViewAction() {
                    @SuppressWarnings("unchecked")
                    @Override
                    public Matcher<View> getConstraints() {
                        return allOf(isDisplayed(), isAssignableFrom(TextView.class));
        //                                        
        // To check that the found view is TextView or it's subclass like EditText
        // so it will work for TextView and it's descendants
                    }

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

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

现在你的方法就是这样

          //need add your test case here
            @Test
            public void showTextView(){

                delay(2000);
                onView(withId(R.id.editText))
                        .perform(setTextInTextView("my text"));
                delay(2000);
            }

**现在您还可以添加延迟方法来显示模拟器或真实设备*

    /* delay checking of this position */
    private void delay(long i) {

    try {
        Thread.sleep(i);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

您只需在您已声明的布局中添加您的布局 TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:textColor="#123456"
        android:textSize="20dp"
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

** 在测试中编写代码 class 以检查文本是否显示在 textview 上**

 @Test
public void checkUserId(){
    Espresso.onView(withId(R.id.textView)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}

Kotlin 版本的 @Be_Negative 很棒的答案,

由于在 Espresso 中 TextView 上没有用于设置文本的默认值 ViewAction,因此您必须创建自己的文本。

第 1 步: 定义一个新的 ViewAction 以在 TextView 上设置文本,例如,

fun setTextInTextView(value: String): ViewAction {
    return object : ViewAction {
        override fun getConstraints(): Matcher<View> {
            return CoreMatchers.allOf(ViewMatchers.isDisplayed(), ViewMatchers.isAssignableFrom(TextView::class.java))
        }

        override fun perform(uiController: UiController, view: View) {
            (view as TextView).text = value
        }

        override fun getDescription(): String {
            return "replace text"
        }
    }
}

然后将其用作,

onView(withId(R.id.my_text_view)).perform(setTextInTextView("Espresso is awesome"))