{"id":4785,"date":"2018-07-13T10:16:50","date_gmt":"2018-07-13T17:16:50","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vsappcenter\/?p=4785"},"modified":"2019-02-16T15:30:13","modified_gmt":"2019-02-16T22:30:13","slug":"visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/appcenter\/visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center\/","title":{"rendered":"Visual UI Testing for Android Apps with Espresso and Visual Studio App Center"},"content":{"rendered":"<p><em>This is a guest post from Daniel Puterman, engineering lead at Applitools.<\/em><\/p>\n<p>Suppose you\u2019ve built a new Android app and want to build some visual test automation scripts to ensure that it appears correctly on the devices it might run on. <a href=\"https:\/\/www.androidbenchmark.net\/popular_chart.html?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Not all the Android devices out there<\/a> \u2014 just the top 20 for now. Suppose further that your app isn\u2019t all that complex; just ten screens, with an average of ten visual elements each. And you won\u2019t check every last attribute of those visual elements, just five: x, y position, height, width, and text. <\/p>\n<p>Simple, right?<\/p>\n<p>Unfortunately, no. <\/p>\n<p>If you apply traditional functional testing techniques to visual testing, you just signed yourself up for writing test automation code with <strong>10,000 checkpoints<\/strong> (= 20 devices * 10 screens * 10 elements * 5 attributes).<\/p>\n<p>Of course, there\u2019s no way you\u2019re going to write 10,000 lines of checkpoint code. That could be more than all the code in your Android app itself. So you cut corners. Reduce your test matrix. Write less validation code.<\/p>\n<p>But then your users find bugs in production, on a platform you didn\u2019t test on.<\/p>\n<p>It\u2019s a dilemma that developers face regularly: Spend all your time writing test code, or scramble to fix production bugs.<\/p>\n<p>Luckily, there\u2019s a way to make it simpler with visual UI testing, which uses artificial intelligence to test an entire screen\u2019s visual appearance with just one API call. With this approach, 200 lines of test code is all that\u2019s needed to test on 20 devices and ten screens.<\/p>\n<p>In this tutorial, you\u2019ll learn how to build and run a visual user interface test of a native Android mobile app using the following tools: <a href=\"https:\/\/developer.android.com\/training\/testing\/espresso\/?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Espresso<\/a>, <a href=\"https:\/\/visualstudio.microsoft.com\/app-center\/\">Visual Studio App Center<\/a>, and <a href=\"https:\/\/applitools.com\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter&amp;utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Applitools<\/a>. <\/p>\n<p>As an engineering lead at Applitools, I rely on visual UI testing for many apps and will cover the following topics:<\/p>\n<ol>\n<li> A quick introduction to visual UI testing <\/li>\n<li> Preparing the Ground <\/li>\n<li> Run your first mobile visual UI test on Visual Studio App Center <\/li>\n<\/ol>\n<h2>Get Started with Visual UI Testing<\/h2>\n<p>If you\u2019re not familiar with visual UI testing, here\u2019s a brief introduction.<\/p>\n<p>Here\u2019s the Hello World app from <a href=\"https:\/\/applitools.com\/resources\/tutorial\/espresso?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Applitools\u2019 \u201cGetting Started\u201d guide for the Espresso SDK:<\/a><\/p>\n<p><img decoding=\"async\" src=\"\" alt=\"\" width=\"150\" height=\"300\" class=\"aligncenter size-medium wp-image-4825\" \/><\/p>\n<p>How would you test this? You could verify that a button exists and the button\u2019s text is \u201cCLICK ME!\u201d. What about the \u201cHELLO WORLD!\u201d title? Again, you can verify the text, but what about the colors? What about the positions of the title element? The position of the button? What about the rest of the elements? <\/p>\n<p>Without visual testing, you need to write a lot of validations (also called assertions or checkpoints), one for every property you want to test (content, position, color, etc.), and even then you cannot be sure the final result is displayed properly to the user. <\/p>\n<p>Visual testing solves this problem easily. Let\u2019s show you how using <a href=\"https:\/\/developer.android.com\/training\/testing\/espresso\/?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Espresso<\/a>, <a href=\"https:\/\/visualstudio.microsoft.com\/app-center\/?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">App Center<\/a>, and <a href=\"https:\/\/applitools.com\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter&amp;utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Applitools<\/a>.<\/p>\n<p>With a single \u201ccheck\u201d command, you verify <strong>all <\/strong>these properties for the <strong>entire<\/strong> content of the application. So your advantage is double:<\/p>\n<ol>\n<li> You don\u2019t need to write a separate assert statement for each element.<\/li>\n<li> You get full coverage, not just coverage for specific elements.<\/li>\n<\/ol>\n<p>The result of visual testing this application using Applitools are shown below. The known good baseline version is on the left; the new test build, on the right. Visual differences are detected automatically using Applitools\u2019 AI and highlighted in magenta:\n&nbsp;\n<img decoding=\"async\" src=\"\" alt=\"\" width=\"1024\" height=\"536\" class=\"aligncenter size-large wp-image-4805\" \/>\n&nbsp;\nSince visual tests capture the entire screen &#8212; not just what\u2019s in the <a href=\"https:\/\/www.techopedia.com\/definition\/32644\/viewport\">viewport<\/a>, but also content the user needs to scroll in order to view &#8212; we have validated the <strong>entire<\/strong> application while we ran the tests.<\/p>\n<p>The command which validates the entire screen is:<\/p>\n<pre style=\"background: transparent !important;border: none\"> \n<em>eyes.checkWindow(\"Hello World screen\");<\/em>\n<\/pre>\n<p>That\u2019s a lot easier than writing dozens of lines of checkpoint code, right?<\/p>\n<p>If you agree, let\u2019s dive into how to get this working with Espresso, Visual Studio App Center, and Applitools.<\/p>\n<h2>Preparing the Ground<\/h2>\n<p>You\u2019ll need this software to get started:<\/p>\n<ul>\n<li><a href=\"https:\/\/developer.android.com\/studio\/\">Android Studio<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/\">npm<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/appcenter-cli\">appcenter-cli<\/a> package from npm<\/li>\n<\/ul>\n<p>You need to create accounts on:<\/p>\n<ul>\n<li><a href=\"https:\/\/appcenter.ms\/\">App Center<\/a><\/li>\n<li><a href=\"https:\/\/applitools.com\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Applitools<\/a><\/li>\n<\/ul>\n<p>Set up the above before continuing.<\/p>\n<h2>Downloading the Demo Application<\/h2>\n<p>For this guide, we\u2019ve prepared a demo application which includes a visual Espresso UI test: \n<a href=\"https:\/\/applitools.bintray.com\/Examples\/app-center-eyes-android-hello-world.zip\">https:\/\/applitools.bintray.com\/Examples\/app-center-eyes-android-hello-world.zip <\/a><\/p>\n<p>You can find the source code for the application on GitHub: <a href=\"https:\/\/github.com\/applitools\/eyes-android-hello-world\/tree\/xamarin_cloud\">https:\/\/github.com\/applitools\/eyes-android-hello-world\/tree\/xamarin_cloud<\/a><\/p>\n<p>If you already have an existing application with Espresso tests just waiting to get visual validations, the quickest way to start would be through the <a href=\"https:\/\/applitools.com\/resources\/tutorial\/espresso\">Applitools getting started guide<\/a>. It walks you through adding the Applitools SDK for Espresso to your application.<\/p>\n<h2>Set up an Applitools Account<\/h2>\n<p><a href=\"https:\/\/applitools.com\/users\/register?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Create your Applitools account<\/a> so you get your API key for running visual tests. You can find it on the menu in the top right corner of the <a href=\"https:\/\/eyes.applitools.com\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Applitools Test Manage<\/a>r. Update the <a href=\"https:\/\/applitools.com\/docs\/topics\/overview\/obtain-api-key.html?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Applitools API key<\/a> in the <em>ExampleInstrumentedTest.java<\/em> file of the demo application, line 42.<\/p>\n<h2>Set up App Center<\/h2>\n<p>Once you <a href=\"\/\/appcenter.com\/ms\">create your App Center account<\/a>, you need to get credentials to run tests on App Center. First, you need to create your application, which allows you to describe the application you want to run on App Center. <strong>Click Add New &gt; Add New App.<\/strong><\/p>\n<p>Our application is a native Android Java application, so we\u2019ll mark it as such. The page that follows explains what to update in your application to run tests in App Center. In brief, the instructions are: <\/p>\n<ol>\n<li> Add dependencies to your <em>build.gradle<\/em> file <\/li>\n<li> Update your main activity:\n<ol type=\"a\">\n<li> Add imports <\/li>\n<li> Add AppCenter API call, with the application secret. <\/li>\n<\/ol>\n<\/ol>\n<p>In our demo application, we\u2019ve already added the imports and the API call. However, if you would also like analytics on the app in App Center, you need to update the proper credentials in the API call with your App Secret (the 2nd parameter to the<em> AppCenter.start<\/em> method). The application secret should be updated in the <em>MainActivity.java<\/em> file:\n&nbsp;\n<img decoding=\"async\" src=\"\" alt=\"\" width=\"1024\" height=\"506\" class=\"aligncenter size-large wp-image-4815\" \/>\n&nbsp;\nUpdating the application is not required to run wherever your test, however.<\/p>\n<h2>Create an App Center Test Run<\/h2>\n<p>Next, create a test run. Click <strong>New Test Run<\/strong> to do this.<\/p>\n<p>Select the devices to use for the tests. (With App Center, you can test your app on thousands of real devices in the cloud.) Then select \u201cmaster\u201d as the test series, and Espresso as the test framework.<\/p>\n<p>The next (\u201cSubmit\u201d) step of creating a new test run explains how to run tests on App Center cloud. This requires <a href=\"https:\/\/docs.microsoft.com\/en-us\/appcenter\/cli\/\">app-center-cli<\/a>.<\/p>\n<p>It also specifies the command to run the test. <strong>Copy this command<\/strong>. You\u2019ll use it soon.<\/p>\n<h2>Run Your First Mobile Visual Test on App Center<\/h2>\n<p>Next, build the application in App Center. To do this, click \u201cbuild\u201d under the \u201capp\u201d section in the \u201cGradle\u201d menu in Android Studio:\n&nbsp;\n<img decoding=\"async\" src=\"\" alt=\"\" width=\"650\" class=\"aligncenter size-medium wp-image-4835\" \/>\n&nbsp;\nTo run the visual test, paste the command you copied from the last step in the \u201cCreate an App Center Test Run\u201d section, in a command line of your choice, such as <a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/scripting\/getting-started\/getting-started-with-windows-powershell?view=powershell-6\">Windows PowerShell<\/a> or <a href=\"https:\/\/www.macworld.co.uk\/how-to\/mac-software\/how-use-terminal-on-mac-3608274\/\">macOS Terminal<\/a>. The command has the following template:<\/p>\n<p><em>appcenter test run espresso &#8211;app &#8220;&#8221; &#8211;devices &#8220;app-center-username\/named-device-set&#8221; &#8211;app-path pathToFile.apk  &#8211;test-series &#8220;master&#8221; &#8211;locale &#8220;en_US&#8221; &#8211;build-dir pathToEspressoBuildFolder<\/em><\/p>\n<p>Where: \n&#8212;<em>test-series<\/em> is the test series to run in. Defaults to &#8220;master&#8221;\n&#8212;<em>app-path<\/em> is the path to the application APK file\n&#8212;<em>build-dir<\/em> is the path to the <strong>tests build<\/strong> folder. E.g., <em>app\/build\/outputs\/apk\/androidTest\/debug\/<\/em><\/p>\n<h2>Visual Validation Results<\/h2>\n<p>After you run the test, open <a href=\"https:\/\/eyes.applitools.com\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Applitools Test Manager<\/a> to see the results.\n&nbsp;\n<img decoding=\"async\" src=\"\" alt=\"\" width=\"1024\" height=\"580\" class=\"aligncenter size-large wp-image-4795\" \/>\n&nbsp;\nAs you can see, you have an Android test named \u201cMy first Espresso Android test!\u201d, which includes two new steps. Both should be green, indicating they passed.<\/p>\n<p>Here\u2019s the visual UI test code sample (also on <a href=\"https:\/\/github.com\/applitools\/eyes-android-hello-world\/blob\/xamarin_cloud\/app\/src\/androidTest\/java\/helloworld\/applitools\/com\/applitoolshelloworld\/ExampleInstrumentedTest.java\">GitHub<\/a>).<\/p>\n<pre style=\"background: transparent !important;border: none\"> \ntry {\n        \/\/ Start a visual test in Applitools\n        eyes.open(\"Hello World!\", \"My first Espresso Android test!\");\n\n        \/\/ Visual checkpoint #1.\n        <strong>eyes.checkWindow(\"Hello!\")<\/strong>;\n\t  \n\t  \/\/ reportHelper creates labeled steps on App Center\n  reportHelper.label(\"Check window method\");\n\n        onView(withId(R.id.click_me_btn)).perform(click());\n        reportHelper.label(\"Click on ClickMe btn\");\n\n        \n\n        \/\/ Visual checkpoint #2.\n        <strong>eyes.checkWindow(\"Click!\")<\/strong>;\n\n        \/\/ End the test in Applitools.\n        eyes.close();\n    } \n\n<\/pre>\n<p>In this example, two <em><strong>eyes.checkWindow<\/strong><\/em> commands are in the test. The first command executes when the application starts, the other after the user clicks a button. Each of these commands corresponds to a step (screenshot) you see in the Applitools Test Manager. Each call to the checkWindow validates an entire screen of the application in a specific state. <\/p>\n<p>Pretty cool.<\/p>\n<h2>Wrapping up<\/h2>\n<p>At this point, you should have everything you need to start doing visual UI testing of Android applications using Applitools on Visual Studio App Center. To learn more:<\/p>\n<ul>\n<li>\t<a href=\"https:\/\/appcenter.ms\/signup?utm_source=AppCenter_blog&amp;utm_medium=Blog&amp;utm_campaign=visual-ui-testing-for-android-apps-with-espresso-and-studio-app-center&amp;utm_content=Visual%20UI%20Testing%20blog\">Get started for free with Visual Studio App Center<\/a> <\/li>\n<li>\t<a href=\"https:\/\/applitools.com\/request-demo?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Contact Applitools <\/a>for a demonstration of Visual Testing for your own apps<\/li>\n<li>\tDive into <a href=\"https:\/\/applitools.com\/docs\/?utm_source=microsoft&amp;utm_medium=blog&amp;utm_content=visual-testing-w-espresso-ms-appcenter\">Applitools documentation<\/a><\/li>\n<\/ul>\n<p>&nbsp;\n<em><strong>About the author<\/strong><\/em>\n<em>Daniel Puterman is R&amp;D director at Applitools &#8211; a leader in AI-powered visual testing and monitoring solutions. Daniel is a veteran programmer and team lead with experience in a wide variety of fields, from temperature loggers and web applications to location-based mobile applications and image processing. <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a guest post from Daniel Puterman, engineering lead at Applitools. Suppose you\u2019ve built a new Android app and want to build some visual test automation scripts to ensure that it appears correctly on the devices it might run on. Not all the Android devices out there \u2014 just the top 20 for now. [&hellip;]<\/p>\n","protected":false},"author":42,"featured_media":38034,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-4785","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mobiledev"],"acf":[],"blog_post_summary":"<p>This is a guest post from Daniel Puterman, engineering lead at Applitools. Suppose you\u2019ve built a new Android app and want to build some visual test automation scripts to ensure that it appears correctly on the devices it might run on. Not all the Android devices out there \u2014 just the top 20 for now. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/posts\/4785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/comments?post=4785"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/posts\/4785\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/media\/38034"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/media?parent=4785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/categories?post=4785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/appcenter\/wp-json\/wp\/v2\/tags?post=4785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}