【发布时间】: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