示例#1
0
  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;
              }
            });
  }
示例#2
0
 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() + "");
 }
 public void testButtonClick() {
   Espresso.onView(ViewMatchers.withId(R.id.btnTest)).perform(ViewActions.click());
 }