Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso 2 - How to Test Multiple Activities?

I have seen some questions about it.

eg. Android Espresso testing app flow

But the anwser above is not working in espresso 2. Here is my snippet

@Rule
public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);


@Test
public void splashActivityTest() {
    onView(withId(R.id.splash_container)).perform(swipeLeft());
    onView(withId(R.id.splash_container)).perform(swipeLeft());

    // launch the main activity
    ViewInteraction appCompatButton = onView(
            allOf(withId(R.id.introduction_goto_btn), withText("goToMainActivity"), isDisplayed()));
    appCompatButton.perform(click());

    // the hierarchy can't find HomeBtn , it still hold the Splash's View, so the code below will fail
    onView(withId(R.id.home_btn)).check(ViewAssertions.matches(isDisplayed()));
}

If Multi-Activities Test is not allow in one TestFile, then how to make a flow to test multiple activities?

like image 750
2BAB Avatar asked Aug 18 '16 04:08

2BAB


1 Answers

I've got same trouble. Espresso doesn't wait for new activity run, even when I used an idling. So it forced me to set delays before checking views on new activity

Create interface:

/**
* Interface for expectations of compliance with the conditions.
*/
public interface Condition {
 /**
  * @return text description for log output when check failed.
 */
 String getDescription();

 /**
  * @return true if the condition is met.
  */
 boolean check();
}

And use it like this:

/**
* Wait while condition come true or timeout limit.
*
* @param condition condition for exit
* @param timeout   limit in seconds
* @throws Exception exception
*/
public static void waitForCondition(Condition condition, int timeout) throws Exception {
final int CONDITION_NOT_MET = 0;
final int CONDITION_MET = 1;
final int TIMEOUT = 2;

final int INTERVAL = 250;

int status = CONDITION_NOT_MET;
int elapsedTime = 0;

do {
  if (condition.check()) {
    status = CONDITION_MET;
  } else {
    elapsedTime += INTERVAL;
    delay(INTERVAL);
  }

  if (elapsedTime >= timeout * 1000) {
    status = TIMEOUT;
    break;
  }
} while (status != CONDITION_MET);

if (status == TIMEOUT) {
  String msg = condition.getDescription() + " - took more than " + timeout + " seconds. Test stopped.";
  log(msg);
  throw new Exception(msg);
 }
}

Example:

public class MovieScreenVisible implements Condition {
  @Override
  public String getDescription() {
    return "Movie screen should be on the top";
  }

  @Override
  public boolean check() {
    Activity activity = TestBase.getCurrentActivity();
    if (activity == null || !(activity instanceof MovieActivity)) {
      return false;
    }

    ViewGroup layout = activity.findViewById(R.id.movie_fragment);
    return layout != null && layout.getVisibility() == View.VISIBLE;
  }
}

// wait maximum 30 seconds until movie screen should be visible
waitForCondition(new MovieScreenVisible(), 30); 
like image 126
MaxF Avatar answered Sep 20 '22 16:09

MaxF