Site icon C1CTech

Android Espresso Testing Basics

<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Espresso Introduction <&sol;strong><&sol;span><&sol;h3>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Espresso<&sol;strong><&sol;span> is an open source instrumentation Testing framework made available by Google for the ease of <span style&equals;"color&colon; &num;008000&semi;"><strong class&equals;"markup--strong markup--p-strong">UI Testing<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>It is used to automate manual test case for android application&period;<&sol;p>&NewLine;<p>Espresso automatically <strong><span style&equals;"color&colon; &num;008000&semi;">syncronizes<&sol;span><&sol;strong> your test actions with the ui of your application&period;The framework also ensures that your activity is started before the tests run&period;It also let the test wait until all observed background activities have finished&period;<&sol;p>&NewLine;<p>The main components of Espresso include the following&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Espresso<&sol;strong><&sol;span> – Entry point to interactions with views &lpar;via <strong><span style&equals;"color&colon; &num;008000&semi;">onView&lpar;&rpar; and onData&lpar;&rpar;<&sol;span><&sol;strong>&rpar;&period; Also exposes APIs that are not necessarily tied to any view&comma; such as <strong><span style&equals;"color&colon; &num;008000&semi;">pressBack&lpar;&rpar;<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ViewMatchers<&sol;strong><&sol;span> –<span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;Match a view&rpar;<&sol;strong><&sol;span> Allows you to find  view in the current view hierarchy&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ViewActions<&sol;strong><&sol;span> – <span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;Perform an action&rpar;<&sol;strong><&sol;span>Allows you to perform an action on a <code>View&period;<&sol;code><&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ViewAssertions<&sol;strong><&sol;span> – <span style&equals;"color&colon; &num;008000&semi;"><strong>&lpar;Assert and verify the result&rpar;<&sol;strong><&sol;span>Check the state of the <code>View<&sol;code> to see if it reflects the expected state or behavior defined by the assertion&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<pre class&equals;"prettyprint lang-java"><code><span class&equals;"pln">onView<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">withId<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">R<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">id<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">my&lowbar;view<&sol;span><span class&equals;"pun">&rpar;&rpar;<&sol;span><span class&equals;"pln">        <&sol;span><strong><span class&equals;"com" style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; withId&lpar;R&period;id&period;my&lowbar;view&rpar; is a ViewMatcher<&sol;span><&sol;strong><span class&equals;"pln">&NewLine;    <&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">perform<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">click<&sol;span><span class&equals;"pun">&lpar;&rpar;&rpar;<&sol;span><span class&equals;"pln">               <&sol;span><strong><span class&equals;"com" style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; click&lpar;&rpar; is a ViewAction<&sol;span><&sol;strong><span class&equals;"pln">&NewLine;    <&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"pln">check<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">matches<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">isDisplayed<&sol;span><span class&equals;"pun">&lpar;&rpar;&rpar;&rpar;&semi;<&sol;span> <strong><span class&equals;"com" style&equals;"color&colon; &num;0000ff&semi;">&sol;&sol; matches&lpar;isDisplayed&lpar;&rpar;&rpar; is a ViewAssertion<&sol;span><&sol;strong><&sol;code><&sol;pre>&NewLine;<h3 class&equals;"devsite-page-title"><strong><span style&equals;"color&colon; &num;000080&semi;">Espresso setup instructions<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>To use Espresso&comma; you must already have the Android Support Repository installed with Android Studio&period; You may also need to configure Espresso in your project&period;<&sol;p>&NewLine;<h6><strong><span style&equals;"color&colon; &num;0000ff&semi;">Check for the Android Support Repository<&sol;span><&sol;strong><&sol;h6>&NewLine;<p><strong>1<&sol;strong>&period; Open the project in Android Studio&comma; and choose <span style&equals;"color&colon; &num;008000&semi;"><strong>Tools &gt&semi; Android &gt&semi; SDK Manager<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>The Android SDK <span style&equals;"color&colon; &num;008000&semi;"><strong>Default Preferences<&sol;strong><&sol;span> pane appears&period;<&sol;p>&NewLine;<p><strong>2<&sol;strong>&period; Click the <span style&equals;"color&colon; &num;008000&semi;"><strong>SDK Tools<&sol;strong><&sol;span> tab and expand <span style&equals;"color&colon; &num;008000&semi;"><strong>Support Repository<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><strong>3<&sol;strong>&period; Look for <span style&equals;"color&colon; &num;008000&semi;"><strong>Android Support Repository<&sol;strong><&sol;span> in the list&period;<&sol;p>&NewLine;<p>If <span style&equals;"color&colon; &num;008000&semi;"><strong>Installed<&sol;strong><&sol;span> appears in the Status column then click <span style&equals;"color&colon; &num;008000&semi;"><strong>Cancel<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p>If <span style&equals;"color&colon; &num;008000&semi;"><strong>Not installed<&sol;strong><&sol;span> or <strong><span style&equals;"color&colon; &num;008000&semi;">Update Available<&sol;span> <&sol;strong>appears&comma; click the checkbox next to <span style&equals;"color&colon; &num;008000&semi;"><strong>Android Support Repository<&sol;strong><&sol;span>&period; A download icon should appear next to the checkbox&period; Click <strong><span style&equals;"color&colon; &num;008000&semi;">OK<&sol;span>&period;<&sol;strong><&sol;p>&NewLine;<p><strong>4<&sol;strong>&period; Click <span style&equals;"color&colon; &num;008000&semi;"><strong>OK<&sol;strong><&sol;span> again&comma; and then <span style&equals;"color&colon; &num;008000&semi;"><strong>Finish<&sol;strong><&sol;span> when the support repository has been installed&period;<&sol;p>&NewLine;<h6><strong><span style&equals;"color&colon; &num;0000ff&semi;">Configure Espresso for the project<&sol;span><&sol;strong><&sol;h6>&NewLine;<p>When you start a project for the phone and tablet form factor using <span style&equals;"color&colon; &num;008000&semi;"><strong>API 15&colon; Android 4&period;0&period;3 &lpar;Ice Cream Sandwich&rpar;<&sol;strong><&sol;span> as the minimum SDK&comma; Android Studio automatically includes the dependencies you need to use Espresso&period;<&sol;p>&NewLine;<p>If you have created your project in a previous version of Android Studio&comma; you may have to add the <span style&equals;"color&colon; &num;008000&semi;"><strong>dependencies and instrumentation runner<&sol;strong><&sol;span> yourself&period; To add the dependencies yourself&comma; follow these steps&colon;<&sol;p>&NewLine;<ol start&equals;"1" type&equals;"1">&NewLine;<li>Open your project in Android Studio in which you want to configure Espresso &period;<&sol;li>&NewLine;<li>Open the <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle &lpar;Module&colon; app&rpar;<&sol;strong><&sol;span> file&period;<&sol;li>&NewLine;<li>Check if the following is included &lpar;along with other dependencies&rpar; in the <span style&equals;"color&colon; &num;008000&semi;"><strong>dependencies<&sol;strong><&sol;span> section&colon;<&sol;li>&NewLine;<&sol;ol>&NewLine;<pre><code>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;27&period;1&period;1'&NewLine; implementation 'com&period;android&period;support&period;constraint&colon;constraint-layout&colon;1&period;1&period;3'&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong> testImplementation 'junit&colon;junit&colon;4&period;12'<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong> androidTestImplementation 'com&period;android&period;support&period;test&colon;runner&colon;1&period;0&period;2'<&sol;strong><&sol;span>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong> androidTestImplementation 'com&period;android&period;support&period;test&period;espresso&colon;espresso-core&colon;3&period;0&period;2'<&sol;strong><&sol;span>&NewLine; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>If the file doesn&&num;8217&semi;t include the above dependency statements&comma; enter them into the <span style&equals;"color&colon; &num;008000&semi;"><strong>dependencies<&sol;strong><&sol;span> section&period;<&sol;p>&NewLine;<ol start&equals;"4" type&equals;"1">&NewLine;<li>Android Studio also adds the following instrumentation statement to the end of the <strong><span style&equals;"color&colon; &num;008000&semi;">defaultConfig<&sol;span><&sol;strong> section of a new project&colon;<&sol;li>&NewLine;<&sol;ol>&NewLine;<pre><code>defaultConfig &lbrace;&NewLine; applicationId "com&period;c1ctech&period;instrumentationtestdemo"&NewLine; minSdkVersion 21&NewLine; targetSdkVersion 27&NewLine; versionCode 1&NewLine; versionName "1&period;0"&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">testInstrumentationRunner "android&period;support&period;test&period;runner&period;AndroidJUnitRunner"<&sol;span><&sol;strong>&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<p>If the file doesn&&num;8217&semi;t include the above instrumentation statement&comma; enter it at the end of the <span style&equals;"color&colon; &num;008000&semi;"><strong>defaultConfig<&sol;strong><&sol;span> section&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<ol start&equals;"5" type&equals;"1">&NewLine;<li>If you modified the <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle &lpar;Module&colon; app&rpar;<&sol;strong><&sol;span> file&comma; click the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Sync Now<&sol;strong><&sol;span> link in the notification about Gradle files in top right corner of the window&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h6 id&equals;"example-gradle-build-file"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Example Gradle build file<&sol;span><&sol;strong><&sol;h6>&NewLine;<pre><code>apply plugin&colon; 'com&period;android&period;application'&NewLine;&NewLine;android &lbrace;&NewLine; compileSdkVersion 27&NewLine; defaultConfig &lbrace;&NewLine; applicationId "com&period;c1ctech&period;instrumentationtestdemo"&NewLine; minSdkVersion 15&NewLine; targetSdkVersion 27&NewLine; versionCode 1&NewLine; versionName "1&period;0"&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;"> testInstrumentationRunner "android&period;support&period;test&period;runner&period;AndroidJUnitRunner"<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine; buildTypes &lbrace;&NewLine; release &lbrace;&NewLine; minifyEnabled false&NewLine; proguardFiles getDefaultProguardFile&lpar;'proguard-android&period;txt'&rpar;&comma; 'proguard-rules&period;pro'&NewLine; &rcub;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;27&period;1&period;1'&NewLine; implementation 'com&period;android&period;support&period;constraint&colon;constraint-layout&colon;1&period;1&period;3'&NewLine; <strong><span style&equals;"color&colon; &num;0000ff&semi;">testImplementation 'junit&colon;junit&colon;4&period;12'&NewLine; androidTestImplementation 'com&period;android&period;support&period;test&colon;runner&colon;1&period;0&period;2'&NewLine; androidTestImplementation 'com&period;android&period;support&period;test&period;espresso&colon;espresso-core&colon;3&period;0&period;2'<&sol;span><&sol;strong>&NewLine; &NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h6><strong><span style&equals;"color&colon; &num;0000ff&semi;">Turn off animations on your test device<&sol;span><&sol;strong><&sol;h6>&NewLine;<p>Android phones and tablets display animations when moving between apps and screens&period; The animations are attractive when using the device&comma; but they slow down performance&comma; and may also cause unexpected results or may lead your test to fail&period; So it&&num;8217&semi;s a good idea to turn off animations on your virtual or physical devices used for testing&period;  On your device&comma; go to <span style&equals;"color&colon; &num;008000&semi;"><b>Settings &gt&semi; Developer options&gt&semi;<strong>Drawing <&sol;strong><&sol;b><&sol;span>and disable the following 3 settings&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Window animation scale<&sol;li>&NewLine;<li>Transition animation scale<&sol;li>&NewLine;<li>Animator duration scale<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3 id&equals;"add-first-test"><strong><span style&equals;"color&colon; &num;000080&semi;">Add the first test<&sol;span><&sol;strong><&sol;h3>&NewLine;<ol>&NewLine;<li>In android studio it is important to write instrumented functional test cases in <strong><span style&equals;"color&colon; &num;008000&semi;">src&sol;androdTest&sol;java<&sol;span><&sol;strong> folder and unit tests in <span style&equals;"color&colon; &num;008000&semi;"><strong>src&sol;Test&sol;java<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<p>Open your project then go to <strong><span style&equals;"color&colon; &num;0000ff&semi;">src&sol;androidTest&sol;java&sol;com&period;c1ctech&period;instrumentationtestdemo&lpar;package name&rpar;<&sol;span><&sol;strong>-&gt&semi;<strong><span style&equals;"color&colon; &num;0000ff&semi;">New<&sol;span><&sol;strong>-&gt&semi;<strong><span style&equals;"color&colon; &num;0000ff&semi;">Java<&sol;span> <span style&equals;"color&colon; &num;0000ff&semi;">Class<&sol;span><&sol;strong><b> <&sol;b>give Class Name <span style&equals;"color&colon; &num;008000&semi;"><strong>TestClass<&sol;strong><&sol;span>  and click on <b><span style&equals;"color&colon; &num;008000&semi;">OK<&sol;span>&period;<&sol;b><&sol;p>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>After creating the <span style&equals;"color&colon; &num;008000&semi;"><strong>TestClass<&sol;strong><&sol;span> you are ready to start writing your test cases&period;<&sol;p>&NewLine;<div class&equals;"devsite-code-button-wrapper">&NewLine;<ul>&NewLine;<li class&equals;"devsite-code-button gc-analytics-event material-icons devsite-click-to-copy-button" data-category&equals;"Site-Wide Custom Events" data-label&equals;"Click To Copy" data-tooltip-align&equals;"b&comma;c" data-tooltip&equals;"Click to copy" aria-label&equals;"Click to copy" data-title&equals;"Click to copy">The class uses two annotations <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;LargeTest<&sol;span><&sol;strong> and <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Runwith<&sol;span><&sol;strong> these two annotations are used to specify the behavior for our TestClass&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;RunWith <&sol;span><&sol;strong>&colon; To create an instrumented JUnit 4 test class&comma; add the <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;RunWith&lpar;AndroidJUnit4&period;class&rpar; <&sol;span><&sol;strong>annotation at the beginning of your test class definition&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Rule <&sol;span><&sol;strong>&colon; Provides functional testing of a single activity&period;UI can access the activity using the rule and there by can access the resources etc&period;<&sol;li>&NewLine;<li>The below line of code is used to run the test case in particular order&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<pre style&equals;"padding-left&colon; 30px&semi;"><code>&commat;FixMethodOrder&lpar;MethodSorters&period;NAME&lowbar;ASCENDING&rpar;<&sol;code><&sol;pre>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;After<&sol;span> <&sol;strong>&colon; Methods annotated with this will be run after every test and can be used for resetting a variable after test&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Before<&sol;span><&sol;strong> &colon; 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&equals;"color&colon; &num;0000ff&semi;">&commat;Test<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Test <&sol;span><&sol;strong>&colon; The &commat;Test annotation tells <span style&equals;"color&colon; &num;0000ff&semi;"><strong>JUnit<&sol;strong><&sol;span> that the <strong><span style&equals;"color&colon; &num;008000&semi;">public void<&sol;span><&sol;strong> method to which it is attached can be run as a test case&period; Methods marked with &commat;Test  are run after the &commat;Before&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">onView&lpar;&rpar;<&sol;span><&sol;strong> is used to select a view for testing and <strong><span style&equals;"color&colon; &num;0000ff&semi;">withId&lpar;&rpar;<&sol;span><&sol;strong> is used to locate the UI element and rest is used to check whether the particular element is displayed or not&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<pre class&equals;"prettyprint lang-java"><code><span class&equals;"lit">&commat;RunWith<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"typ">AndroidJUnit4<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"kwd">class<&sol;span><span class&equals;"pun">&rpar;<&sol;span>&NewLine;<span class&equals;"lit">&commat;LargeTest&NewLine;<&sol;span>&commat;FixMethodOrder&lpar;MethodSorters&period;NAME&lowbar;ASCENDING&rpar;&NewLine;<span class&equals;"kwd">public<&sol;span> <span class&equals;"kwd">class<&sol;span> <span class&equals;"typ">HelloWorldEspressoTest<&sol;span> <span class&equals;"pun">&lbrace;<&sol;span><span class&equals;"pln">&NewLine;&NewLine;    <&sol;span><span class&equals;"lit">&commat;Rule<&sol;span><span class&equals;"pln">&NewLine;    <&sol;span><span class&equals;"kwd">public<&sol;span> <span class&equals;"typ">ActivityTestRule<&sol;span><span class&equals;"pun">&lt&semi;<&sol;span><span class&equals;"typ">MainActivity<&sol;span><span class&equals;"pun">&gt&semi;<&sol;span><span class&equals;"pln"> mActivityRule <&sol;span><span class&equals;"pun">&equals;<&sol;span><span class&equals;"pln">&NewLine;            <&sol;span><span class&equals;"kwd">new<&sol;span> <span class&equals;"typ">ActivityTestRule<&sol;span><span class&equals;"pun">&lt&semi;&gt&semi;&lpar;<&sol;span><span class&equals;"typ">MainActivity<&sol;span><span class&equals;"pun">&period;<&sol;span><span class&equals;"kwd">class<&sol;span><span class&equals;"pun">&rpar;&semi;&NewLine;<&sol;span>&NewLine;&commat;Before&NewLine;public void setUp&lpar;&rpar; throws Exception &lbrace;&NewLine; <span style&equals;"color&colon; &num;0000ff&semi;"><strong> &sol;&sol;Before Test case execution<&sol;strong><&sol;span> &NewLine;&rcub;&NewLine;&NewLine;<span class&equals;"lit">&commat;Test<&sol;span><span class&equals;"pln">&NewLine;    <&sol;span><span class&equals;"kwd">public<&sol;span> <span class&equals;"kwd">void<&sol;span><span class&equals;"pln"> CheckHelloWorldDisplay<&sol;span><span class&equals;"pun">&lpar;&rpar;<&sol;span> <span class&equals;"pun">&lbrace;<&sol;span><span class&equals;"pln">&NewLine;        onView<&sol;span><span class&equals;"pun">&lpar;withId&lpar;R&period;id&period;hello&lowbar;world&rpar;<&sol;span><span class&equals;"pun">&rpar;&period;<&sol;span><span class&equals;"pln">check<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">matches<&sol;span><span class&equals;"pun">&lpar;<&sol;span><span class&equals;"pln">isDisplayed<&sol;span><span class&equals;"pun">&lpar;&rpar;&rpar;&rpar;&semi;<&sol;span><span class&equals;"pln">&NewLine;    <&sol;span><span class&equals;"pun">&rcub;<&sol;span>&NewLine;&NewLine;&commat;After&NewLine;public void tearDown&lpar;&rpar; throws Exception &lbrace;&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>&sol;&sol;After Test case Execution<&sol;strong><&sol;span>&NewLine;&rcub;&NewLine;&rcub;<&sol;code><&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Run the example test<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Android Studio creates a blank Espresso test class when you create the project&period;<&sol;p>&NewLine;<ol start&equals;"1" type&equals;"1">&NewLine;<li>In the <span style&equals;"color&colon; &num;008000&semi;"><strong>Project &gt&semi; Android<&sol;strong><&sol;span> pane&comma; open <span style&equals;"color&colon; &num;008000&semi;"><strong><strong>java &gt&semi;com&period;c1ctech&period;instrumentationtestdemo<&sol;strong><&sol;strong><strong>&lpar;androidTest&rpar;<&sol;strong><&sol;span>&comma; and open <span style&equals;"color&colon; &num;008000&semi;"><strong>ExampleInstrumentedTest<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><img class&equals;"wp-image-840 size-full aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;espresso&period;png" alt&equals;"" width&equals;"495" height&equals;"330" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>The project is supplied with this example test&period; You can create as many tests as you wish in this folder&period;<&sol;p>&NewLine;<ol start&equals;"2" type&equals;"1">&NewLine;<li>To run the test&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>right-click<&sol;strong><&sol;span> &lpar;or <span style&equals;"color&colon; &num;008000&semi;"><strong>Control-click<&sol;strong><&sol;span>&rpar; <span style&equals;"color&colon; &num;008000&semi;"><strong>ExampleInstrumentedTest<&sol;strong><&sol;span> and choose <strong><span style&equals;"color&colon; &num;008000&semi;">Run<&sol;span> <span style&equals;"color&colon; &num;008000&semi;">ExampleInstrumentedTest<&sol;span><&sol;strong> from the pop-up menu&period; You can then choose to run the test on the emulator or on your device&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>The Run pane at the bottom of Android Studio shows the progress of the test&comma; and when finishes&comma; it displays &&num;8220&semi;Tests ran to completion&period;&&num;8221&semi; In the left column Android Studio displays &&num;8220&semi;All Tests Passed&&num;8221&semi;&period;<&sol;p>&NewLine;<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&period;<&sol;strong><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version