我刚刚开始使用espresso进行UI测试。我编写了一个简单的测试,试图测试用户是否正确注册。它目前看起来像这样:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class RegisterTest {
@Rule
public ActivityTestRule<RegisterActivity> mActivityRule =
new ActivityTestRule<>(RegisterActivity.class);
@Before
public void runThisBefore(){
}
@Test
public void registerUserCorrect(){
onView(withId(R.id.etUsername)).perform(typeText("ESPRESSOTEST"));
onView(withId(R.id.etPassword)).perform(typeText("ESPRESSOTEST"));
onView(withId(R.id.etConfirmPassword)).perform(typeText("ESPRESSOTEST"));
onView(withId(R.id.etEmail)).perform(typeText("ESPRESSOTEST@gmail.com"));
closeSoftKeyboard();
onView(withId(R.id.etConfirmEmail)).perform(typeText("ESPRESSOTEST@gmail.com"));
closeSoftKeyboard();
onView(withId(R.id.etAge)).perform(typeText("25"));
closeSoftKeyboard();
onView(withId(R.id.rbMale)).perform(click());
onView(withId(R.id.bRegister)).perform(click());
onView(allOf(withId(android.R.id.button1), withText("Ok"), isDisplayed())).perform(click());
}
@After
public void checkLogin(){
//delete user;
}
}
点击alertDialog按钮后......
final Firebase registerUser = new Firebase(FirebaseReference.FIREBASE_URL);
registerUser.createUser(userEmail, userPassword, new Firebase.ValueResultHandler<Map<String, Object>>(){
@Override
public void onSuccess(Map<String, Object> stringObjectMap) {
registerUser.authWithPassword(userEmail, userPassword, new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
Map<String, Object> userMap = new HashMap<String, Object>(); //create new hashmap
userMap.put(FirebaseReference.KEY_PROVIDER, authData.getProvider()); //authentication method
userMap.put(FirebaseReference.KEY_USER_NAME, userName); //set username
userMap.put(FirebaseReference.KEY_EMAIL, authData.getProviderData().get(FirebaseReference.KEY_EMAIL)); //user email
userMap.put(FirebaseReference.KEY_AGE, userAge);
userMap.put(FirebaseReference.KEY_SEX, userSex);
//create timestamp (purpose is for easy user comparison later on)
long createTime = new Date().getTime();
userMap.put(FirebaseReference.KEY_TIMESTAMP, String.valueOf(createTime));
//set the map to a new node in the users child
registerUser.child(FirebaseReference.CHILD_USERS).child(authData.getUid()).setValue(userMap);
Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
Toast.makeText(RegisterActivity.this, "An error occurred!", Toast.LENGTH_SHORT).show();
finish();
}
});
}
@Override
public void onError(FirebaseError firebaseError) {
showErrorMessageToUser(firebaseError.getMessage());
}
});
单击注册按钮后,我的firebase引用会调用createUser()
,它会等待两种回调方法之一。 onSuccess()
或onError()
。但是,使用espresso,只要单击注册按钮,测试就会结束,甚至不会发生网络活动。有没有办法让测试等待,然后检查触发了哪个回调方法?