1. Introduction

It is well known that there are two types of testing in Android.

  1. Instrumented testing, is the use of the Android framework for testing. In the projectandroidTestFolder.
  2. Unit testing, not using the Android framework, tests the code logic. In the projecttestFolder.

Espresso is a framework that is often used for instrumental testing on the Android platform.

Espresso is mainly composed of the following three parts:

  1. ViewMatcher: matches the specified View in the current View hierarchy.
  2. ViewActions: Performs some behavior of the View, for exampleonClickEvents.
  3. ViewAssertions: Checks some state of the View, such as whether it is displayed or not.

2. Use

2.1 Adding a Dependency

Add dependencies to build.gradle in AppModule:

    androidTestImplementation 'androidx. Test. Espresso: espresso - core: 3.2.0'
    androidTestImplementation 'androidx. Test: runner: 1.2.0'
    androidTestImplementation 'androidx. Test: rules: 1.2.0'
    androidTestImplementation 'androidx. Test: core: 1.2.0'
Copy the code

2.2 add testRunner

Add the following configuration to build.gradle android.defaultConfig in AppModle (this setting is added by default when creating projects) :

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Copy the code

2.3 Creating a Test Class

Create the test class in the androidTest folder, and add androidTest at the end of the name as recommended. Since you need AndroidJunit4, add @runwith (AndroidUnit4.class) at the top of the class. You also need to create the TestRule variable as follows. You also need to add @test at the top of each Test method.

@RunWith(AndroidJUnit4.class)
public class MainActivityUITest {

    @Rule
    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void checkMainActivity(a) {}}Copy the code

2.4 Basic usage of Espresso

Against 2.4.1 access to View

The matching method for getting a View is espress.onView. To find the specified View, use the withId and withText methods of the ViewMatcher.

  1. Get by ID:

The withId method is introduced:

import static androidx.test.espresso.matcher.ViewMatchers.withId;
Copy the code

Access to the View:

Espresso.onView(withId(R.id.editView))
Copy the code
  1. Get from the contents of text:

The withText method is introduced:

import static androidx.test.espresso.matcher.ViewMatchers.withText;
Copy the code

Access to the View:

Espresso.onView(withText("hello"))
Copy the code
2.4.2 Executing events

Methods in ViewActions are used to perform certain events, such as click events, etc. First, don’t forget to introduce methods in ViewActions.

import static androidx.test.espresso.action.ViewActions.*;
Copy the code

The following are commonly used.

  1. click(): click on the
  2. closeSoftKeyboard(): Put away the keyboard
  3. typeText(String stringToBeTyped): Enter text
  4. swipeLeft()Slide left:
  5. swipeRight(): the slider to the right
  6. swipeDown()To decline:
  7. swipeUp()Upward slide:
  8. doubleClick(): double-click
  9. longClick(): long press
  10. pressKey(int keyCode): Presses the specified key

The overall code is as follows:

Espresso.onView(withId(R.id.editText)).perform(typeText("hello"),closeSoftKeyboard());
Espresso.onView(withId(R.id.btnSend)).perform(click());
Copy the code
2.4.3 check

After we do a bunch of things with the View, we need to decide if it meets our expectations. And of course that’s the key to instrumentation. We need to check with the ViewAssertions method. Start with the method of ViewAssertions.

import static androidx.test.espresso.assertion.ViewAssertions.*;
Copy the code

View is checked using the check method, where ViewAssertions are passed in the Maches method. It’s a little convoluted, but when you look at the code, it makes sense.

Espresso.onView(withId(R.id.textViewInSecond)).check(matches(withText("hello")));
Copy the code
2.4.4 Test of RecyclerView

If you have RecyclerView in your layout, it becomes a little more complicated to test the content inside. First, an external method is introduced:

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
Copy the code

The test method is as follows:

// Get option data from spinner
String[] arrays = rule.getActivity().getResources().getStringArray(R.array.spinner);
for (int i = 0; i < arrays.length; i++) {
    Espresso.onView(withId(R.id.spinner)).perform(click());
    Espresso.onData(is(arrays[i])).perform(click());
        Espresso.onView(withId(R.id.textViewInSecondTwo)).check(matches(withText(containsString(arrays[i]))));
        }
Copy the code

Esspresso. OnData (Matcher
dataMatcher (extends Object> dataMatcher) extends Object> dataMatcher (extends Object> dataMatcher) extends Object> dataMatcher (extends Object> dataMatcher)

onViewandonDataThe difference between

OnView: The element to be tested must be in the viewable, such as RecyclerView 11 is not visible, then can not select the 11 element to test, otherwise it will report an error.

OnData: You can specify an off-screen element. If an off-screen element is selected, the frame will move to the specified element and then perform the specified action.

3. Record Espresso Test

In addition to writing the Espresso test code manually, you can also have the framework generate the test code automatically through the UI.

ViewAssertions

4. Pay attention to the point

One caveat here is that system animation can have an impact on UI testing. So when testing the need to zoom in the window animation, excessive animation zoom, animation program time zoom off. The diagram below.

Making: github.com/HyejeanMOON…