Last active
November 27, 2023 15:45
-
-
Save XichengSpencer/90a8b53f6292be9a21f5495d7437b078 to your computer and use it in GitHub Desktop.
Error Text Test Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO(#3363): Test passes on Pixel3a sometimes and fails on Pixel3. | |
@Test | |
fun testAddProfileActivity_configChange_inputShortPin_create_pinLengthError() { | |
launch(AddProfileActivity::class.java).use { | |
testCoroutineDispatchers.runCurrent() | |
onView(isRoot()).perform(orientationLandscape()) | |
onView( | |
allOf( | |
withId(R.id.add_profile_activity_user_name_edit_text), | |
isDescendantOfA(withId(R.id.add_profile_activity_user_name)) | |
) | |
).perform(scrollTo()).perform( | |
editTextInputAction.appendText("test"), | |
closeSoftKeyboard() | |
) | |
testCoroutineDispatchers.runCurrent() | |
onView(withId(R.id.add_profile_activity_pin_check_box)).perform(scrollTo()) | |
onView(withId(R.id.add_profile_activity_pin_check_box)).perform(click()) | |
onView( | |
allOf( | |
withId(R.id.add_profile_activity_pin_edit_text), | |
isDescendantOfA(withId(R.id.add_profile_activity_pin)) | |
) | |
).perform(scrollTo()) | |
onView( | |
allOf( | |
withId(R.id.add_profile_activity_pin_edit_text), | |
isDescendantOfA(withId(R.id.add_profile_activity_pin)) | |
) | |
).perform( | |
editTextInputAction.appendText("12"), | |
closeSoftKeyboard() | |
) | |
testCoroutineDispatchers.runCurrent() | |
// S1 (very rare):Scrolling to view was attempted, but the view is not displayed | |
// S2 (very rare): failed to perform scroll to (Scrolling to view was attempted, but the view is not displayed) | |
onView(withId(R.id.add_profile_activity_create_button)).perform(scrollTo()) | |
// This line is to solve button not fully displayed which will failed the accessibility check | |
onView(withId(R.id.add_profile_activity_scroll_view)).perform(swipeUp()) | |
waitForMatch(withId(R.id.add_profile_activity_pin), 200L) | |
// S3(Most Common): Doesn't perform click | |
onView(withId(R.id.add_profile_activity_create_button)).perform(click()) | |
waitForMatch(withId(R.id.add_profile_activity_pin), 200L) | |
onView(withId(R.id.add_profile_activity_pin)).perform(scrollTo()) | |
// S3 result : Didn't scroll to add_profile_activity_pin check failed | |
// androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'The expected error text is 'Your PIN should be 3 digits long.'' doesn't match the selected view. | |
// Expected: The expected error text is 'Your PIN should be 3 digits long.' | |
// Got: "TextInputLayout{id=2131296340, res-name=add_profile_activity_pin, visibility=VISIBLE, width=786, | |
onView(withId(R.id.add_profile_activity_pin)) | |
.check( | |
matches( | |
hasErrorText( | |
context.resources.getString(R.string.add_profile_error_pin_length) | |
) | |
) | |
) | |
} | |
} | |
private fun waitForMatch(viewMatcher: Matcher<View>, millis: Long): ViewAction { | |
return object : ViewAction { | |
override fun getDescription(): String { | |
return "wait for a specific view with matcher <$viewMatcher> during $millis millis." | |
} | |
override fun getConstraints(): Matcher<View> { | |
return isRoot() | |
} | |
override fun perform(uiController: UiController?, view: View?) { | |
checkNotNull(uiController) | |
uiController.loopMainThreadUntilIdle() | |
val startTime = System.currentTimeMillis() | |
val endTime = startTime + millis | |
do { | |
if (TreeIterables.breadthFirstViewTraversal(view).any { viewMatcher.matches(it) }) { | |
return | |
} | |
uiController.loopMainThreadForAtLeast(50) | |
} while (System.currentTimeMillis() < endTime) | |
// Couldn't match in time. | |
throw PerformException.Builder() | |
.withActionDescription(description) | |
.withViewDescription(HumanReadables.describe(view)) | |
.withCause(TimeoutException()) | |
.build() | |
} | |
} | |
} |
Here is the updated video: I did the scroll to make the button fully visible and even tried to add delay before and after the click action. But the test is still flaky.
https://gist.github.com/assets/74568012/898a3733-33e7-4610-894b-99953989aacd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looked into this a bit, the issue here is that the create button is never fully displayed, therefore not clicked. This is probably a scrollview isue, and could be solved maybe using a nested scrollview, or a custom espresso function.