Espresso is the framework for Google's official Android UI Automation test.
Why is it called espresso (espresso), it seems that the Android farmers can easily finish the automated use case to enjoy the coffee, watching the automated test "fly."
This is the official address of the espresso. In addition, we recommend that you watch this YouTube on the espresso environment set up a short video.
Espresso Environment Construction
The following will be based on Android Studio, which describes how to build espresso.
1. Create a new project in Android studio;
2. Modify the App/build.gradle script in project;
Main Changes 3:
1) Increase within defaultconfig,testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner", Used to run scripts;
2) Increase packagingoptions, avoid the liscens conflict when compiling;
3) Add espresso-related references in dependencies;
Apply plugin: ' Com.android.application 'Android {compilesdkversion 22Buildtoolsversion "22.0.1"defaultconfig {ApplicationID "com.example.yezhenrong.myapplication"Minsdkversion 15Targetsdkversion 22Versioncode 1Versionname "1.0"Testinstrumentationrunner "Android.support.test.runner.AndroidJUnitRunner"} buildtypes {release {minifyenabled false proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' }} packagingoptions {exclude ' LICENSE.txt ' }}dependencies {compile filetree (dir: ' Libs ', in Clude: [' *.jar ']) compile ' com.android.support:appcompat-v7:22.1.1 '
//Espresso related references compile ' com.android.support:support-annotations:22.1.1 ' androidtestcompile ' com.android.support:support-annotations:22.1.1 ' androidtestcompile (' Com.android.support.test.espresso: espresso-core:2.1 ') {Exclude group: ' Javax.inject '//support 2.3 } androidtestcompile ' com.android.support.test.espresso:espresso-intents:2.1 ' androidtestcompile ' com.android.support.test.espresso:espresso-contrib:2.1 ' androidtestcompile ' Com.android.support.test:runner : 0.2 '}
3. Add the espresso Testrunner.
1) Click on the top bar menu Run->edit configurations;
2) When the following window appears, click "+" in the upper left corner and select "Android Tests";
3) Modify the name of the new configuration, select the app Module, enter runner, and select "Show chooer dialog".
Click "OK" to finish!
4. Create a new test case class.
To create a new class under App/androidtest/java, such as Mainactivitytest.class, this is the class of our specific test case.
5. Start writing test cases.
1) First create a @rule,activitytestrule to indicate the activity being tested;
2) The method of test case is @test annotation annotation, the method name can be arbitrary.
You can also write the setup () and teardown () methods.
Yes, Espresso's testing framework is based on JUnit.
3) test method Testtextviewdisplay () Inside:
... import static android.support.test.espresso.espresso.onview;import static Android.support.test.espresso.matcher.viewmatchers.isdisplayed;import Static Android.support.test.espresso.matcher.ViewMatchers.withText; @RunWith (Androidjunit4.class) public class Mainactivitytest {@Rule public activitytestrule Mactivityrule = New Activitytestrule (Mainactivity.class); @Test public void Testtextviewdisplay () {Onview (Withtext ("Hello world! " ) . Check (Viewassertions.matches (isdisplayed ())); }}
Onview (Withtext ("Hello world!")). Check (Viewassertions.matches (isdisplayed ()));
This line of code is as the name implies, guess meaning is based on "Hello world!" Locate the specified view, and then check whether the view is isdisplayded (visible).
Espresso provides a rich API for UI Automation, and here's a simple hierarchical relationship of their class.
Espress recommend that you use static import to simplify your code when writing test cases. In Android Studio, the shortcut key for static import is when you enter "Onview" and then press ALT + on the keyboard Enter will appear a menu select static import that's done! such as
6. Run the test case to see the results of the run.
Android Espresso (UI Automation test) build