【问题标题】:Espresso Assert Text Does Match But Still FailsEspresso 断言文本匹配但仍然失败
【发布时间】:2018-10-19 15:32:01
【问题描述】:

我正在使用 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)));
}

【问题讨论】:

  • Espresso 识别白色字符。也许某处有牢不可破的空间或一些看不见的迹象。
  • 您的应用在登录时是否进行任何网络/数据库调用?可能在您尝试登录时和登录过程结束之前,espresso 会检查您想要的文本是否存在。您需要等待登录结束才能断言您的声明
  • 它确实会进行数据库调用,但仅限于本地。

标签: android testing android-espresso


【解决方案1】:

我上周讨论了这个问题。可能为时已晚,但它可能对未来的某些人有所帮助。

我创建了一个变量来表示编码空间。

private const val ENCODE_SPACE = '\u00A0'

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

 "Welcome${ENCODE_SPACE}"

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

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-12
    • 2013-03-17
    • 1970-01-01
    • 2012-06-21
    • 2012-08-29
    • 2021-10-09
    相关资源
    最近更新 更多