如何使用 Espresso 为下一次测试检查保存的数据?

How can the Saved Data be checked for next test using Espresso?

我能够将数据保存到共享首选项中。但是我卡在了其中一部分,当提供用户名和密码字段时选中了复选框并单击了登录。然后登录得到 Success.And 在另一个测试用例中,用户名应该填写之前提供的 data.I 在下一个测试用例中,我没有在 edittext 的用户名字段中设置名称吗?

成功测试用例

 String userName = "984********";
    String passWord = "123456789";

    Intent intent;
    SharedPreferences.Editor preferencesEditor;

   @Test
    public void btnLoginClickWithUserNameAndPasswordProvidedWithCheckedCheckBox() throws Exception {

        onView(withId(R.id.etUsername)).perform(typeText(userName));
        onView(withId(R.id.etPassword)).perform(typeText(passWord));
        onView(withText(R.string.remember_username)).check(matches(isNotChecked())).perform(scrollTo(), setChecked(true));
        onView(withId(R.id.btnLogin)).perform(click());
//        preferencesEditor.putString("username", "userName"); // Storing string
//        preferencesEditor.commit(); // commit changes
    }

How the userName is setted in the editText in the NextCase

  @Test
    public void ifCheckBoxisChecked() throws Exception {
        btnLoginClickWithUserNameAndPasswordProvidedWithCheckedCheckBox();

        preferencesEditor.putString("username", "userName"); // Storing string
        preferencesEditor.commit(); // commit changes


        onView(withId(R.id.etUsername)).check(matches(withText(userName)));
    }

我卡在第二个testCase.How这个问题能解决吗??

您可以简单地检查相同的 SharedPreference 数据 testCase.you 可以简单地完成 Activity 和 StartAgain 来检查用户名字段是否设置了 SharedPreference 数据?

you can use below testCode

   @Test
    public void btnLoginClickWithUserNameAndPasswordProvidedWithCheckedCheckBox() throws Exception {
        onView(withId(R.id.etUsername)).perform(clearText());
        onView(withId(R.id.etUsername)).perform(typeText(userName));
        onView(withId(R.id.etPassword)).perform(typeText(passWord));
        onView(withText(R.string.remember_username)).check(matches(isNotChecked())).perform(scrollTo(), setChecked(true));
        onView(withId(R.id.btnLogin)).perform(click());
        preferencesEditor.putString("username", "userName"); // Storing string
        preferencesEditor.commit(); // commit changes
        mActivityRule.getActivity().finish();
        mActivityRule.getActivity().startActivity(new Intent(
                mActivityRule.getActivity().getApplicationContext(), mActivityRule.getActivity().getClass()));
    }

That mightbe helpful to you.