Jig*_*wda 38
对的,这是可能的.在其中一个示例中,他们已经在此处进行了演示https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/浓咖啡/ 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)));
}
Run Code Online (Sandbox Code Playgroud)
阅读内联评论.
等待新活动加载由Espresso隐含处理.
Val*_*rov 11
绝对有可能编写一个跨多个活动的Espresso(或任何基于仪器的)测试.您必须从一个Activity开始,但可以在应用程序的UI中导航到其他活动.唯一的警告 - 由于安全限制,测试流程必须保持在您的应用程序的过程中.
我测试过这个:
onView(withId(R.id.hello_visitor)).perform(click());
pressBack();
onView(withId(R.id.hello_visitor)).check(matches(isDisplayed())); //fails here
Run Code Online (Sandbox Code Playgroud)
点击操作显然会启动一项新活动.
假设您有两个活动:HomeActivity和SearchResultsActivity.对于测试,您希望对HomeActivity执行某些操作,并在SearchResultsActivity上验证结果.然后测试将如下所示:
public class SearchTest extends ActivityInstrumentationTestCase2<HomeActivity> {
public SearchTest() {
super(HomeActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
getActivity(); // launch HomeActivity
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSearch() {
onView(withId(R.id.edit_text_search_input)).perform(typeText("Hello World"));
onView(withId(R.id.button_search)).perform(click());
// at this point, another activity SearchResultsActivity is started
onView(withId(R.id.text_view_search_result)).check(matches(withText(containsString("Hello World"))));
}
}
Run Code Online (Sandbox Code Playgroud)
因此,您唯一需要注意的是,您应该从ActivityInstrumentationTestCase2 < FirstActivity > 扩展测试类,并在构造函数中调用super(FirstActivity .class).
以上示例相当容易.
高级示例(当startActivityForResult发生时):
有时写一个测试真的很混乱,你仍然有两个活动A和B,而且应用程序流程与上面不同:
即使整个测试部分发生在活动B上,您可能只需要验证活动A上的一小部分,但您的测试应该从ActivityInstrumentationTestCase2 < ActivityWhoCallsStartActivityForResult > 扩展,这是活动A,而不是活动B.否则,当测试部分是完成后,活动A将不会恢复,您无法验证结果.
| 归档时间: |
|
| 查看次数: |
33871 次 |
| 最近记录: |