今天试着用 espresso 去写脚本,发现它是基于 activity 的,就是每个 test 都会跟一个 activity 绑定吧,代码如下:
@Rule
public ActivityTestRule mLoginActivityTestRule =
new ActivityTestRule(LoginActivity.class);
@Test
public void clickLoginButton_showsSuccessScreenAfterLogin() {
String email = "username@email.com";
String password = "password";
//type in email
onView(withId(R.id.edit_text_email)).perform(typeText(email), closeSoftKeyboard());
//type in password
onView(withId(R.id.edit_text_password)).perform(typeText(password), closeSoftKeyboard());
//click on login button
onView(withId(R.id.button_login)).perform(click());
//verify that success screen shows
String successString = InstrumentationRegistry.getTargetContext().getString(R.string.text_login_successful);
onView(withText(successString)).check(matches(isDisplayed()));
但是我跟 Android 研发同事聊,他们用了 RN 框架后就一个 MainActivity,这样的话是不是 espresso 也不能用了