下面列出了android.support.test.espresso.action.ViewActions#android.support.test.espresso.Espresso 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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()));
}
@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));
}
@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"))));
}
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()));
}
@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());
}
@After
public void unregisterIntentServiceIdlingResource() {
if (mPagerIdle != null)
Espresso.unregisterIdlingResources(mPagerIdle);
if (mSimpleIdle != null)
Espresso.unregisterIdlingResources(mPagerIdle);
}
@Before
public void registerIdlingResource() {
List<View> viewList = Arrays.asList(mActivityRule.getActivity()
.findViewById(R.id.textView));
mIdlingResource = new ViewIdlingResource(viewList);
Espresso.registerIdlingResources(mIdlingResource);
}
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()));
}
@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);
}
@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();
}
@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);
}
@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))));
}
/**
* 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);
}