我可以将数据保存到共享偏好设置。但我得到了Stucked的一个部分,当用户名和密码字段都提供了复选框选中并单击登录时。然后登录成功。然后在另一个testCase userName应该填充先前提供的数据。我没有在下一个testCase的edittext中的userName字段中设置名称?
成功testCase
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
}
如何在NextCase
中的editText中设置userName
@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中。这个问题怎么解决?
答案 0 :(得分:1)
你可以在同一个testCase上查看SharedPreference的数据。你可以简单地完成Activity和StartAgain以检查UserName字段是否设置了SharedPreference数据?
你可以在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()));
}
这可能对你有帮助。