<h3></h3>
<h3><span style="color: #000080;"><strong>Espresso Introduction </strong></span></h3>
<p><span style="color: #008000;"><strong>Espresso</strong></span> is an open source instrumentation Testing framework made available by Google for the ease of <span style="color: #008000;"><strong class="markup--strong markup--p-strong">UI Testing</strong></span>.</p>
<p>It is used to automate manual test case for android application.</p>
<p>Espresso automatically <strong><span style="color: #008000;">syncronizes</span></strong> your test actions with the ui of your application.The framework also ensures that your activity is started before the tests run.It also let the test wait until all observed background activities have finished.</p>
<p>The main components of Espresso include the following:</p>
<ul>
<li><span style="color: #0000ff;"><strong>Espresso</strong></span> – Entry point to interactions with views (via <strong><span style="color: #008000;">onView() and onData()</span></strong>). Also exposes APIs that are not necessarily tied to any view, such as <strong><span style="color: #008000;">pressBack()</span></strong>.</li>
<li><span style="color: #0000ff;"><strong>ViewMatchers</strong></span> –<span style="color: #008000;"><strong>(Match a view)</strong></span> Allows you to find view in the current view hierarchy.</li>
<li><span style="color: #0000ff;"><strong>ViewActions</strong></span> – <span style="color: #008000;"><strong>(Perform an action)</strong></span>Allows you to perform an action on a <code>View.</code></li>
<li><span style="color: #0000ff;"><strong>ViewAssertions</strong></span> – <span style="color: #008000;"><strong>(Assert and verify the result)</strong></span>Check the state of the <code>View</code> to see if it reflects the expected state or behavior defined by the assertion.</li>
</ul>
<p><strong><span style="color: #0000ff;">Example:</span></strong></p>
<pre class="prettyprint lang-java"><code><span class="pln">onView</span><span class="pun">(</span><span class="pln">withId</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">my_view</span><span class="pun">))</span><span class="pln"> </span><strong><span class="com" style="color: #0000ff;">// withId(R.id.my_view) is a ViewMatcher</span></strong><span class="pln">
 </span><span class="pun">.</span><span class="pln">perform</span><span class="pun">(</span><span class="pln">click</span><span class="pun">())</span><span class="pln"> </span><strong><span class="com" style="color: #0000ff;">// click() is a ViewAction</span></strong><span class="pln">
 </span><span class="pun">.</span><span class="pln">check</span><span class="pun">(</span><span class="pln">matches</span><span class="pun">(</span><span class="pln">isDisplayed</span><span class="pun">()));</span> <strong><span class="com" style="color: #0000ff;">// matches(isDisplayed()) is a ViewAssertion</span></strong></code></pre>
<h3 class="devsite-page-title"><strong><span style="color: #000080;">Espresso setup instructions</span></strong></h3>
<p>To use Espresso, you must already have the Android Support Repository installed with Android Studio. You may also need to configure Espresso in your project.</p>
<h6><strong><span style="color: #0000ff;">Check for the Android Support Repository</span></strong></h6>
<p><strong>1</strong>. Open the project in Android Studio, and choose <span style="color: #008000;"><strong>Tools >; Android >; SDK Manager</strong></span>.</p>
<p>The Android SDK <span style="color: #008000;"><strong>Default Preferences</strong></span> pane appears.</p>
<p><strong>2</strong>. Click the <span style="color: #008000;"><strong>SDK Tools</strong></span> tab and expand <span style="color: #008000;"><strong>Support Repository</strong></span>.</p>
<p><strong>3</strong>. Look for <span style="color: #008000;"><strong>Android Support Repository</strong></span> in the list.</p>
<p>If <span style="color: #008000;"><strong>Installed</strong></span> appears in the Status column then click <span style="color: #008000;"><strong>Cancel</strong></span>.</p>
<p>If <span style="color: #008000;"><strong>Not installed</strong></span> or <strong><span style="color: #008000;">Update Available</span> </strong>appears, click the checkbox next to <span style="color: #008000;"><strong>Android Support Repository</strong></span>. A download icon should appear next to the checkbox. Click <strong><span style="color: #008000;">OK</span>.</strong></p>
<p><strong>4</strong>. Click <span style="color: #008000;"><strong>OK</strong></span> again, and then <span style="color: #008000;"><strong>Finish</strong></span> when the support repository has been installed.</p>
<h6><strong><span style="color: #0000ff;">Configure Espresso for the project</span></strong></h6>
<p>When you start a project for the phone and tablet form factor using <span style="color: #008000;"><strong>API 15: Android 4.0.3 (Ice Cream Sandwich)</strong></span> as the minimum SDK, Android Studio automatically includes the dependencies you need to use Espresso.</p>
<p>If you have created your project in a previous version of Android Studio, you may have to add the <span style="color: #008000;"><strong>dependencies and instrumentation runner</strong></span> yourself. To add the dependencies yourself, follow these steps:</p>
<ol start="1" type="1">
<li>Open your project in Android Studio in which you want to configure Espresso .</li>
<li>Open the <span style="color: #008000;"><strong>build.gradle (Module: app)</strong></span> file.</li>
<li>Check if the following is included (along with other dependencies) in the <span style="color: #008000;"><strong>dependencies</strong></span> section:</li>
</ol>
<pre><code>dependencies {
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'com.android.support:appcompat-v7:27.1.1'
 implementation 'com.android.support.constraint:constraint-layout:1.1.3'
 <span style="color: #0000ff;"><strong> testImplementation 'junit:junit:4.12'</strong></span>
<span style="color: #0000ff;"><strong> androidTestImplementation 'com.android.support.test:runner:1.0.2'</strong></span>
<span style="color: #0000ff;"><strong> androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'</strong></span>
 
}</code></pre>
<p>If the file doesn&#8217;t include the above dependency statements, enter them into the <span style="color: #008000;"><strong>dependencies</strong></span> section.</p>
<ol start="4" type="1">
<li>Android Studio also adds the following instrumentation statement to the end of the <strong><span style="color: #008000;">defaultConfig</span></strong> section of a new project:</li>
</ol>
<pre><code>defaultConfig {
 applicationId "com.c1ctech.instrumentationtestdemo"
 minSdkVersion 21
 targetSdkVersion 27
 versionCode 1
 versionName "1.0"
 <strong><span style="color: #0000ff;">testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"</span></strong>
}</code></pre>
<p>If the file doesn&#8217;t include the above instrumentation statement, enter it at the end of the <span style="color: #008000;"><strong>defaultConfig</strong></span> section.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<ol start="5" type="1">
<li>If you modified the <span style="color: #008000;"><strong>build.gradle (Module: app)</strong></span> file, click the <span style="color: #0000ff;"><strong>Sync Now</strong></span> link in the notification about Gradle files in top right corner of the window.</li>
</ol>
<h6 id="example-gradle-build-file"><strong><span style="color: #0000ff;">Example Gradle build file</span></strong></h6>
<pre><code>apply plugin: 'com.android.application'

android {
 compileSdkVersion 27
 defaultConfig {
 applicationId "com.c1ctech.instrumentationtestdemo"
 minSdkVersion 15
 targetSdkVersion 27
 versionCode 1
 versionName "1.0"
 <strong><span style="color: #0000ff;"> testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"</span></strong>
 }
 buildTypes {
 release {
 minifyEnabled false
 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
 }
 }
}

dependencies {
 implementation fileTree(dir: 'libs', include: ['*.jar'])
 implementation 'com.android.support:appcompat-v7:27.1.1'
 implementation 'com.android.support.constraint:constraint-layout:1.1.3'
 <strong><span style="color: #0000ff;">testImplementation 'junit:junit:4.12'
 androidTestImplementation 'com.android.support.test:runner:1.0.2'
 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'</span></strong>
 
}</code></pre>
<h6><strong><span style="color: #0000ff;">Turn off animations on your test device</span></strong></h6>
<p>Android phones and tablets display animations when moving between apps and screens. The animations are attractive when using the device, but they slow down performance, and may also cause unexpected results or may lead your test to fail. So it&#8217;s a good idea to turn off animations on your virtual or physical devices used for testing. On your device, go to <span style="color: #008000;"><b>Settings >; Developer options>;<strong>Drawing </strong></b></span>and disable the following 3 settings:</p>
<ul>
<li>Window animation scale</li>
<li>Transition animation scale</li>
<li>Animator duration scale</li>
</ul>
<h3 id="add-first-test"><strong><span style="color: #000080;">Add the first test</span></strong></h3>
<ol>
<li>In android studio it is important to write instrumented functional test cases in <strong><span style="color: #008000;">src/androdTest/java</span></strong> folder and unit tests in <span style="color: #008000;"><strong>src/Test/java</strong></span>.</p>
</li>
<li>
<p>Open your project then go to <strong><span style="color: #0000ff;">src/androidTest/java/com.c1ctech.instrumentationtestdemo(package name)</span></strong>->;<strong><span style="color: #0000ff;">New</span></strong>->;<strong><span style="color: #0000ff;">Java</span> <span style="color: #0000ff;">Class</span></strong><b> </b>give Class Name <span style="color: #008000;"><strong>TestClass</strong></span> and click on <b><span style="color: #008000;">OK</span>.</b></p>
</li>
</ol>
<p>After creating the <span style="color: #008000;"><strong>TestClass</strong></span> you are ready to start writing your test cases.</p>
<div class="devsite-code-button-wrapper">
<ul>
<li class="devsite-code-button gc-analytics-event material-icons devsite-click-to-copy-button" data-category="Site-Wide Custom Events" data-label="Click To Copy" data-tooltip-align="b,c" data-tooltip="Click to copy" aria-label="Click to copy" data-title="Click to copy">The class uses two annotations <strong><span style="color: #0000ff;">@LargeTest</span></strong> and <strong><span style="color: #0000ff;">@Runwith</span></strong> these two annotations are used to specify the behavior for our TestClass.</li>
</ul>
<ul>
<li><strong><span style="color: #0000ff;">@RunWith </span></strong>: To create an instrumented JUnit 4 test class, add the <strong><span style="color: #0000ff;">@RunWith(AndroidJUnit4.class) </span></strong>annotation at the beginning of your test class definition.</li>
<li><strong><span style="color: #0000ff;">@Rule </span></strong>: Provides functional testing of a single activity.UI can access the activity using the rule and there by can access the resources etc.</li>
<li>The below line of code is used to run the test case in particular order.</li>
</ul>
<pre style="padding-left: 30px;"><code>@FixMethodOrder(MethodSorters.NAME_ASCENDING)</code></pre>
<ul>
<li><strong><span style="color: #0000ff;">@After</span> </strong>: Methods annotated with this will be run after every test and can be used for resetting a variable after test.</li>
<li><strong><span style="color: #0000ff;">@Before</span></strong> : Methods annotated with this will be run before every test and can be used for setting up pre-conditions that needs to be executed before executing each <strong><span style="color: #0000ff;">@Test</span></strong>.</li>
<li><strong><span style="color: #0000ff;">@Test </span></strong>: The @Test annotation tells <span style="color: #0000ff;"><strong>JUnit</strong></span> that the <strong><span style="color: #008000;">public void</span></strong> method to which it is attached can be run as a test case. Methods marked with @Test are run after the @Before.</li>
<li><strong><span style="color: #0000ff;">onView()</span></strong> is used to select a view for testing and <strong><span style="color: #0000ff;">withId()</span></strong> is used to locate the UI element and rest is used to check whether the particular element is displayed or not.</li>
</ul>
</div>
<pre class="prettyprint lang-java"><code><span class="lit">@RunWith</span><span class="pun">(</span><span class="typ">AndroidJUnit4</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">)</span>
<span class="lit">@LargeTest
</span>@FixMethodOrder(MethodSorters.NAME_ASCENDING)
<span class="kwd">public</span> <span class="kwd">class</span> <span class="typ">HelloWorldEspressoTest</span> <span class="pun">{</span><span class="pln">

 </span><span class="lit">@Rule</span><span class="pln">
 </span><span class="kwd">public</span> <span class="typ">ActivityTestRule</span><span class="pun"><;</span><span class="typ">MainActivity</span><span class="pun">>;</span><span class="pln"> mActivityRule </span><span class="pun">=</span><span class="pln">
 </span><span class="kwd">new</span> <span class="typ">ActivityTestRule</span><span class="pun"><;>;(</span><span class="typ">MainActivity</span><span class="pun">.</span><span class="kwd">class</span><span class="pun">);
</span>
@Before
public void setUp() throws Exception {
 <span style="color: #0000ff;"><strong> //Before Test case execution</strong></span> 
}

<span class="lit">@Test</span><span class="pln">
 </span><span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> CheckHelloWorldDisplay</span><span class="pun">()</span> <span class="pun">{</span><span class="pln">
 onView</span><span class="pun">(withId(R.id.hello_world)</span><span class="pun">).</span><span class="pln">check</span><span class="pun">(</span><span class="pln">matches</span><span class="pun">(</span><span class="pln">isDisplayed</span><span class="pun">()));</span><span class="pln">
 </span><span class="pun">}</span>

@After
public void tearDown() throws Exception {
<span style="color: #0000ff;"><strong>//After Test case Execution</strong></span>
}
}</code></pre>
<h3><strong><span style="color: #000080;">Run the example test</span></strong></h3>
<p>Android Studio creates a blank Espresso test class when you create the project.</p>
<ol start="1" type="1">
<li>In the <span style="color: #008000;"><strong>Project >; Android</strong></span> pane, open <span style="color: #008000;"><strong><strong>java >;com.c1ctech.instrumentationtestdemo</strong></strong><strong>(androidTest)</strong></span>, and open <span style="color: #008000;"><strong>ExampleInstrumentedTest</strong></span>.</li>
</ol>
<p> ;</p>
<p><img class="wp-image-840 size-full aligncenter" src="https://c1ctech.com/wp-content/uploads/2018/09/espresso.png" alt="" width="495" height="330" /></p>
<p> ;</p>
<p>The project is supplied with this example test. You can create as many tests as you wish in this folder.</p>
<ol start="2" type="1">
<li>To run the test, <span style="color: #008000;"><strong>right-click</strong></span> (or <span style="color: #008000;"><strong>Control-click</strong></span>) <span style="color: #008000;"><strong>ExampleInstrumentedTest</strong></span> and choose <strong><span style="color: #008000;">Run</span> <span style="color: #008000;">ExampleInstrumentedTest</span></strong> from the pop-up menu. You can then choose to run the test on the emulator or on your device.</li>
</ol>
<p>The Run pane at the bottom of Android Studio shows the progress of the test, and when finishes, it displays &#8220;Tests ran to completion.&#8221; In the left column Android Studio displays &#8220;All Tests Passed&#8221;.</p>
<p><strong>I hope this tutorial will help you in understanding the basics of espresso testing and also how to do UI testing using espresso in our applications.</strong></p>
<p> ;</p>


