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

6

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

例如:Android Espresso测试应用程序流程

但是上面的答案在Espresso 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()));
}

如果一个测试文件中不允许多个活动进行测试,那么如何组织流程来测试多个活动?

你能分享一下你的调用方和被调用方活动的截图吗? - Hasaan Ali
@2BAB,嘿伙计。你解决了那个问题吗?我也遇到了同样的问题。 - optimus prime
@2BAB,你找到解决方案了吗?我这里也有同样的问题。 - JJJ
2个回答

1
I've got同样的麻烦。Espresso不会等待新的activity运行,即使我使用了idling资源。所以它迫使我在检查新activity的视图之前设置延迟。
创建接口:
/**
* 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); 

这个能用吗?我遇到了同样的问题。这是可行的解决方案吗? - Md. Tahmid Mozaffar
@Md.TahmidMozaffar 我编辑了答案。这是一种更聪明的等待活动开始或任何挂起操作的方式。而且,它是100%有效的解决方案。您只需要用您的变量替换check()方法即可。 - MaxF

-2

是的,这是可能的。在他们展示的样例中,他们演示了这个功能,链接在这里 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)));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接