以下是我的Espresso测试用例之一.
public void testLoginAttempt() {
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com"));
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
// AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.
// Clicking launches a new activity that shows the text entered above. You don't need to do
// anything special to handle the activity transitions. Espresso takes care of waiting for the
// new activity to be resumed and its view hierarchy to be laid out.
Espresso.onView(ViewMatchers.withId(R.id.action_logout))
.check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
}
Run Code Online (Sandbox Code Playgroud)
目前我所做的是检查新活动(R.id.action_logout)中的视图是否可见.如果可见,我将假设活动成功打开.但它似乎没有像我预期的那样工作.有没有更好的方法来检查是否成功启动了新活动而不是检查该活动中的视图是否可见?谢谢
我正在尝试一些基本的webView测试:
public class TheMainActivity {
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class, false, false) {
@Override
protected void afterActivityLaunched() {
onWebView().forceJavascriptEnabled();
}
};
@Test
public void theIntroductionShouldShow() {
activityTestRule.launchActivity(null);
onWebView().check(webMatches(getText(), containsString("SURVIVAL ACTIONS")));
}
}
Run Code Online (Sandbox Code Playgroud)
关于本文档-但我得到:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: WebView with JS enabled
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1776, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
|
+->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1776, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, …Run Code Online (Sandbox Code Playgroud) 这是代码的存根。
单击上的数据项ListView。按照设计方式工作并打开Chrome自定义标签:
onData(anything()).inAdapterView(withId(R.id.listView))
.atPosition(0).perform(click());
Pause(5000);
Espresso.pressBack();
Run Code Online (Sandbox Code Playgroud)
似乎无法评估标签中的任何内容,甚至无法点击设备后退按钮。得到这个错误
Error : android.support.test.espresso.NoActivityResumedException: No
activities in stage RESUMED.
Run Code Online (Sandbox Code Playgroud)
您是否忘了发起活动。(test.getActivity() 或类似)?