首页 > 解决方案 > Espresso 断言文本匹配但仍然失败

问题描述

我正在使用 Espresso 来创建测试。用户进入注册界面,输入详细信息,成功注册用户。用户然后进入登录屏幕,输入用户名并进入仪表板。仪表板有一条向用户打招呼的消息。

测试运行良好,但在检查仪表板消息时失败。它应该说“欢迎{用户的全名}”。但是错误日志表明它失败了:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Welcome Jane Doe"' 与所选视图不匹配。预期:带文字:是“Welcome Jane Doe”

这是我的测试:

// Test when user tries to register with alpha numeric character and correct email format, register user. Go to login screen and
// login to the dashboard. Should only display full name
@Test
public void testRegisterWithValidUsernameEmailThenLogin(){
    // register user
    onView(ViewMatchers.withId(R.id.usernameEditText)).perform(clearText(), typeText(studentFullNameTwo.getUsername()), closeSoftKeyboard());
    onView(ViewMatchers.withId(R.id.emailEditText)).perform(clearText(), typeText(studentFullNameTwo.getEmail()), closeSoftKeyboard());
    onView(ViewMatchers.withId(R.id.registerButton)).perform(click());
    // login with new user
    onView(ViewMatchers.withId(R.id.editTextUsername)).perform(clearText(), typeText(studentFullNameTwo.getUsername()), closeSoftKeyboard());
    onView(ViewMatchers.withId(R.id.buttonLogin)).perform(click());
    // check welcome header
    String fullName = studentFullNameTwo.getFirstName() + " " + studentFullNameTwo.getLastName();
    // Change view to dashboard screen
    onView(withId(R.id.loggedUserDashHeader)).check(matches(withText("Welcome " + fullName)));
}

标签: androidtestingandroid-espresso

解决方案


我上周进入了这个问题。它可能会迟到,但它可能会在未来帮助一些人。

我创建了一个代表编码空间的变量。

private const val ENCODE_SPACE = '\u00A0'

然后,使用编码空间而不是空字符串。

 "Welcome${ENCODE_SPACE}"

就我而言,问题来自项目其他部分的字符串连接。这是项目中可重用的代码。


推荐阅读