类android.support.test.espresso.Espresso源码实例Demo

下面列出了怎么用android.support.test.espresso.Espresso的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: android-sdk   文件: MainActivityEspressoTest.java
@Override
protected void before() throws Throwable {
    super.before();
    countingIdlingResource = new CountingIdlingResource("optly", true);
    CountingIdlingResourceInterface wrapper = new CountingIdlingResourceInterface() {
        @Override
        public void increment() {
            countingIdlingResource.increment();
        }

        @Override
        public void decrement() {
            countingIdlingResource.decrement();
        }
    };
    CountingIdlingResourceManager.setIdlingResource(wrapper);
    // To prove that the test fails, omit this call:
    Espresso.registerIdlingResources(countingIdlingResource);
}
 
public void testStartPrimaryTypeDisplayWithExtras() throws Exception {
    final String integerExtra = "1";
    final String longExtra = "2";
    final String floatExtra = "3.4";
    final String doubleExtra = "5.6";
    final String stringExtra = "String value";
    final String charSequenceExtra = "CharSequence value";

    Espresso.onView(ViewMatchers.withText(integerExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(longExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(floatExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(doubleExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText("true")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(stringExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(charSequenceExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
 
private static ViewInteraction[] getButtonInteractions() {
    ViewInteraction[] buttonsInteractions = new ViewInteraction[10];
    // We cannot rely on the withDigit() matcher to retrieve these because,
    // after performing a click on a button, the time display will update to
    // take on that button's digit text, and so withDigit() will return a matcher
    // that matches multiple views with that digit text: the button
    // itself and the time display. This will prevent us from performing
    // validation on the same ViewInteractions later.
    buttonsInteractions[0] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text10));
    buttonsInteractions[1] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text0));
    buttonsInteractions[2] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text1));
    buttonsInteractions[3] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text2));
    buttonsInteractions[4] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text3));
    buttonsInteractions[5] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text4));
    buttonsInteractions[6] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text5));
    buttonsInteractions[7] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text6));
    buttonsInteractions[8] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text7));
    buttonsInteractions[9] = Espresso.onView(ViewMatchers.withId(R.id.nptp_text8));
    return buttonsInteractions;
}
 
/**
 * Checks if all the views are visible on the login activity
 */
@Test
public void checkAllViewAreVisible() throws Exception {
    mLoginActivityActivityTestRule.launchActivity(null);
    onView(withId(R.id.logo)).check(matches(isDisplayed()));
    onView(withId(R.id.tvAppName)).check(matches(withText(R.string.app_name)));
    onView(withId(R.id.loginlayout)).check(matches(isDisplayed()));
    onView(withId(R.id.myExperimentIcon)).check(matches(isDisplayed()));
    onView(withId(R.id.input_layout_email)).check(matches(isDisplayed()));
    onView(withId(R.id.input_layout_password)).check(matches(isDisplayed()));
    onView(withId(R.id.etEmail)).check(matches(isDisplayed()));
    onView(withId(R.id.etPassword)).check(matches(isDisplayed()));
    Espresso.closeSoftKeyboard();
    onView(withId(R.id.bLogin)).check(matches(isDisplayed()));
    onView(withId(R.id.bRegister)).check(matches(isDisplayed()));
}
 
@Test
public void verifyProgressCycleWithCompleteStateText() throws InterruptedException {
    Espresso.onView(withId(R.id.button)).perform(scrollTo());
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton3), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.SUBMIT_TO_PROGRESS_MORPH_DURATION);
    Utils.doProgress(doProgressText);
    Utils.doProgress(doProgressText);
    Utils.doProgress(doProgressText);
    Utils.doProgress(doProgressText);
    Thread.sleep(Constants.MORPH_DURATION);
    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton3))
            .check(matches(allOf(withText(completeStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_accept)))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton3);
}
 
源代码6 项目: friendly-plans   文件: TaskCreateActivityTest.java
@Test
public void whenChangedViewChangeNavigationMenuHighlight() {
    onView(withId(R.id.id_et_task_name))
            .perform(replaceText(EXPECTED_NAME));
    closeSoftKeyboard();

    onView(withId(R.id.id_btn_steps))
            .perform(click());
    onView(withId(R.id.id_navigation_steps))
            .check(matches(isEnabled()));
    onView(withId(R.id.id_navigation_basic_info))
            .check(matches(not(isEnabled())));

    Espresso.pressBack();
    onView(withId(R.id.id_navigation_basic_info))
            .check(matches(isEnabled()));
    onView(withId(R.id.id_navigation_steps))
            .check(matches(not(isEnabled())));

    closeSoftKeyboard();

    List<TaskTemplate> taskTemplates = taskTemplateRepository.get(EXPECTED_NAME);
    idsToDelete.add(taskTemplates.get(0).getId());
}
 
public void testStartPrimaryTypeDisplayWithExtras() throws Exception {
    final String integerExtra = "1";
    final String longExtra = "2";
    final String floatExtra = "3.4";
    final String doubleExtra = "5.6";
    final String stringExtra = "String value";
    final String charSequenceExtra = "CharSequence value";

    Espresso.onView(ViewMatchers.withText(integerExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(longExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(floatExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(doubleExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText("true")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(stringExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(charSequenceExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
 
源代码8 项目: android-sdk   文件: CameraHelperTest.java
@Test
public void testCameraFlow() {
  Bitmap icon = BitmapFactory.decodeResource(
      InstrumentationRegistry.getTargetContext().getResources(),
      R.mipmap.ic_launcher);

  Intent resultData = new Intent();
  resultData.putExtra("data", icon);
  Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);

  intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);

  Espresso.onView(withId(R.id.camera_button)).perform(click());

  intended(hasAction(MediaStore.ACTION_IMAGE_CAPTURE));
}
 
源代码9 项目: Awesome-WanAndroid   文件: TestActivityTest.java
@Test
public void ViewMatchers() {
    Espresso.onView(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button));
    //onView内部最好不要使用withText()断言处理
    Espresso.onView(Matchers.allOf(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button), ViewMatchers.withText("HaHa")));
    Espresso.onView(Matchers.allOf(ViewMatchers.withId(json.chao.com.wanandroid.R.id.button), Matchers.not(ViewMatchers.withText("HaHa"))));
}
 
源代码10 项目: PrettyBundle   文件: InjectStringExtrasTest.java
public void testStartActivityTestStringExtra2WithExtras() throws Exception {
    final String extra1 = "Giang";
    final String extra2 = "Nguyen";
    Espresso.onView(ViewMatchers.withHint(R.string.string_extra_1)).perform(ViewActions.typeText(extra1), ViewActions.pressImeActionButton());
    Espresso.onView(ViewMatchers.withHint(R.string.string_extra_2)).perform(ViewActions.typeText(extra2), ViewActions.pressImeActionButton());

    Espresso.onView(ViewMatchers.withText(R.string.submit)).perform(ExtViewActions.waitForSoftKeyboard(), ViewActions.click());

    Espresso.onView(ViewMatchers.withText(extra1)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(extra2)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
 
源代码11 项目: AndroidSchool   文件: RepositoriesActivityTest.java
@After
public void tearDown() throws Exception {
    Intents.release();
    if (idlingResource != null) {
        Espresso.unregisterIdlingResources(idlingResource);
    }
    RepositoryProvider.provideKeyValueStorage().clear();
    Realm.getDefaultInstance().executeTransaction(realm -> realm.deleteAll());
}
 
@Override
public Statement apply(final Statement base, Description description) {
  return new Statement() {
    @Override
    public void evaluate() throws Throwable {
      IdlingResource idlingResource = OkHttp3IdlingResource.create(
          "okhttp", OkHttp.getInstance());
      Espresso.registerIdlingResources(idlingResource);

      base.evaluate();

      Espresso.unregisterIdlingResources(idlingResource);
    }
  };
}
 
@Override
public void onCreate(Bundle arguments) {
    super.onCreate(arguments);

    RxEspressoScheduleHandler rxEspressoScheduleHandler = new RxEspressoScheduleHandler();
    RxJavaPlugins.setScheduleHandler(rxEspressoScheduleHandler);
    Espresso.registerIdlingResources(rxEspressoScheduleHandler.getIdlingResource());
}
 
源代码14 项目: Equate   文件: TestEspressoUnitTypeVisibility.java
@After
public void unregisterIntentServiceIdlingResource() {
	if (mPagerIdle != null)
		Espresso.unregisterIdlingResources(mPagerIdle);
	if (mSimpleIdle != null)
		Espresso.unregisterIdlingResources(mPagerIdle);
}
 
源代码15 项目: NYBus   文件: HugeEventActivityTest.java
@Before
public void registerIdlingResource() {
    List<View> viewList = Arrays.asList(mActivityRule.getActivity()
            .findViewById(R.id.textView));

    mIdlingResource = new ViewIdlingResource(viewList);

    Espresso.registerIdlingResources(mIdlingResource);
}
 
源代码16 项目: PrettyBundle   文件: InjectPrimaryTypeExtrasTest.java
public void testStartPrimaryTypeDisplayWithExtras() throws Exception {
    getInstrumentation().runOnMainSync(new Runnable() {
        @Override public void run() {
            ((CheckBox) activity.findViewById(R.id.cbBoolean)).setChecked(true);
        }
    });
    final String integerExtra = "1";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_int)).perform(ViewActions.typeText(integerExtra), ViewActions.pressImeActionButton());
    final String longExtra = "2";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_long)).perform(ViewActions.typeText(longExtra), ViewActions.pressImeActionButton());
    final String floatExtra = "3.4";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_float)).perform(ViewActions.typeText(floatExtra), ViewActions.pressImeActionButton());
    final String doubleExtra = "5.6";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_double)).perform(ViewActions.typeText(doubleExtra), ViewActions.pressImeActionButton());
    final String stringExtra = "String value";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_string)).perform(ViewActions.typeText(stringExtra), ViewActions.pressImeActionButton());
    final String charSequenceExtra = "CharSequence value";
    Espresso.onView(ViewMatchers.withHint(R.string.hint_char_sequence)).perform(ViewActions.typeText(charSequenceExtra), ViewActions.pressImeActionButton());

    Espresso.closeSoftKeyboard();

    Espresso.onView(ViewMatchers.withText(R.string.submit)).perform(ExtViewActions.waitForSoftKeyboard(), ViewActions.scrollTo(), ViewActions.click());

    Espresso.onView(ViewMatchers.withText(integerExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(longExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(floatExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(doubleExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText("true")).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(stringExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
    Espresso.onView(ViewMatchers.withText(charSequenceExtra)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
}
 
源代码17 项目: Falcon   文件: FalconDialogInOnCreateTest.java
@Test
public void takesDialogOnCreate() {
  DialogOnCreate activity = _activityRule.getActivity();
  onView(withText(DialogOnCreate.DIALOG_TITLE)).check(matches(isDisplayed()));

  Bitmap withDialog = Falcon.takeScreenshotBitmap(activity);
  Espresso.pressBack();
  onView(withText(DialogOnCreate.DIALOG_TITLE)).check(doesNotExist());

  Bitmap afterDialogDismiss = Falcon.takeScreenshotBitmap(activity);

  assertThatBitmap(withDialog).isDarkerThan(afterDialogDismiss);
}
 
源代码18 项目: flowless   文件: LoginInstrumentedTest.java
@Before
public void setup() {
    Espresso.registerIdlingResources(EspressoIdlingResource.getIdlingResource());
    mainActivityActivityTestRule.launchActivity(null);
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    MainActivity mainActivity = mainActivityActivityTestRule.getActivity();
    instrumentation.runOnMainSync(() -> {
        Flow.get(mainActivity.getBaseContext()).setHistory(History.single(LoginKey.create()), Direction.REPLACE);
    });
    ServiceProvider serviceProvider = ServiceProvider.get(mainActivity.getBaseContext());
    LoginComponent loginComponent = serviceProvider.getService(LoginKey.create(), DaggerService.TAG);
    loginPresenter = loginComponent.loginPresenter();
}
 
源代码19 项目: flowless   文件: RepositoriesInstrumentedTest.java
@Before
public void setup() {
    Espresso.registerIdlingResources(EspressoIdlingResource.getIdlingResource());
    mainPage = new MainPage();
    repositoriesPage = new RepositoriesPage();
    mainActivityActivityTestRule.launchActivity(null);
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    MainActivity mainActivity = mainActivityActivityTestRule.getActivity();
    instrumentation.runOnMainSync(() -> {
        Flow.get(mainActivity.getBaseContext()).setHistory(History.single(RepositoriesKey.create()), Direction.REPLACE);
    });
}
 
@Test
public void verifyProgressCycleWithCancelStateText() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton1), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.INDETERMINATE_PROGRESS_DURATION);
    Espresso.onView(withId(R.id.circularButton1)).perform(click());
    Thread.sleep(Constants.MORPH_DURATION);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton1))
            .check(matches(allOf(withText(cancelStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_cross)))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton1);
}
 
源代码21 项目: AndroidSchool   文件: AuthActivityTest.java
@Test
public void testErrorAuth() throws Exception {
    RepositoryProvider.provideKeyValueStorage().saveToken(ERROR);

    onView(withId(R.id.loginEdit)).perform(typeText("login"));
    closeSoftKeyboard();
    onView(withId(R.id.passwordEdit)).perform(typeText("pass"));
    closeSoftKeyboard();
    onView(withId(R.id.logInButton)).perform(click());

    IdlingResource idlingResource = TimeIdlingResource.timeout(4000);
    onView(withId(R.id.loginInputLayout)).check(matches(withInputError(R.string.error)));
    Espresso.unregisterIdlingResources(idlingResource);
}
 
private void checkIndeterminateProgressCancelCompleteErrorTestWithText() {
    Espresso.onView(withId(R.id.circularButton1))
            .check(matches(allOf(withText(cancelStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_cross)))));
    Espresso.onView(withId(R.id.circularButton4))
            .check(matches(allOf(withText(completeStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_accept)))));
    Espresso.onView(withId(R.id.circularButton5))
            .check(matches(allOf(withText(errorStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_cancel)))));
}
 
private void checkIndeterminateProgressIdleTestWithText() {
    Espresso.onView(withId(R.id.circularButton1))
            .check(matches(allOf(withText(idleStateText))));
    Espresso.onView(withId(R.id.circularButton4))
            .check(matches(allOf(withText(idleStateText))));
    Espresso.onView(withId(R.id.circularButton5))
            .check(matches(allOf(withText(idleStateText))));
}
 
源代码24 项目: CleanGUITestArchitecture   文件: StepDefinitions.java
/**
 * All the clean up of application's data and state after each scenario must happen here
 * The last call of this method should always be the call to parent's tear down method
 */
@After
public void tearDown() throws Exception {
    LoginActivity.setIdlingNotificationListener(null);
    Espresso.unregisterIdlingResources(mCountingIdlingResourceListener.getCountingIdlingResource());
    ActivityFinisher.finishOpenActivities(); // Required for testing App with multiple activities
    letScreenOfTestDeviceTurnOff();
}
 
@Test
public void verifyCompleteProgressCycleWithCompleteStateText() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton4), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.INDETERMINATE_PROGRESS_DURATION);
    Espresso.onView(withId(R.id.circularButton4)).perform(click());
    Thread.sleep(Constants.MORPH_DURATION);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton4))
            .check(matches(allOf(withText(completeStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_accept)))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton4);
}
 
@Test
public void verifyCompleteProgressCycleWithErrorStateIcon() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton5), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.INDETERMINATE_PROGRESS_DURATION);
    Espresso.onView(withId(R.id.circularButton5)).perform(click());
    Thread.sleep(Constants.MORPH_DURATION);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton5))
            .check(matches(allOf(withText(emptyString), Utils.withCompoundDrawable(R.drawable.ic_action_cancel))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton5);
}
 
@Test
public void verifyCompleteProgressCycleWithErrorStateText() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton5), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.INDETERMINATE_PROGRESS_DURATION);
    Espresso.onView(withId(R.id.circularButton5)).perform(click());
    Thread.sleep(Constants.MORPH_DURATION);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton5))
            .check(matches(allOf(withText(errorStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_cancel)))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton5);
}
 
@Test
public void verifyProgressCycleWithCompleteStateIcon() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton2), withText(idleStateText)))
            .perform(click());
    Thread.sleep(MainActivity.sweepDuration + MainActivity.sweepDuration/3);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton2))
            .check(matches(allOf(withText(emptyString), Utils.withCompoundDrawable(R.drawable.ic_action_accept))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton2);
}
 
@Test
public void verifyProgressCycleWithCompleteStateText() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton2), withText(idleStateText)))
            .perform(click());
    Thread.sleep(MainActivity.sweepDuration + MainActivity.sweepDuration/3);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton2))
            .check(matches(allOf(withText(completeStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_accept)))));

    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton2);
}
 
@Test
public void verifyProgressCycleWithCancelStateText() throws InterruptedException {
    // Click submit button.
    Espresso.onView(allOf(withId(R.id.circularButton2), withText(idleStateText)))
            .perform(click());
    Thread.sleep(Constants.SUBMIT_TO_PROGRESS_MORPH_DURATION);
    Espresso.onView(withId(R.id.circularButton2)).perform(click());
    Thread.sleep(Constants.MORPH_DURATION);

    // Check the text and compound drawable.
    Espresso.onView(withId(R.id.circularButton2))
            .check(matches(allOf(withText(completeStateText), not(Utils.withCompoundDrawable(R.drawable.ic_action_cross)))));
    Utils.clickAndShowIdleState(idleStateText, R.id.circularButton2);
}
 
 类所在包
 同包方法