{"id":1711,"date":"2015-04-03T02:04:02","date_gmt":"2015-04-03T09:04:02","guid":{"rendered":"http:\/\/wiebe-elsinga.com\/blog\/?p=1711"},"modified":"2015-04-08T10:50:17","modified_gmt":"2015-04-08T17:50:17","slug":"the-hitchhikers-guide-to-android-testing-part-2-espresso","status":"publish","type":"post","link":"http:\/\/wiebe-elsinga.com\/blog\/the-hitchhikers-guide-to-android-testing-part-2-espresso\/","title":{"rendered":"The Hitchhiker&#8217;s guide to Android Testing, part 2: Espresso"},"content":{"rendered":"<div class=\"wpsso-pinterest-pin-it-image\" style=\"display:none !important;\">\n<\/div><!-- .wpsso-pinterest-pin-it-image -->\n\n<p>As mentioned in the <a href=\"http:\/\/www.technotalkative.com\/part-10-the-hitchhikers-guide-to-android-testing\/\">first article<\/a> <a href=\"https:\/\/plus.google.com\/+AliDerbane\">Ali Derbane<\/a> and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called <a href=\"https:\/\/code.google.com\/p\/android-test-kit\/\">Espresso<\/a>.<br \/>\n<!--more--><br \/>\n<img src=\"http:\/\/i.giphy.com\/VtFd68Pr19fYk.gif\" width=100%\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>Introduced at the GTAC in 2013, Espresso is designed to be used in environments where the developers write their own tests, and makes it possible to write concise, beautiful, and reliable Android UI tests quickly.<br \/>\nEspresso has several general components:<\/p>\n<ul>\n<li>The <code class=\"codecolorer bash default\"><span class=\"bash\">Espresso<\/span><\/code> class offers the <code class=\"codecolorer bash default\"><span class=\"bash\">onView<\/span><\/code> and <code class=\"codecolorer bash default\"><span class=\"bash\">onData<\/span><\/code> methods which, alone, can be used for a good numbers of possible tests on a given interface.<\/li>\n<li><code class=\"codecolorer bash default\"><span class=\"bash\">ViewMatchers<\/span><\/code> contains a collection of objects that implements the interface <code class=\"codecolorer bash default\"><span class=\"bash\">Matcher <span class=\"sy0\">&lt;<\/span>? super View<span class=\"sy0\">&gt;<\/span><\/span><\/code>. Using this Class you can collect and check View elements. For example, getting a View element (Button) with text &#8220;7&#8221;.<\/li>\n<li><code class=\"codecolorer bash default\"><span class=\"bash\">ViewActions<\/span><\/code> contains a collection of <code class=\"codecolorer bash default\"><span class=\"bash\">viewAction<\/span><\/code> objects to perform actions on a view. These actions are passed to the method <code class=\"codecolorer bash default\"><span class=\"bash\">ViewInteraction.perform<\/span><\/code> and may contain multiple actions. For example, clicking on a View element (Button).<\/li>\n<li><code class=\"codecolorer bash default\"><span class=\"bash\">ViewAssertions<\/span><\/code> contains a collection of <code class=\"codecolorer bash default\"><span class=\"bash\">ViewAssertion<\/span><\/code> to conduct checks on views. <\/li>\n<\/ul>\n<p>\nTo illustrate these components a test can look like this:<\/p>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"java codecolorer\">&nbsp; Espresso.<span class=\"me1\">onView<\/span><span class=\"br0\">&#40;<\/span>ViewMatchers.<span class=\"me1\">withText<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;7&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>ViewActions.<span class=\"me1\">click<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; Espresso.<span class=\"me1\">onView<\/span><span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">result<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">check<\/span><span class=\"br0\">&#40;<\/span>ViewAssertions.<span class=\"me1\">matches<\/span><span class=\"br0\">&#40;<\/span>ViewMatchers.<span class=\"me1\">withText<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;42&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>And the good news, as of last year Google has introduced a <a href=\"https:\/\/developer.android.com\/tools\/support-library\/index.html\">Testing Support Library<\/a> containing Espresso. So lets start by implementing Espresso.<\/p>\n<div style=\"background: lightgray; padding: 12pt;\">To illustrate, we are going to write some tests that tests agains a <a href=\"https:\/\/github.com\/welsinga\/sample_espresso\/app\">Android calculator application<\/a>. The common test scenario we will be implementing is testing if <em>\u20186\u2019 x \u20187\u2019 equals \u201842\u2019<\/em>.<\/div>\n<p>&nbsp;&nbsp;<\/p>\n<h3>Define the test runner<\/h3>\n<p>To use Espresso we first need to define who is running the tests. Espresso uses a new runner named <code class=\"codecolorer bash default\"><span class=\"bash\">AndroidJUnitRunner<\/span><\/code>. This runner, based on <code class=\"codecolorer bash default\"><span class=\"bash\">InstrumentationTestRunner<\/span><\/code> and <code class=\"codecolorer bash default\"><span class=\"bash\">GoogleInstrumentationTestRunner<\/span><\/code>, runs <em>JUnit3<\/em> and <em>JUnit4<\/em> tests against your Android application.<\/p>\n<p>First add the dependencies to your <code class=\"codecolorer bash blackboard\"><span class=\"bash\">build.gradle<\/span><\/code>, assuming you have installed the <a href=\"https:\/\/developer.android.com\/tools\/support-library\/index.html\">Testing Support Library<\/a>.<\/p>\n<div class=\"codecolorer-container groovy blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"groovy codecolorer\">dependencies <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; androidTestCompile <span class=\"st0\">'com.android.support.test:testing-support-lib:0.1'<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Then add the runner in your build.gradle<code class=\"codecolorer groovy default\"><span class=\"groovy\">android.<span class=\"me1\">defaultConfig<\/span><\/span><\/code> configuration:<\/p>\n<div class=\"codecolorer-container groovy blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><div class=\"groovy codecolorer\">defaultConfig <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; ...<br \/>\n&nbsp; <span class=\"me1\">testInstrumentationRunner<\/span> <span class=\"st0\">&quot;android.support.test.runner.AndroidJUnitRunner&quot;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>&nbsp;&nbsp;<\/p>\n<h3>Writing the test<\/h3>\n<p>As you may know test classes must be in <code class=\"codecolorer bash default\"><span class=\"bash\">src\\androidTest\\com.example.package.tests<\/span><\/code>, com.example.package being the package specified in the package attribute of the manifest element in the <code class=\"codecolorer bash default\"><span class=\"bash\">AndroidManifest<\/span><\/code> file.&nbsp;<br \/>\nAlso each test class must extend the abstract class <code class=\"codecolorer bash default\"><span class=\"bash\">ActivityInstrumentationTestCase2<\/span><\/code> and supply the Test Activity as generic type that will be used by default for testing.<br \/>\nIt must also be passed to the superclass via the <code class=\"codecolorer bash default\"><span class=\"bash\">super<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><\/span><\/code>. To make the Test Activity being called by the test framework, simply define a setup which calls the synchronous method <code class=\"codecolorer bash default\"><span class=\"bash\">getActivity<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>.<\/span><\/code><\/p>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/><\/div><\/td><td><div class=\"java codecolorer\"><span class=\"kw1\">public<\/span> <span class=\"kw1\">class<\/span> FunctionalInstrumentationTest <span class=\"kw1\">extends<\/span> ActivityInstrumentationTestCase2<span class=\"sy0\">&lt;<\/span>ActivityToTest<span class=\"sy0\">&gt;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">public<\/span> FunctionalInstrumentationTest<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">super<\/span><span class=\"br0\">&#40;<\/span>ActivityToTest.<span class=\"kw1\">class<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; @Override<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">protected<\/span> <span class=\"kw4\">void<\/span> setUp<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"kw1\">throws<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception\"><span class=\"kw3\">Exception<\/span><\/a> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">super<\/span>.<span class=\"me1\">setUp<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; getActivity<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>As mentioned we want to check if <em>\u20186\u2019 x \u20187\u2019 equals \u201842\u2019<\/em>.<\/p>\n<div class=\"codecolorer-container java blackboard\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/><\/div><\/td><td><div class=\"java codecolorer\"><span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> testAnswer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; onView<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;7&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; onView<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;\u00d7&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; onView<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;6&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; onView<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;=&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">perform<\/span><span class=\"br0\">&#40;<\/span>click<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; onView<span class=\"br0\">&#40;<\/span>withId<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">resText<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">check<\/span><span class=\"br0\">&#40;<\/span>matches<span class=\"br0\">&#40;<\/span>withText<span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;42&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>You may have noticed, this example is using static imports. This is solely done to make the code more readable.<\/p>\n<p>Other actions you want to use are:<\/p>\n<ul>\n<li><code class=\"codecolorer java default\"><span class=\"java\">pressBack<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/span><\/code> to simulate the use of the &#8220;back&#8221; button,<\/li>\n<li><code class=\"codecolorer java default\"><span class=\"java\">isDisplayed<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/span><\/code> to check if an element is being shown and <\/li>\n<li><code class=\"codecolorer java default\"><span class=\"java\">scrollTo<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/span><\/code> to scroll to an element.<\/li>\n<\/ul>\n<p>&nbsp;&nbsp;<\/p>\n<h3>Running the test<\/h3>\n<p>Now lets do the fun part, lets run the test. This can be done from the command line with he command line with <code class=\"codecolorer bash blackboard\"><span class=\"bash\">gradle connectedAndroidTest<\/span><\/code> or using Android Studio by:<\/p>\n<ol>\n<li><em>Open Run menu | Edit Configurations<\/em><\/li>\n<li><em>Add a new Android Tests configuration<\/em><\/li>\n<li><em>Choose the module you are testing<\/em><\/li>\n<li><em>Define our test runner: <code class=\"codecolorer bash default\"><span class=\"bash\">android.support.test.runner.AndroidJUnitRunner<\/span><\/code><\/em><\/li>\n<\/ol>\n<p>\t<br \/>\n<img src=\"http:\/\/i.giphy.com\/10OjZwNlstPj9e.gif\" width=100%\"\/><br \/>\n<br \/>\t<br \/>\nSo now you know a bit more about Espresso. If you want to know more, you may want to visit:<\/p>\n<ul>\n<li><a href=\"https:\/\/code.google.com\/p\/android-test-kit\/\">Espresso website<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/welsinga\/sample_espresso\">Github repo corresponding to this article<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/googlesamples\/android-testing\">General Espresso Github samples by Google<\/a><\/li>\n<\/ul>\n<p>\n<strong>Updated<\/strong> (08-04) When running the tests with gradle you can just use <code class=\"codecolorer bash blackboard\"><span class=\"bash\">gradle connectedAndroidTest<\/span><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned in the first article Ali Derbane and me wrote about Android Functional Testing, there are a lot of frameworks you can use. In this second part of the journey I will be explaining the functional testing framework called Espresso.<\/p>\n","protected":false},"author":1,"featured_media":1739,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[133,7],"tags":[154,125,149,150],"_links":{"self":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1711"}],"collection":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/comments?post=1711"}],"version-history":[{"count":0,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/posts\/1711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media\/1739"}],"wp:attachment":[{"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/media?parent=1711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/categories?post=1711"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wiebe-elsinga.com\/blog\/wp-json\/wp\/v2\/tags?post=1711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}