Issue
I have created a Android app which has Login Edit Text & Password EditText with condition that if both contents in Login EditText & Password EditText matches then its true else false . How to test it using Espresso Unit Testing
I have used below code .
@Test
public void validLogin() {
onView(withId(R.id.edtUserId))
.perform(typeText(mStringToBetyped), closeSoftKeyboard());
onView(withId(R.id.edtPass))
.perform(typeText(mValidPass), closeSoftKeyboard());
onView(withId(R.id.loginBtn)).perform(click());
// onView(allOf(withId(R.id.edtUserId))).check(matches(withText(String.valueOf(allOf(withId(R.id.edtPass))))));
onView(withId(R.id.edtUserId)).check(matches(isEditTextValueEqualTo(R.id.edtPass, mStringToBetyped)));
/* onView(allOf(withId(R.id.edtUserId)))
.check(matches(withText(String.valueOf(withId(R.id.edtPass)))));*/
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Matcher<View> isEditTextValueEqualTo(final int viewId, final String content) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Match Edit Text Value with View ID Value : : " + content);
}
@Override
public boolean matchesSafely(View view) {
if (view != null) {
String editTextValue = ((EditText) view.findViewById(viewId)).getText().toString();
if (editTextValue.equalsIgnoreCase(content)) {
return true;
}
}
return false;
}
};
}
But it gives me error for Matcher as seen below
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.test.espressodemo.MainActivityTest$1.matchesSafely(MainActivityTest.java:90)
at com.test.espressodemo.MainActivityTest$1.matchesSafely(MainActivityTest.java:80)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:65)
at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:526)
at android.support.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:103)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:415)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:279)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:265)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Solution
Here's a solution without using a custom matcher:
onView(withId(R.id.parentViewId)).check(matches(allOf(
hasDescendant(allOf(withId(R.id.edtUserId), withText(mStringToBetyped))),
hasDescendant(allOf(withId(R.id.edtPass), withText(mStringToBetyped)))
)));
But since you're only comparing the text in 2 views, you can just use the Assert class as Bach Vu mentioned:
Assert.equals(edittext1.getText().toString(), edittext2.getText().toString())
Answered By - A Droid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.