Android Espresso 测试用于匹配两个 EditText 之间的文本内容
Android Espresso Testing for matching text content between two EditTexts
我创建了一个 Android 应用程序,其中包含登录编辑文本和密码编辑文本,条件是如果登录编辑文本和密码编辑文本中的内容都匹配,则其为 true 否则为 false 。如何使用 Espresso 单元测试对其进行测试
我使用了下面的代码。
@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;
}
};
}
但是它给了我如下所示的 Matcher 错误
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.matchesSafely(MainActivityTest.java:90)
at com.test.espressodemo.MainActivityTest.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.call(ViewInteraction.java:279)
at android.support.test.espresso.ViewInteraction.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)
isEditTextValueEqualTo
中的 view
实际上是 R.id.edtUserId
,所以 view.findViewById(R.id.edtPass)
会 return 为空。
试试改成这个
onView(withId(R.id.your_parent_view_that_has_edtPass)).check(matches(isEditTextValueEqualTo(R.id.edtPass, mStringToBetyped)));
如果你真的想使用自定义Matcher
,那么这个如何
Matcher<View> isEditTextValueEqualTo(final int viewId1, final int viewId2, 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 editTextValue1 = ((EditText) view.findViewById(viewId1)).getText().toString();
String editTextValue2 = ((EditText) view.findViewById(viewId2)).getText().toString();
if (editTextValue1.equalsIgnoreCase(editTextValue2)) {
return true;
}
}
return false;
}
};
}
在你的测试中:
onView(withId(R.id.your_parent_view_that_has_edtPass)).check(matches(isEditTextValueEqualTo(R.id.edtPass, R.id.edtUserId)));
这是一个不使用自定义匹配器的解决方案:
onView(withId(R.id.parentViewId)).check(matches(allOf(
hasDescendant(allOf(withId(R.id.edtUserId), withText(mStringToBetyped))),
hasDescendant(allOf(withId(R.id.edtPass), withText(mStringToBetyped)))
)));
但是由于您只比较 2 个视图中的文本,您可以只使用 Assert class as :
Assert.equals(edittext1.getText().toString(), edittext2.getText().toString())
我创建了一个 Android 应用程序,其中包含登录编辑文本和密码编辑文本,条件是如果登录编辑文本和密码编辑文本中的内容都匹配,则其为 true 否则为 false 。如何使用 Espresso 单元测试对其进行测试
我使用了下面的代码。
@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;
}
};
}
但是它给了我如下所示的 Matcher 错误
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.matchesSafely(MainActivityTest.java:90)
at com.test.espressodemo.MainActivityTest.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.call(ViewInteraction.java:279)
at android.support.test.espresso.ViewInteraction.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)
isEditTextValueEqualTo
中的 view
实际上是 R.id.edtUserId
,所以 view.findViewById(R.id.edtPass)
会 return 为空。
试试改成这个
onView(withId(R.id.your_parent_view_that_has_edtPass)).check(matches(isEditTextValueEqualTo(R.id.edtPass, mStringToBetyped)));
如果你真的想使用自定义Matcher
,那么这个如何
Matcher<View> isEditTextValueEqualTo(final int viewId1, final int viewId2, 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 editTextValue1 = ((EditText) view.findViewById(viewId1)).getText().toString();
String editTextValue2 = ((EditText) view.findViewById(viewId2)).getText().toString();
if (editTextValue1.equalsIgnoreCase(editTextValue2)) {
return true;
}
}
return false;
}
};
}
在你的测试中:
onView(withId(R.id.your_parent_view_that_has_edtPass)).check(matches(isEditTextValueEqualTo(R.id.edtPass, R.id.edtUserId)));
这是一个不使用自定义匹配器的解决方案:
onView(withId(R.id.parentViewId)).check(matches(allOf(
hasDescendant(allOf(withId(R.id.edtUserId), withText(mStringToBetyped))),
hasDescendant(allOf(withId(R.id.edtPass), withText(mStringToBetyped)))
)));
但是由于您只比较 2 个视图中的文本,您可以只使用 Assert class as
Assert.equals(edittext1.getText().toString(), edittext2.getText().toString())