I'm working with some simple tests with Espresso. One of them is about click on view and check if a Dialog is showed.
My problem is that sometimes works, and sometimes not. It only work always if I put a sleep before check the dialog. Any solution without ussing sleep?
Here is my code (so simple):
onView(withId(R.id.forgot_password)).perform(click());
// only works if use Thread.sleep(ms) here
onView(withText(R.string.reset_password)).check(matches(isDisplayed()));
Edit:
I'm showing dialog with static helpers, but the simplification is this. And I'm not performing any background task in the middle.
final TextInputDialog textInputDialog = new
TextInputDialog.Builder(context)
.setTitle(titleId)
.setInputType(inputType)
.setHint(hintId)
.setPreFilledText(preFilledText)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(positiveButtonId, onTextSubmittedListener)
.create();
textInputDialog.show(textInputDialog);
Thank you!
In some cases disable animations is not possible like:
In those cases the simplest is to wait for an element to show. Here is how:
Simple helper:
class WaifForUIUpdate {
public static void waifForWithId(@IdRes int stringId) {
ViewInteraction element;
do {
waitFor(500);
//simple example using withText Matcher.
element = onView(withText(stringId));
} while (!MatcherExtension.exists(element));
}
static void waitFor(int ms) {
final CountDownLatch signal = new CountDownLatch(1);
try {
signal.await(ms, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Assert.fail(e.getMessage());
}
}
}
Matcher from twisterrob
public class MatcherExtension {
@CheckResult
public static boolean exists(ViewInteraction interaction) {
try {
interaction.perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return any(View.class);
}
@Override
public String getDescription() {
return "check for existence";
}
@Override
public void perform(UiController uiController, View view) {
// no op, if this is run, then the execution will continue after .perform(...)
}
});
return true;
} catch (AmbiguousViewMatcherException ex) {
// if there's any interaction later with the same matcher, that'll fail anyway
return true; // we found more than one
} catch (NoMatchingViewException ex) {
return false;
} catch (NoMatchingRootException ex) {
// optional depending on what you think "exists" means
return false;
}
}
}
Usage:
WaifForUIUpdate.waifForWithId(R.string.some_string);
//now do your validations
Finally it seems like the problem was animations. For make Espresso works properly is needed to disable animations in developer options menu.
In this case, the problem was solved. But there are other cases in which the problem could be a background task, like the comments to my question suggest. So I recommend to have a look at IdlingResource https://medium.com/azimolabs/wait-for-it-idlingresource-and-conditionwatcher-602055f32356#.pw55uipfj or this Espresso: Thread.sleep( );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With