On Github georgelin422 / Introduction-to-Espresso
Created by George Lin / @georgelin422
dependencies { androidTestCompile 'com.android.support.test:runner:0.4' // Set this dependency to use JUnit 4 rules androidTestCompile 'com.android.support.test:rules:0.4' // Set this dependency to build and run Espresso tests androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' }
And DONE!
onView(ViewMatcher) .perform(ViewAction) .check(ViewAssertion)
onView(withId(android.R.id.button1)) .perform(ViewAction) .check(ViewAssertion)
onView(withId(android.R.id.button1)) .perform(click()) .check(ViewAssertion)
onView(withId(android.R.id.button1)) .perform(click()) .check(matches(withText("BAR")))
Test case: if the Toolbar title is "FOO"
The title is a TextView with a parent of class Toolbar
onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))) .check(matches(withText("FOO")))http://www.marcphilipp.de/downloads/posts/2013-01-02-hamcrest-quick-reference/Hamcrest-1.3.pdf
testCompile 'org.hamcrest:hamcrest-library:1.3'
onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))) .check(matches(withText("FOO")))
GG in this case
<Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> </Toolbar>
Test case: if the Toolbar title is "FOO"
Veryfy the Toolbar itself, not verify descendant TextView
onView(isAssignableFrom(Toolbar.class)) .check(ViewAssertion)
onView(isAssignableFrom(Toolbar.class)) .check(Assert this Toolbar has its title "FOO")
onView(isAssignableFrom(Toolbar.class)) .check(matches(withToolbarTitle("FOO")))
onView(isAssignableFrom(Toolbar.class)) .check(matches(withToolbarTitle("FOO")))
public static Matcher<View> withToolbarTitle(CharSequence title) { return withToolbarTitle(is(title)); // is(T) from Hamcrest } public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) { checkNotNull(textMatcher); return new BoundedMatcher<View, Toolbar>(Toolbar.class) { @Override public void describeTo(Description description) { description.appendText("with title: "); textMatcher.describeTo(description); } @Override public boolean matchesSafely(Toolbar toolbar) { return textMatcher.matches(toolbar.getTitle()); } }; }
Using onData
Why not use onView?
onData(ObjectMatcher) .DataOptions // reference DataInteraction.java .perform(ViewAction) .check(ViewAssertion)
Click item with String "Americano" in a Spinner
onData(allOf(is(instanceOf(String.class)), is("Americano"))) .inAdapterView(R.id.spinner) .perform(click());
RecyclerView is NOT an AdapterView
RecyclerView is a ViewGroup
So, use onView
Click item at position 27
onView(withId(R.id.recycler_view)) .perform(RecyclerViewActions.actionOnItemAtPosition(27, click()));
in build.gradle
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
More in espresso-contrib
When we talk about Espresso's Synchronization, it means...
When doing tests...
In some cases, we need to define out own Idling Resource
Implement IdlingResource and override three methods
@Override public String getName() { return IntentServiceIdlingResource.class.getName(); }
@Override public void registerIdleTransitionCallback( ResourceCallback resourceCallback) { mResourceCallback = resourceCallback; }
@Override public boolean isIdleNow() { boolean idle = !isIntentServiceRunning(); if (idle && resourceCallback != null) { mResourceCallback.onTransitionToIdle(); } return idle; }
Register your IdlingResource in test
@Before public void registerIntentServiceIdlingResource() { idlingResource = new IntentServiceIdlingResource( InstrumentationRegistry.getTargetContext()); Espresso.registerIdlingResources(idlingResource); } @After public void unregisterIntentServiceIdlingResource() { Espresso.unregisterIdlingResources(idlingResource); }
Make sure your device is connected with usb
Turn off animations from Settings by opening Developing Options and turning all the following options off: