Reputation: 3121
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!
Upvotes: 9
Views: 6844
Reputation: 15103
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
Upvotes: 6
Reputation: 15103
I ended up using another approach different from my first answer that works good too but it is more easy to adapt to anything, by using a CountdownLatch
.
Here is the code:
public static boolean viewExists(final Matcher<View> viewMatcher, final long millis) throws InterruptedException {
final Boolean[] found = new Boolean[1];
final CountDownLatch latch = new CountDownLatch(1);
ViewAction action = new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isRoot();
}
@Override
public String getDescription() {
return "wait for a specific view with id <" + viewMatcher.toString() + "> during " + millis + " millis.";
}
@Override
public void perform(final UiController uiController, final View view) {
uiController.loopMainThreadUntilIdle();
final long startTime = System.currentTimeMillis();
final long endTime = startTime + millis;
do {
for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
if (viewMatcher.matches(child)) {
Log.d(TAG, "perform: found match");
found[0] = true;
latch.countDown();
return;
}
}
uiController.loopMainThreadForAtLeast(50);
}
while (System.currentTimeMillis() < endTime);
found[0] = false;
latch.countDown();
}
};
onView(isRoot()).perform(action);
latch.await();
return found[0];
}
Google approach is to use Idling resource classes, but requires to insert test code into production apk, or use flavors and Dependency Injection pattern to avoid it.
Upvotes: 0
Reputation: 3121
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( );
Upvotes: 4