From 28e5401876f8d21db1ecf6dd608044c9e83d7832 Mon Sep 17 00:00:00 2001 From: owencooke Date: Sun, 3 Dec 2023 19:25:15 -0700 Subject: [PATCH] fix flaky tests --- .../househomey/EditItemFragmentTest.java | 14 +++++----- .../househomey/SignUpFragmentTest.java | 28 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/app/src/androidTest/java/com/example/househomey/EditItemFragmentTest.java b/app/src/androidTest/java/com/example/househomey/EditItemFragmentTest.java index 80f8a94..090cd99 100644 --- a/app/src/androidTest/java/com/example/househomey/EditItemFragmentTest.java +++ b/app/src/androidTest/java/com/example/househomey/EditItemFragmentTest.java @@ -43,13 +43,13 @@ public void navigateToEditItemFragment() throws Exception { // Create mock initial item in DB database.addTestItem(mockData); // Click to view the item page - waitFor(() -> onData(anything()) - .inAdapterView(withId(R.id.item_list)) - .atPosition(0) - .perform(click())); - // Click on edit button - waitFor(() -> onView(withId(R.id.edit_button)).perform(scrollTo())); - onView(withId(R.id.edit_button)).perform(click()); + waitFor(() -> { + onData(anything()) + .inAdapterView(withId(R.id.item_list)) + .atPosition(0) + .perform(click()); + onView(withId(R.id.edit_button)).perform(scrollTo(), click()); + }); } @Test diff --git a/app/src/androidTest/java/com/example/househomey/SignUpFragmentTest.java b/app/src/androidTest/java/com/example/househomey/SignUpFragmentTest.java index 0392adc..8847b19 100644 --- a/app/src/androidTest/java/com/example/househomey/SignUpFragmentTest.java +++ b/app/src/androidTest/java/com/example/househomey/SignUpFragmentTest.java @@ -3,7 +3,7 @@ import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.clearText; import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.action.ViewActions.typeText; +import static androidx.test.espresso.action.ViewActions.scrollTo; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.hasErrorText; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; @@ -41,16 +41,16 @@ public void setUp() { @Test public void testUsername() { - onView(withId(R.id.signup_username)).perform(typeText("antonio2")); + enterText(R.id.signup_username, "antonio2"); onView(withId(R.id.signup_username)).perform(clearText()); onView(withId(R.id.signup_username)).check(matches(hasErrorText("username cannot be empty"))); - onView(withId(R.id.signup_username)).perform(typeText("antonio2$")); + enterText(R.id.signup_username, "antonio2$"); onView(withId(R.id.signup_username)).check(matches(hasErrorText("only alphanumeric, underscore, or period"))); } @Test public void testEmail() { - onView(withId(R.id.signup_email)).perform(typeText("antonio2")); + enterText(R.id.signup_email, "antonio2"); onView(withId(R.id.signup_email)).check(matches(hasErrorText("not a valid email"))); onView(withId(R.id.signup_email)).perform(clearText()); onView(withId(R.id.signup_email)).check(matches(hasErrorText("email cannot be empty"))); @@ -65,27 +65,27 @@ public void testConfirmedPassword() { @Test public void testRegister() { - onView(withId(R.id.signup_username)).perform(typeText("ESPRESSO_GENERAL_USER")); - onView(withId(R.id.signup_email)).perform(typeText("espresso_general_user@example.com")); + enterText(R.id.signup_username, "ESPRESSO_GENERAL_USER"); + enterText(R.id.signup_email, "espresso_general_user@example.com"); enterText(R.id.signup_password, "12345"); enterText(R.id.signup_confirm_password, "12345"); - onView(withId(R.id.signup_button)).perform(click()); - waitFor(() -> onView(withId(R.id.signup_username)).check(matches(hasErrorText("username already exists")))); + onView(withId(R.id.signup_button)).perform(scrollTo(), click()); + waitFor(()->onView(withId(R.id.signup_username)).check(matches(hasErrorText("username already exists")))); enterText(R.id.signup_username, "a"); - onView(withId(R.id.signup_button)).perform(click()); - waitFor(() -> onView(withId(R.id.signup_password)).check(matches(hasErrorText("The given password is invalid. [ Password should be at least 6 characters ]")))); + onView(withId(R.id.signup_button)).perform(scrollTo(), click()); + waitFor(() -> onView(withId(R.id.signup_password)).check(matches(hasErrorText("The given password is invalid. [ Password should be at least 6 characters ]")))); onView(withId(R.id.signup_confirm_password)).check(matches(hasErrorText("The given password is invalid. [ Password should be at least 6 characters ]"))); enterText(R.id.signup_password, "123456"); enterText(R.id.signup_confirm_password, "123456"); - onView(withId(R.id.signup_button)).perform(click()); + onView(withId(R.id.signup_button)).perform(scrollTo(), click()); waitFor(() -> onView(withId(R.id.signup_email)).check(matches(hasErrorText("The email address is already in use by another account.")))); enterText(R.id.signup_email, "a@example.com"); - onView(withId(R.id.signup_button)).perform(click()); + onView(withId(R.id.signup_button)).perform(scrollTo(), click()); waitFor(() -> onView(withId(R.id.signin_username)).check(matches(isDisplayed()))); enterText(R.id.signin_username, "a"); enterText(R.id.signin_password, "123456"); - onView(withId(R.id.signin_button)).perform(click()); - waitFor(() -> hasListLength(0)); + onView(withId(R.id.signin_button)).perform(scrollTo(), click()); + waitFor(()->hasListLength(0)); DocumentReference docRef = db.collection("user").document("a"); docRef.delete().addOnSuccessListener(a -> Objects.requireNonNull(auth.getCurrentUser()).delete().addOnSuccessListener(aVoid -> Log.d("ESP-TEST", "Firebase Auth user deleted successfully")) .addOnFailureListener(e -> Log.e("ESP-TEST", "Could not delete Firebase Auth user: " + e.getMessage()))).addOnFailureListener(a -> Log.e("ESP-TEST", "Document failed to delete!"));