Tutorial by Examples

In the build.gradle file of your Android app module add next dependencies: dependencies { // Android JUnit Runner androidTestCompile 'com.android.support.test:runner:0.5' // JUnit4 Rules androidTestCompile 'com.android.support.test:rules:0.5' // Espresso core a...
Place next java class in src/androidTest/java and run it. public class UITest { @Test public void Simple_Test() { onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher .perform(click()) // click() is a ViewAction .check(matches(isDi...
public final class DrawerLayoutTest { @Test public void Open_Close_Drawer_Layout() { onView(withId(R.id.drawer_layout)).perform(actionOpenDrawer()); onView(withId(R.id.drawer_layout)).perform(actionCloseDrawer()); } public static ViewAction actionOpenDrawer() { return ne...
UI testing tools Two main tools that are nowadays mostly used for UI testing are Appium and Espresso. AppiumEspressoblackbox testwhite/gray box testingwhat you see is what you can testcan change inner workings of the app and prepare it for testing, e.g. save some data to database or sharedpreferen...
@Test public void testUpNavigation() { intending(hasComponent(ParentActivity.class.getName())).respondWith(new Instrumentation.ActivityResult(0, null)); onView(withContentDescription("Navigate up")).perform(click()); intended(hasComponent(ParentActivity.class.getName())...
It is possible to perform ViewActions on a view using the perform method. The ViewActions class provides helper methods for the most common actions, like: ViewActions.click() ViewActions.typeText() ViewActions.clearText() For example, to click on the view: onView(...).perform(click()); onVi...
With the ViewMatchers you can find view in the current view hierarchy. To find a view, use the onView() method with a view matcher which selects the correct view. The onView() methods return an object of type ViewInteraction. For example, finding a view by its R.id is as simple as: onView(withId(...
Espresso by default has many matchers that help you find views that you need to do some checks or interactions with them. Most important ones can be found in the following cheat sheet: https://google.github.io/android-testing-support-library/docs/espresso/cheatsheet/ Some examples of matchers are...
Setup Espresso : androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestCompile 'com.android.support.test:runner:0.5' ViewMatchers – A collection of objects that implement Matcher<? super View> interface. You can pass one or more of these to the onView met...
You can organize the execution of your instrumented unit tests defining a Suite. /** * Runs all unit tests. */ @RunWith(Suite.class) @Suite.SuiteClasses({MyTest1.class , MyTest2.class, MyTest3.class}) public class AndroidTestSuite {} Then in AndroidStudio you can run...

Page 1 of 1