Espresso framework usage tutorial

Espresso framework

1. Preparation

1.1 Dependency configuration

1.2 Eliminate interference

2. Espresso architecture

2.1 Positioning View (onView / onData)

2.2 [Operation View (perform)] (#Operation View)

2.3 [Assert View (check)] (# Assert View)

Preparation

Dependency configuration

1. Add dependencies

androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1')
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation files('libs/mockito-core-2.21.0.jar')
testImplementation 'junit:junit:4.12'

2. Configure the test runner

Configure testIntrumentRunner in the defaultConfig area of ​​the build.gradle file under the app module

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Eliminate interference

Under developer options, turn off some configurations that may affect the transition animation zoom window animation zoom animation program duration zoom

Espresso architecture

 Espresso has three important classes, namely Matchers, ViewAction, and ViewAssertions, where Matchers often need to find UI components or filter UI through matching conditions, and ViewAction is to simulate users The behavior of the operation interface, ViewAssertions transforms and validates the view that simulates the behavior operation

1. Positioning View (onView / onData)

The class of positioning elements is com.android.support.test.espresso.Espresso. There are two methods onView () and onData () under this class to locate the name of the ordinary View method: com.android.support.test.espresso.Espresso.onVew ( ) Method declaration public static ViewInteraction onView (final Matcher viewMatcher) Enter the ViewMatchers to describe the View tool class you are looking for android.support.test.espresso.matcher.ViewMatchers There are a variety of static factory methods in the ViewMatchers class used to generate ViewMatcher for positioning elements (see the general View positioning principle section). The value ViewInteraction represents an actionable view android.support.test.espresso.ViewInteraction positioning principle id ViewMatchers.withId () text ViewMatchers.withText () class type ViewMatchers.instanceOf () currently has focus ViewMatchers.hasFocus () parent layout. . .

Locate the View onData ()
method name of the adapter com.android.support.test.espresso.Espresso.onData ()
method declaration public static DataInteraction onData (Matcher <? Extends Object> dataMatcher)
positioning principle In a row in the
input parameter DataMatchers is used to express the ViewMatchers.instanceOf which is the View
     class of inherited Adapter to be found
     . . . 
The return value DataInteraction represents the data set that can be manipulated android.support.test.espresso.DataInteraction

Second, operate the View (perform)

Perform an operation on an element (perform) ViewInteraction / DataInteraction perform () can be chained to call viewInteraction.perform (xx1) .perform (xx2) ...; parameter android.support.test.espresso.action.ViewActions common actions under the ViewActions tool class click (): Click operation typeText (): Enter text clearText (): Clear text replaceText (): Replace text. . .

Three, assert View (check)

Three, judge the execution result check
check () method android.support.test.espresso.ViewInteraction.check ()
incoming parameters ViewAssertion ViewAssertions tool class

Four, thread synchronization

Fourth, thread synchronization
IddlingResoureces
https://developer.android.google.cn/reference/android/support/test/uiautomator/UiWatcher

References
https://www.jianshu.com/p/c5da84eae63b
https://www.jianshu.com/p/474f276986ac
https://blog.csdn.net/u013247461/article/details/81042763

Guess you like

Origin www.cnblogs.com/peter-dev/p/10688080.html