1

I found a pretty cool lump of code from here that helps with performing a wait function until a resource withId(int) appears - and it seems to be working fine when I actually have a resource id to work with.

However, the app I am working with does not normally have simple resource id's and I have to work with Matchers instead.

An Example:

Matcher secondBanner = allOf(childAtPosition(allOf(withId(R.id.story_details_body),
                childAtPosition(IsInstanceOf.<View>instanceOf(
                android.widget.LinearLayout.class),2)),0)))

Is there any way to perform a similar wait on a Matcher like there is the resource id?

The code I am referring to in the previous link is -

/** Perform action of waiting for a specific view id. */
public static ViewAction waitId(final int viewId, final long millis) {
  return new ViewAction() {
    @Override
    public Matcher<View> getConstraints() {
        return isRoot();
    }

    @Override
    public String getDescription() {
        return "wait for a specific view with id <" + viewId + "> 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;
        final Matcher<View> viewMatcher = withId(viewId);

        do {
            for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                // found view with required ID
                if (viewMatcher.matches(child)) {
                    return;
                }
            }

            uiController.loopMainThreadForAtLeast(50);
        }
        while (System.currentTimeMillis() < endTime);

        // timeout happens
        throw new PerformException.Builder()
                .withActionDescription(this.getDescription())
                .withViewDescription(HumanReadables.describe(view))
                .withCause(new TimeoutException())
                .build();
    }
};
}

I've tried to simply just change everything from withId(int) to Matcher to no avail.

So is it possible to turn this bit of code into something that can perform a wait/timeout?

Thanks for any and all help.

Community
  • 1
  • 1
Nefariis
  • 3,451
  • 10
  • 34
  • 52

1 Answers1

1

You can simply use Thread.sleep(timeInMilisec). It would wait as is for some specified time.

or

Idling Resources It is ok, but you need to add some additional code not only in test but also in your project. You can specify in code when test should wait and when it should go forward again.

Piotr Mądry
  • 221
  • 2
  • 7