gpt4 book ai didi

android - TextView 中的 Espresso withText 与所选 View 不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:17 29 4
gpt4 key购买 nike

我遇到了 Espresso 的奇怪测试失败。下面是在显示的Dialog中测试一个TextView。我收到以下错误:

   com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with string from resource id: <2131099772>' doesn't match the selected view.
Expected: with string from resource id: <2131099772>[my_content] value: Test Content Available
Got: "TextView{id=2131296340, res-name=dialog_content, visibility=VISIBLE, width=620, height=38, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Test Content Available, input-type=0, ime-target=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:69)
at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:40)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:159)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.check(ViewInteraction.java:133)
at com.myapp.testContentDetails(FPATest.java:109)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: junit.framework.AssertionFailedError: 'with string from resource id: <2131099772>' doesn't match the selected view.
Expected: with string from resource id: <2131099772>[my_content] value: Test Content Available
Got: "TextView{id=2131296340, res-name=dialog_content, visibility=VISIBLE, width=620, height=38, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Test Content Available, input-type=0, ime-target=false}"

at com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:789)
at com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:76)
at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$2.run(ViewInteraction.java:145)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5081)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

很明显,找到了 View ,在它的详细信息中,您可以看到 text= 我期望的值。然而它说不匹配。它使用以下测试语句运行

onView(withId(R.id.dialog_content)).check(
matches(withText(R.string.my_content)));

我也尝试用下面的语句来做,但是得到了一个 NoMatchingViewException,即使通过 View 在异常的 View 层次结构中显示也是如此。

onView(withText(R.string.my_content)).check(
matches(isDisplayed()));

关于为什么会失败的任何帮助?值得注意的是,我能够在对话框的同级字段上使用 withText() isDisplayed()。

最佳答案

这个答案是根据@haffax 上面的评论创建的。

/**
* Original source from Espresso library, modified to handle spanned fields
*
* Returns a matcher that matches a descendant of {@link TextView} that is
* displaying the string associated with the given resource id.
*
* @param resourceId
* the string resource the text view is expected to hold.
*/
public static Matcher<View> withText(final int resourceId) {

return new BoundedMatcher<View, TextView>(TextView.class) {
private String resourceName = null;
private String expectedText = null;

@Override
public void describeTo(Description description) {
description.appendText("with string from resource id: ");
description.appendValue(resourceId);
if (null != this.resourceName) {
description.appendText("[");
description.appendText(this.resourceName);
description.appendText("]");
}
if (null != this.expectedText) {
description.appendText(" value: ");
description.appendText(this.expectedText);
}
}

@Override
public boolean matchesSafely(TextView textView) {
if (null == this.expectedText) {
try {
this.expectedText = textView.getResources().getString(
resourceId);
this.resourceName = textView.getResources()
.getResourceEntryName(resourceId);
} catch (Resources.NotFoundException ignored) {
/*
* view could be from a context unaware of the resource
* id.
*/
}
}
if (null != this.expectedText) {
return this.expectedText.equals(textView.getText()
.toString());
} else {
return false;
}
}
};
}

关于android - TextView 中的 Espresso withText 与所选 View 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26150522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com