# Espresso Dependency ###### tags: `Android Dependency` [TOC] # What is it for? It is used to do **unit testing on UI Views** (like [UI Automator](https://developer.android.com/training/testing/ui-automator)). It enable us to: * Look for some views * Control views * Compare views # Dependency Actually it should be there when you create a project, unless you deleted it. ## build.gradle(:app) dependency and setting **build.gradle (:app)** ```java= //Espresso testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test:runner:1.4.0' androidTestImplementation 'androidx.test:rules:1.4.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' ``` **testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"** ```java= defaultConfig { applicationId "com.zc.androiddependencies" minSdkVersion 16 targetSdkVersion 30 versionCode 1 versionName "1.0" //Multidex Enable here only works for >= 21 sdks multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } ``` >**Note:** >JUnit is a testing framework for Java. [color=pink] ## What is androidTestImplementation? >Remember the folders below /src? >We have **/main, /test and /androidTest** >androidTestImplementation just deals with /androidTest dependency settings.[color=orange] # How to use Espresso? ## Espresso Tools 1. **ViewMatcher: Find Views** Ex: withId、withText 1. **ViewAction: Control views' actions** Ex: click、typeText 1. **ViewAssertion: Compares View** Ex: matches ## Steps towards Espresso UI Test base file ### 1. [Add dependencies in build.gradle](#Dependency) ### 2. New a Test class in androidTest Folder >**Note:** The class **MUST BE ++PUBLIC++** or else you will get Class not found/ class is not a test/ etc.[color=red] **Example:** ![](https://i.imgur.com/rf93Hhc.png) ### 3. Write your test ## How to write a Test? ### Introduction Things to take into account: * **Annotations** * @Rule: For ActivityScenarioRule. * @Before: Actions before test. * @Test: The test method itself. * **ActivityScenarioRule:** Is used to find the activity in which you want to proceed the test on. ### Let's check an example directly! **Example** <details> <summary>Dependencies</summary> import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import com.zc.androiddependencies.espresso.EspressoActivity; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.clearText; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.typeText; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; import static androidx.test.espresso.matcher.ViewMatchers.isNotEnabled; import static androidx.test.espresso.matcher.ViewMatchers.withId; </details> ```java= package com.zc.androiddependencies; @RunWith(AndroidJUnit4.class) @LargeTest public class EspressoActivityTest { public String input; //Tells which activity you are performing the test on. @Rule public ActivityScenarioRule<EspressoActivity> activityRule = new ActivityScenarioRule<>(EspressoActivity.class); //Actions before test @Before public void stringToBeTyped(){ input = "Espresso Test"; } @Test public void checkSubmitState(){ //Check Submit Button is not enabled onView(withId(R.id.btn_submit)) .check(matches(isNotEnabled())); //Test type into EditText onView(withId(R.id.et_account)) .perform(typeText(input)); //Check Submit Button is enabled onView(withId(R.id.btn_submit)) .check(matches(isEnabled())); //Check Submit Button is clickable onView(withId(R.id.btn_submit)) .perform(click()); //Clear Text onView(withId(R.id.et_account)) .perform(clearText()); //Check Submit Button is not enabled onView(withId(R.id.btn_submit)) .check(matches(isNotEnabled())); } //ViewAssertions: matches //ViewMatchers: isNotEnabled //ViewActions: clearText/click/typeText //ViewInteraction: check/perform } ``` **Activity** ```java= public class EspressoActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_espresso); EditText etAccount = findViewById(R.id.et_account); Button btnSubmit = findViewById(R.id.btn_submit); btnSubmit.setEnabled(false); etAccount.addTextChangedListener(new SimplifyTextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { btnSubmit.setEnabled(s != null && s.length() > 0); } }); } } ``` **Note:** **ActivityTestRule** is deprecated so better use **ActivityScenarioRule**. ## How to run test? ### Run by clicking run >![](https://i.imgur.com/fUiuwAu.png)[color=lightblue] ### Add Test in Edit Configuration >![](https://i.imgur.com/152jLSz.png)[color=lightblue] **Note:** If you cannot run test, you may as well delete all tests and new them again. # Reference [Espresso Article in CH](https://ithelp.ithome.com.tw/articles/10227511) [Android Studio Espresso](https://developer.android.com/training/testing/ui-testing/espresso-testing)