android - 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)));
}
解决方案
我上周进入了这个问题。它可能会迟到,但它可能会在未来帮助一些人。
我创建了一个代表编码空间的变量。
private const val ENCODE_SPACE = '\u00A0'
然后,使用编码空间而不是空字符串。
"Welcome${ENCODE_SPACE}"
就我而言,问题来自项目其他部分的字符串连接。这是项目中可重用的代码。
推荐阅读
- php - 错误已弃用:将站点 magento 1.9 服务器传输到 localhost 时不建议使用 __autoload()
- object - java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为 ClassName
- java - 我正在尝试从 java spring boot 调用节点 api(扫描 aws),但从服务器获取 Unexpected end of file;
- reporting-services - SSRS 是否可以构建自定义组件
- python - python中有一个函数可以解决我的要求吗
- node.js - Express 代理是否有任何方式在所有情况下都保留原始 url?
- html - 如何不使用元素的标签或 ID 从源代码中通过 VBA 获取值
- nearprotocol - 检查 access_key 是否与 NEAR 帐户匹配的最佳方法
- reactjs - 如何使用 axios 上传图像数组?
- python - 使用 altair 将标识线添加到散点图