Espresso 2 - 如何测试多项活动?

时间:2016-08-18 04:15:46

标签: android-espresso

我已经看到了一些关于它的问题。

例如。 Android Espresso testing app flow

但上面的anwser在浓咖啡2中不起作用。这是我的片段

@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()));
}

如果一个TestFile中不允许进行多活动测试,那么如何建立一个流来测试多个活动?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的麻烦。即使我使用空转,Espresso也不会等待新活动的运行。所以它迫使我在检查新活动的观点之前设置延迟

创建界面:

/**
* 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();
}

并像这样使用它:

/**
* 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);
 }
}

示例:

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); 

答案 1 :(得分:0)

是的,有可能。在其中一个样本中,他们已在此处演示https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java

@Test
public void changeText_newActivity() {
// Type text and then press the button.


onView(withId(R.id.editTextUserInput)).perform (typeText(STRING_TO_BE_TYPED),
        closeSoftKeyboard());
onView(withId(R.id.activityChangeTextBtn)).perform(click());

// This view is in a different Activity, no need to tell Espresso.
onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED)));
}