@Test public void testDescriptionExpand() { setupMockServer(null); activityRule.launchActivity(intent); TimingIdlingResource idlingResource = new TimingIdlingResource(3000); // wait dummy network operation Espresso.registerIdlingResources(idlingResource); onView(withId(R.id.description)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE))); onView(withId(R.id.description_parent)).perform(click()); onView(withId(R.id.description)) .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))); Espresso.unregisterIdlingResources(idlingResource); }
public static void screenshot(final String screenshotName) { // Use Espresso to find the Activity of the Root View. Using a ViewMatcher guarantees that // the Activity of the view is RESUMED before taking the screenshot Espresso.onView(ViewMatchers.isRoot()) .perform( new ViewAction() { @Override public Matcher<View> getConstraints() { return ViewMatchers.isDisplayed(); } @Override public String getDescription() { return "taking screenshot of the Activity"; } @Override public void perform(UiController uiController, View view) { final Activity activity = scanForActivity(view.getContext()); if (activity == null) { throw new IllegalStateException( "Couldn't get the activity from the view context"); } if (!TAG_PATTERN.matcher(screenshotName).matches()) { throw new IllegalArgumentException( "Tag may only contain the letters a-z, A-Z, the numbers 0-9, " + "underscores, and hyphens"); } try { File screenshotDirectory = getFilesDirectory(activity.getApplicationContext(), Locale.getDefault()); String screenshotFileName = screenshotName + NAME_SEPARATOR + System.currentTimeMillis() + EXTENSION; File screenshotFile = new File(screenshotDirectory, screenshotFileName); takeScreenshot(activity, screenshotFile); Log.d(TAG, "Captured screenshot \"" + screenshotFileName + "\""); } catch (Exception e) { throw new RuntimeException("Unable to capture screenshot.", e); } } private Activity scanForActivity(Context context) { if (context == null) { return null; } else if (context instanceof Activity) { return (Activity) context; } else if (context instanceof ContextWrapper) { return scanForActivity(((ContextWrapper) context).getBaseContext()); } return null; } }); }
@Test public void goUserPageFromComment() { setupMockServer(null); activityRule.launchActivity(intent); TimingIdlingResource idlingResource = new TimingIdlingResource(3000); // wait dummy network operation Espresso.registerIdlingResources(idlingResource); onView(withId(R.id.comment_parent)).perform(clickComment()); Espresso.unregisterIdlingResources(idlingResource); Instrumentation.ActivityMonitor receiverActivityMonitor = InstrumentationRegistry.getInstrumentation() .addMonitor(UserActivity.class.getName(), null, false); Activity activity = receiverActivityMonitor.waitForActivityWithTimeout(1000); assertEquals("Launched Activity is not UserActivity", UserActivity.class, activity.getClass()); onView(withText("¡! Nature B■x !¡")).check(matches(isDisplayed())); pressBack(); SystemClock.sleep(2000); }
public void testChangeText() { // 获取某个View ViewInteraction viewFocus = Espresso.onView(ViewMatchers.withId(R.id.et)); // 执行某个动作 viewFocus.perform(ViewActions.clearText()); viewFocus.perform(ViewActions.typeText("new")); TextView tv = (TextView) activity.findViewById(R.id.et); // 检查结果 EspressoTest.assertEquals("new", tv.getText() + ""); }
/** * Helper method that waits until the given Snackbar has been fully dismissed. Note that calling * this method will reset the currently set {@link Snackbar.Callback}. */ public static void waitUntilFullyDismissed(Snackbar snackbar) { SnackbarDismissedCallback snackbarCallback = new SnackbarDismissedCallback(); snackbar.setCallback(snackbarCallback); try { // Register our listener as idling resource so that Espresso waits until the // the snackbar has been fully dismissed Espresso.registerIdlingResources(snackbarCallback); // Mark the callback to require waiting for idle state snackbarCallback.mNeedsIdle = true; // Perform a dummy Espresso action that loops until the UI thread is idle. This // effectively blocks us until the Snackbar has completed its sliding animation. onView(isRoot()).perform(waitUntilIdle()); snackbarCallback.mNeedsIdle = false; } finally { // Unregister our idling resource Espresso.unregisterIdlingResources(snackbarCallback); // And remove our tracker listener from Snackbar snackbar.setCallback(null); } }
/** TODO: Passed List data and detail data is wrong item */ @Test public void checkDetailInfo_isSet() { setupMockServer(null); activityRule.launchActivity(intent); onView(withId(R.id.title)) .check(matches(withText("A Quiet Evening"))); // we know its title from list onView(withId(R.id.user_name)).check(matches(withText(""))); onView(withId(R.id.description)).check(matches(withText(""))); onView(withId(R.id.date_text)).check(matches(withText(""))); TimingIdlingResource idlingResource = new TimingIdlingResource(3000); // wait dummy network operation Espresso.registerIdlingResources(idlingResource); onView(withId(R.id.title)).check(matches(withText("Sundown on the Oregon Coast"))); onView(withId(R.id.user_name)).check(matches(withText("Cole Chase Photography"))); onView(withId(R.id.description)).check(matches(startsWith("I was treated to this"))); onView(withId(R.id.date_text)).check(matches(withText("Posted on 2015-08-02 22:40:28"))); onView(withId(R.id.comment_parent)).check(matches(withChildOn(0, "C'est magnifique !"))); onView(withId(R.id.comment_parent)).check(matches(withChildOn(1, "A wonderful moment"))); onView(withId(R.id.comment_parent)).check(matches(withChildOn(2, "beautiful capture Cole"))); onView(withId(R.id.comment_parent)).check(matches(withChildOn(3, "See All 35 Comments"))); Espresso.unregisterIdlingResources(idlingResource); }
public void testButtonClick() { Espresso.onView(ViewMatchers.withId(R.id.btnTest)).perform(ViewActions.click()); }
@After public void unregisterIdlingResource() { Espresso.unregisterIdlingResources(idlingResource); }
@Before public void registerIdlingResource() { idlingResource = new OnMapReadyIdlingResource(rule.getActivity()); Espresso.registerIdlingResources(idlingResource); }
/** * Prepare your test fixture for this test. In this case we register an IdlingResources with * Espresso. IdlingResource resource is a great way to tell Espresso when your app is in an idle * state. This helps Espresso to synchronize your test actions, which makes tests significantly * more reliable. */ @Before public void registerIdlingResource() { Espresso.registerIdlingResources( mAddNoteIntentsTestRule.getActivity().getCountingIdlingResource()); }