Android Espresso 失败 AssertionFailedWithCauseError

Android Espresso fails AssertionFailedWithCauseError

我正在尝试 运行 在包含多个 EditTexts 的布局上进行 Android Espresso 测试。每个 EditText 都有一个唯一的 id。我的测试成功地清除了每个 EditText 中的文本,但无法用我的 String (stringToBeTyped = "123 Sesame St.").

替换文本

同样的测试也在其他只有一个 EditText 的布局上成功 运行。我的预感是 Espresso 中可能存在与每个布局多个 EditTexts 相关的错误。

我的测试方法:

void performEditTextComparisonAndModificationTest(int resId, String initialText, String stringToBeTyped) {
    onView(withId(resId)).check(matches(withText(initialText)));
    onView(withId(resId)).perform(click(), replaceText(stringToBeTyped));
    onView(withId(resId)).check(matches(withText(stringToBeTyped)));
}

我的logcat输出:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "123 Sesame St."' doesn't match the selected view.
Expected: with text: is "123 Sesame St."
Got: "EditText{id=3, visibility=VISIBLE, width=432, height=49, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=true, editor-info=[inputType=0x2071 imeOptions=0x8000005 privateImeOptions=null actionLabel=null actionId=0 initialSelStart=0 initialSelEnd=0 initialCapsMode=0x2000 hintText=Street Address label=null packageName=null fieldId=0 fieldName=null extras=null ], x=0.0, y=0.0, text=, hint=Street Address, input-type=8305, ime-target=false, has-links=false}"

从第三行 logcat 的末尾可以看出,EditText 的 text=""。为什么是这样?这可能是 espresso 框架中的错误吗?

替换onView(withId(resId)).perform(click(), replaceText(stringToBeTyped));

onView(withId(resId)).perform(click(), clearText(), replaceText(stringToBeTyped));

看起来它可能正在保存提示文本并且没有正确替换字符串。

编辑:

再看一遍,我认为您正试图将最后一步中的字符串与 resId 相匹配,这就是您得到 'Expected: 123 sesame, Got: EditText' 的原因试试这个:

onView(allOf(withId(resId), withText(stringToBeTyped))).check(matches(withText(stringToBeTyped)));