There is the following test, which when entering the login form, enters incorrect data, in response to which it displays a dialog box with the text "Warning":

@Test fun signInUserWithInvalidPassword() { goToSignIn() AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_email, VALID_EMAIL) AcceptanceHelper.updateValidationTextView(R.string.ui_data_attribute_password, "987654321") AcceptanceHelper.clickOnButtonInLayout(R.id.mainSignButton, R.string.common_signin_button_text, R.id.inputLayout) onView(isRoot()).perform(AcceptanceHelper.waitId(R.id.titleTextView, 5000)) AcceptanceHelper.checkTextView(R.id.titleTextView, "Warning") 

The problem is that the test falls on the line:

  onView(isRoot()).perform(AcceptanceHelper.waitId(R.id.titleTextView, 5000)) 

A pier opens in a fragment of a dialogue, the elements of which he does not find. And here is the actual waitId method waitId :

 fun waitId(id: Int, millis: Long): ViewAction { return object : ViewAction { override fun getConstraints(): Matcher<View> { return isRoot() } override fun getDescription(): String { return "wait for a specific view with id <$id> during $millis millis." } override fun perform(uiController: UiController, view: View) { uiController.loopMainThreadUntilIdle() val startTime = System.currentTimeMillis() val endTime = startTime + millis val viewMatcher = withId(id) do { for (child in TreeIterables.breadthFirstViewTraversal(view)) { // found view with required ID if (viewMatcher.matches(child)) { return } } uiController.loopMainThreadForAtLeast(50) } while (System.currentTimeMillis() < endTime) // timeout happens throw PerformException.Builder() .withActionDescription(this.description) .withViewDescription(HumanReadables.describe(view)) .withCause(TimeoutException()) .build() } } } 

Maybe someone came across, and can tell.

    1 answer 1

    As an option, you can use instead of your method in the following way:

     SystemClock.sleep(5000) 

    or

     Thread.sleep(1500); 

    You can also simplify your method and set timeout values ​​in advance:

      fun waitFor(id: Int, millis: Long): ViewAction { return object : ViewAction { override fun getConstraints(): Matcher<View> { return isRoot() } override fun getDescription(): String { return "Wait for a specific view with $id during $millis millis." } override fun perform(uiController: UiController, view: View) { uiController.loopMainThreadUntilIdle() } } } 

    But do not forget that these are not the best options, since you will have to constantly wait for the n-th number of times. It is best to load the items you need, in this case, the text you want to check as soon as the dialog has loaded. Therefore, try another such option as IdlingResource , and as an example, try to figure out by clicking on the link here .

    • Thanks came, now I'm trying to figure out the last option. It's not easy to figure it out yet. - Inkognito