Issue
I am able to save the Data to the Shared preferences .But i got Stucked in one part that ,when both the username and password field is supplied with the Checkbox checked and Login is clicked .Then Login got Success.And in the Another testCase the userName should be filled with the previous provided data.I am not set name in the userName field in edittext in the next testCase?
success 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
}
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)));
}
I got stucked in 2nd one testCase.How can this issue be solved??
Solution
You can simply check the Data of the SharedPreference on same testCase.you can simply finish the Activity and StartAgain to check either the UserName Field is setted with the SharedPreference Data or not?
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.
Answered By - seon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.