<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Roman Kushnarenko on Medium]]></title>
        <description><![CDATA[Stories by Roman Kushnarenko on Medium]]></description>
        <link>https://medium.com/@sromku?source=rss-34b3a7a21dc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zeVGqSGicQ53FYVLkzRPUw.jpeg</url>
            <title>Stories by Roman Kushnarenko on Medium</title>
            <link>https://medium.com/@sromku?source=rss-34b3a7a21dc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 16:35:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sromku/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Running Android UI Tests -  Part 2]]></title>
            <link>https://medium.com/medisafe-tech-blog/running-android-ui-tests-part-2-15ef08056d94?source=rss-34b3a7a21dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/15ef08056d94</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Roman Kushnarenko]]></dc:creator>
            <pubDate>Tue, 08 Aug 2017 14:04:15 GMT</pubDate>
            <atom:updated>2017-08-08T14:04:15.454Z</atom:updated>
            <content:encoded><![CDATA[<h3>Running Android UI Tests - Part 2</h3><blockquote>In the previous part (<a href="https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8">Running Android UI Tests — Part 1</a>) we explained how we run Android UI tests and could easily clear data, clear notifications, pass params by adding simple Java annotations to our test methods. In addition, we collect logs, dump local db and preferences xml file, record and extract mp4 video and more. Read <a href="https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8">part 1</a> and check our <a href="https://github.com/medisafe/run-android-tests">open source</a> project for step by step tutorials and examples.</blockquote><p>In this part we will show how to run many tests in <strong>parallel </strong>on multiple devices. We will also show how to run <strong>group </strong>of sequencing<strong> </strong>tests on the same device when running in parallel.</p><blockquote>We improved execution UI tests time by 40% 💫</blockquote><h3>Parallel — The regular approach vs. push approach</h3><p>Before diving into optimizations and how we improve parallel execution test times, we explain the current sharding option that is provided by the Android testing framework.</p><h4>The regular approach - Sharding</h4><p>The Android Instrumentation runner supports test sharding using the numShards and shardIndex arguments (<a href="https://developer.android.com/tools/testing-support-library/index.html#ajur-sharding">documentation</a>).</p><p>Run:<br>adb -s {device} shell am instrument -w -e numShards 4 -e shardIndex 1</p><p><em>numShards</em> = number of groups we ask Android to divide all our tests.<br><em>shardIndex</em> = the group index we want to run on specific {device}.</p><p>This means, that if we want to run in parallel on 4 devices, we need to execute the same shell command with <em>shardIndex</em> starting from 1 to 4.</p><p>The main issues of this approach are:</p><ol><li><strong>Execution time</strong> — Android will split the tests to the groups without taking into consideration the execution time of each group. It might happen that the group of test in shard #1 will run for 1 hour, while the other shard will finish in 20 minutes.</li><li><strong>Grouping following tests</strong> — Android will split the tests to groups randomly. So if you decided to split a very big scenario to sequential tests in the same class, sharding might break your logic by putting the tests in different shards. And you have no control over this.</li></ol><h4>The push approach</h4><p>The idea is simple. Instead of splitting all tests into equal groups, we push the next test to a free device. If all devices are running, we wait until one of the devices completes the test execution.</p><p><strong>For example: </strong>We took 16 UI tests and ran them by using 2 different approaches. You can see an improvement of <strong>40%</strong> when pushing tests to free devices, instead of allocating an equal number of tests per device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0S-OllT4_zuV3b5zGNmgZQ.png" /></figure><p><strong>Running the script</strong></p><pre>./scripts/8/raw.sh artifacts/raw-tests.txt<br>./scripts/8/plan.sh artifacts/raw-tests.txt artifacts/plan-tests.txt<br>./scripts/8/run-shard.sh artifacts/plan-tests.txt artifacts</pre><p>Check the <a href="https://github.com/medisafe/run-android-tests/tree/master/app/src/androidTest/java/com/sromku/sample/runtests/shard">source code</a> and <a href="https://github.com/medisafe/run-android-tests/blob/master/docs/Parallel.md">detailed results here</a>.</p><p><strong>The script will:</strong></p><ol><li>Dump tests in raw format.</li><li>Prepare execution plan and split the tests to groups.</li><li>Run the groups of tests in parallel on all connected devices and emulators.</li></ol><h3>Grouping following tests</h3><p>It might be that we have a big scenario and we split it to multiple tests. The tests methods will follow each other in the same Class. This is fine if we run the tests on one device. But if we decide to use sharding, Android will split the tests to different devices. This is bad, because some of the following tests will fail.</p><p>This is why we added the @Following annotation and this annotation will enforce grouping and executing all following tests on the same device when running in parallel.</p><p><strong>For example: </strong>We set tests number 2,3 and 4 to be sequential. This means that test number 3 can successfully run only if test number 2 passes. Same for test number 4 that is following test number 3.</p><p>If we run the regular way, the tests will be split randomly between shards. In this example, luckily tests number 2 and 3 are running on the same device while unluckily test <strong>4 will always fail! :(</strong>.</p><p>If we use the @Following annotation and push the group of following tests to a free device, we will be able to keep the following dependency between tests on the same device. In addition, you can see that the tests are distributed differently than before and we could still improve 40% of max execution time. Amazing 💪 :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rhf392Adu8Ax4s8SfLdo7Q.png" /></figure><p><strong>Running the script</strong></p><pre># dump tests to raw file<br>./scripts/9/raw.sh artifacts/raw-tests.txt<br><br># build a plan<br>./scripts/9/plan.sh artifacts/raw-tests.txt artifacts/plan-tests.txt<br><br># run tests in parallel on all connected devices &amp; emulators<br>./scripts/9/run-shard.sh artifacts/plan-tests.txt artifacts</pre><p>Check the <a href="https://github.com/medisafe/run-android-tests/tree/master/app/src/androidTest/java/com/sromku/sample/runtests/follow">source code</a> and <a href="https://github.com/medisafe/run-android-tests/blob/master/docs/Following.md#run">detailed results here</a>.</p><p><strong>The script will:</strong></p><ol><li>Dump tests in raw format.</li><li>Prepare execution plan and split the tests to groups.</li><li>Run the groups of tests in parallel on all connected devices and emulators.</li></ol><h3>Summary</h3><p>In this part, we learned how we can run our UI tests in parallel and improved the max time execution by 40%. In addition, we can safely run following tests on the same device when running in parallel.</p><p>This is an addition to what we already learned in <a href="https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8"><strong>part 1</strong></a>. And you can use combination of all annotations as it used in our sample tests. Just run our full examples and check it yourself.</p><blockquote><strong>Source code</strong>: <a href="https://github.com/medisafe/run-android-tests">https://github.com/medisafe/run-android-tests</a></blockquote><p>That’s it for now. If you have any questions or comments, please share. Your feedback is super important ;)</p><p><a href="https://twitter.com/MedisafeApp">Medisafe (@MedisafeApp) | Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15ef08056d94" width="1" height="1" alt=""><hr><p><a href="https://medium.com/medisafe-tech-blog/running-android-ui-tests-part-2-15ef08056d94">Running Android UI Tests -  Part 2</a> was originally published in <a href="https://medium.com/medisafe-tech-blog">Medisafe Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running Android UI Tests - Part 1]]></title>
            <link>https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8?source=rss-34b3a7a21dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/53e85e5c8da8</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Roman Kushnarenko]]></dc:creator>
            <pubDate>Tue, 28 Mar 2017 20:28:32 GMT</pubDate>
            <atom:updated>2017-08-07T16:09:31.511Z</atom:updated>
            <content:encoded><![CDATA[<p>At <a href="https://medisafe.com/">Medisafe</a> 💊, we know how much our users depend and rely on our product. That is why we go the extra mile to make sure we bring them a complete, reliable solution that really works. Besides our Product and Dev teams, we do a lot of manual QA and put a lot of effort into answering our users daily through our support. Just as an example to how much coding and a quality app saves lives — <a href="https://www.facebook.com/MedisafeProject/photos/a.462934493758961.124433.111249148927499/1399084003477334/?type=3&amp;theater">read this amazing story</a>. To top it all, we were featured both on <a href="https://play.google.com/store/apps/details?id=com.medisafe.android.client">Android</a> and <a href="https://itunes.apple.com/us/app/medisafe-pill-reminder-medication-tracker/id573916946?mt=8">iOS</a> apps and were recently chosen for Editor’s Choice 🎉 on Google Play.</p><p>While we improve and extend our capabilities and partnerships, we had to start writing and running automation tests. We started by covering all manual sanity scenarios. We have many. The idea (at least for now) is to run automation tests on every build and on every branch.</p><p>There are some basic things we wanted to achieve when running automated tests. We wanted no additional headaches other than working with Espresso and UIAutomator on Android side. Therefore any additional features should be simple and straightforward to apply. We wanted to be able to quickly investigate failed tests, we wanted to use only unrooted, vanilla devices.</p><p>We came up with a working flow that works perfectly for us at Medisafe and we wanted to share it with you.</p><h4>Running tests</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sDL3bVF3Q0EGMtjx8gC1fg.png" /></figure><h4>Test Options</h4><p>We write UI tests same as before, when we did it manually. Only now, we can add more annotations that will give us more options:</p><ol><li><strong>ClearData</strong> — Clear data via ADB before test execution.</li><li><strong>Repeat</strong> — Repeat the same test X number of times, when current iteration is passed to the test.</li><li><strong>Tags</strong> — You can tag your tests. Later, you can run tests by selected tags only.</li><li><strong>ClearNotifications</strong> — Clear notification bar via ADB before running the test.</li></ol><h4>Test example</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/559a2c00f56cf9e084447b3aba8f4ee8/href">https://medium.com/media/559a2c00f56cf9e084447b3aba8f4ee8/href</a></iframe><h4><strong>Artifacts</strong></h4><p>In addition to new added options, after each failed test, we fetch and build useful files that will help us investigate the failed issues better:</p><ul><li>Recording (mp4)</li><li>Logs</li><li>DB (sqlite)</li><li>Shared preferences</li><li>Dumpsys — Netstats, battery, other.</li></ul><h3>📘 For this blog post</h3><ul><li><strong>Source code</strong>: <a href="https://github.com/medisafe/run-android-tests">https://github.com/medisafe/run-android-tests</a></li><li><strong>Device</strong>: Vanilla device. I am using regular Nexus 5X with Nougat.</li><li><strong>Android frameworks</strong>: Espresso, UIAutomator</li><li><strong>Script</strong>: Used for building and running execution plan. Written in bash.</li></ul><h3>📖 Table of Contents</h3><ol><li>Building Execution Plan</li><li>Collect Logs, Record Video, Dump DB, Shared Preferences</li><li>Add ‘Clear data’ support</li><li>Add ‘Clear notifications’ support</li><li>Add parameterized support</li><li>Run tests by #tags</li><li>Dump network stats, battery, alarms and more.</li><li>All together</li></ol><h3>The Idea</h3><p>Instead of running all tests at once, we run them one at a time, test by test. For each test we collect logs, record a video, dump db and preferences. The main trick is to control additional tasks for every test such as clearing data by using <strong>custom annotations, InstrumentationRunListener, ADB</strong> and simple <strong>bash touch</strong>.</p><blockquote><strong>Note</strong>: connectAndroidTest - One might ask, why aren’t we using the connectedAndroidTest gradle task and combining it with custom Rules. The issue is that we can’t run shell commands within a running process and user on an unrooted device. For example - “Clear data” can&#39;t be done this way.</blockquote><h4>The flow — Prepare &amp; Run</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HOq3SJeeuHqcvXTrIz_Eeg.png" /><figcaption>Instead of &quot;<em>connectTest&quot; Gradle task execution.</em></figcaption></figure><h4>Prepare</h4><ol><li><strong>Assemble</strong> app and test APKs and <strong>install</strong> them on device.</li><li>Use -e log true to retrieve all tests with their meta data.</li><li>Parse the tests raw file and create <strong>‘execution plan’</strong> for running the tests.</li></ol><h4>Run</h4><ol><li>Run the executable plan line by line.</li><li>For each non test, run appropriate ADB command.</li><li>For each UI test start video recording, logcat.</li><li>Execute the test.</li><li>If test failed stop everything.</li><li>Pull the video from device.</li><li>Dump DB, preferences, dumpsys files.</li></ol><h4>Results</h4><p>Without going into details:</p><ol><li>Deploy artifacts to cloud storage (S3/Drive/etc.).</li><li>Notify on company channels (Slack/Hipchat/Trello/Jira/etc.).</li><li>Send some CURLs to my internal webhooks to audit the results.</li></ol><h3>1. Building Execution Plan</h3><p>In this chapter we will:</p><ol><li>Assemble and install app and test APKs.</li><li>Fetch and print raw tests to by using -e log true flag.</li><li>Extract the tests from raw file and build execution plan — <strong><em>execution-plan.txt</em>.</strong></li><li>Run the tests.</li></ol><p>Go to <em>run-android-tests</em> folder and from terminal, <strong>build app and test APKs:</strong></p><pre># build app APK <br><strong>./gradlew assembleDebug --stacktrace </strong><br># build test APK <br><strong>./gradlew assembleAndroidTest --stacktrace</strong></pre><p>Install both app APK and test APK on real device.</p><pre># install app APK <br><strong>adb install -r app/build/outputs/apk/app-debug.apk </strong><br># install test APK <br><strong>adb install -r app/build/outputs/apk/app-debug-androidTest.apk</strong></pre><p>Print all available tests to the file: <strong>raw-tests.txt</strong>.</p><pre>./scripts/1/raw.sh artifacts/raw-tests.txt</pre><p>The <strong>raw.sh</strong> script doesn’t run the tests. By using -e log true flag, we print the tests only without real execution.</p><pre>adb shell am instrument -w -r <strong>-e log true</strong> -e package com.sromku.sample.runtests.basic com.sromku.sample.runtests.test/android.support.test.runner.AndroidJUnitRunner &gt; artifacts/raw-tests.txt</pre><p><strong>raw-tests.txt</strong></p><pre>... <br>INSTRUMENTATION_STATUS: numtests=2 <br>INSTRUMENTATION_STATUS: stream= <br>com.sromku.sample.runtests.basic.BasicOne:<br>INSTRUMENTATION_STATUS: id=AndroidJUnitRunner INSTRUMENTATION_STATUS: test=useAppContext <br>...</pre><h4>Build execution plan</h4><p>Let’s parse this file and extract all available tests and create execution plan to <strong>execution-plan.txt</strong></p><pre>./scripts/1/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><p>The script iterates over all lines of raw file and extracts the full name of the tests of format <em>Class#method</em>. Later it will also do more extractions from annotations (I will explain later).</p><p><strong>execution-plan.txt</strong></p><pre>com.sromku.sample.runtests.basic.BasicOne#testA com.sromku.sample.runtests.basic.BasicOne#testB</pre><h4>Run the tests</h4><pre>./scripts/1/run.sh artifacts/execution-plan.txt artifacts</pre><p>Explanation — <strong>scripts/1/run.sh</strong></p><p>Very simple, we run line by line in <em>execution-plan.txt</em> and execute the test. If we fail, we stop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daffd7eb7a332a6a5a8da3e19f159c5c/href">https://medium.com/media/daffd7eb7a332a6a5a8da3e19f159c5c/href</a></iframe><blockquote><strong>“FAILURES!!!”</strong> — is something that is hard coded in JUnit4 — <a href="https://github.com/junit-team/junit4/blob/ead2fe7ca97e4b0bc013efe0e734e39a45db213c/src/main/java/org/junit/internal/TextListener.java#L88">As seen on their branch</a>. Thus we can rely on this.</blockquote><h3>2. Collect Logs, Record Video, Dump DB, Shared Preferences</h3><p>Once we can run the tests from script, we have the advantage of running shell commands between the tests. This means that, before starting each test we can start Logcat and video recording processes and stop once the test is completed.</p><p>Do the same steps as before:</p><ol><li>Assemble and install app and test APKs</li><li>Fetch and print raw tests to by using flag -e log true</li><li>Extract the tests from raw file and make execution plan — <em>execution-plan.txt</em></li><li>Run the test and plan.</li></ol><h4><strong>Prepare</strong></h4><p>Same as before. Assemble and install test APK. Create raw test file <strong>raw-tests.txt</strong>.</p><pre>./gradlew assembleAndroidTest --stacktrace<br>adb install -r app/build/outputs/apk/app-debug-androidTest.apk<br>./scripts/2/raw.sh artifacts/raw-tests.txt</pre><h4>Build execution plan</h4><pre>./scripts/2/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><h4>Run the tests</h4><pre>./scripts/2/run.sh artifacts/execution-plan.txt artifacts</pre><p>The same script as in <em>scripts/1/run.sh</em> besides the addition before and after test execution. We start logcat process and start video recording. At the end of the test we kill both processes. Then we dump db and fetch shared preferences.</p><p><strong>scripts/2/run.sh</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2eedf8e72b27af12202a387c6a4b362f/href">https://medium.com/media/2eedf8e72b27af12202a387c6a4b362f/href</a></iframe><p>📦 The <strong>artifacts</strong> folder will contain:</p><ol><li>recording.mp4</li><li>logcat</li><li>running-test.txt &lt;- with full stacktrace inside</li><li>app.db — dumped sqlite db</li><li>shared_preferences.xml — default shared preferences file</li></ol><h3>3. Add ‘Clear data’ support</h3><p>Originally I wanted to clear data before selected tests. Unfortunately it’s impossible from already running process on device. I had to find a working solution. This is how to clear data. We just put @ClearData annotation above the test ;)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10263882730da8fc8a9e96b50c32c967/href">https://medium.com/media/10263882730da8fc8a9e96b50c32c967/href</a></iframe><p>The idea is as follow:</p><ol><li>We create a new annotation and call it ClearData.</li><li>Add new AnnotationsTestPrinter that prints all annotations to the <strong>raw-tests.txt</strong> file.</li><li>Build execution plan and add <em>clearData</em> command.</li><li>Execute the plan and run adb shell pm clear package where said.</li></ol><h4>Adding &#39;ClearData&#39; annotation</h4><p>We add new annotation under <em>androidTest</em> called ClearData.</p><pre>@Retention(RetentionPolicy.RUNTIME) <br>@Target({ElementType.METHOD}) <br>public <strong>@interface</strong> <strong>ClearData</strong> { }</pre><h4>Extending and printing raw file with new &quot;AnnotationsTestPrinter&quot;</h4><p>When we print all available tests by using -e log true, we trigger built-in InstrumentationRunListeners and each of them print some metadata about the test. So, we can ride on the same logic and add our own class that will print test&#39;s annotations as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e29933e0cffe626f03f40a183bda5f67/href">https://medium.com/media/e29933e0cffe626f03f40a183bda5f67/href</a></iframe><p>Now, we need to run:</p><pre>./scripts/3/raw.sh artifacts/raw-tests.txt</pre><p>Added:<strong><em> -e listener com.sromku.sample.runtests.AnnotationsTestPrinter</em></strong><em><br></em> Android JUnit Runner will run this listener per test.</p><p>The <strong>raw-tests.txt</strong> will look like this:</p><pre>... <br>INSTRUMENTATION_STATUS: current=2 <br>INSTRUMENTATION_STATUS_CODE: 1 <br><strong>INSTRUMENTATION_STATUS: annotations=ClearData,Test </strong>INSTRUMENTATION_STATUS_CODE: 1 <br>INSTRUMENTATION_STATUS: numtests=2 <br>...</pre><p>🔥 You can see a new line there: <strong>INSTRUMENTATION_STATUS: annotations=ClearData,Test</strong></p><h4>Build execution plan</h4><pre>./scripts/3/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><p>The updated script extracts the annotations and takes <em>ClearData</em> into account.</p><p>The printed <strong>execution-plan.txt</strong> will look something like this:</p><pre>com.sromku.sample.runtests.clear.ClearOne#testA <br><strong>clearData </strong><br>com.sromku.sample.runtests.clear.ClearOne#testB</pre><p>You can see the added line <strong>clearData</strong> which means that we need to clear data at this point of time.</p><h4>Run the tests</h4><pre>./scripts/3/run.sh artifacts/execution-plan.txt artifacts</pre><p>This is exactly the same script as <em>scripts/2/run.sh</em> other than a small additions of checking if line==clearData. If yes, clear data by using ADB command and continue to the next line in the execution plan.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33002320cd4b4a50a3f29e80da0d4e58/href">https://medium.com/media/33002320cd4b4a50a3f29e80da0d4e58/href</a></iframe><h3>4. Add ‘Clear notifications’ support</h3><p>Sometimes we just want to clean the notifications bar. It can happen if I need to test that I received a new notification. But before I get one, I must be sure that the notification bar is clean of previous alerts.</p><p>All we need to do is to add ClearNotifications annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f299fe0d256ef4df5652dd0332d5ec63/href">https://medium.com/media/f299fe0d256ef4df5652dd0332d5ec63/href</a></iframe><p>The idea is exactly the same as ClearData:</p><ol><li>We create new annotation and call it ClearNotifications.</li><li>We use already created AnnotationsTestPrinter.</li><li>Build execution plan and add <em>clearNotifications</em> command.</li><li>Execute the plan and clear notifications from ADB. I already wrote about the way of <a href="https://www.sromku.com/blog/android-adb-clear-notifications">cleaning notifications from ADB</a> in my other blog post.</li></ol><h4>Adding “ClearNotifications” annotation</h4><p>We add new annotation under <em>androidTest</em> called ClearNotifications.</p><pre>@Retention(RetentionPolicy.RUNTIME) <br>@Target({ElementType.METHOD}) <br>public <strong>@interface ClearNotifications</strong> { }</pre><p>Run and create <strong>raw_tests.txt</strong>:</p><pre>./scripts/4/raw.sh artifacts/raw-tests.txt</pre><p>The <strong>raw-tests.txt</strong> will look like this:</p><pre>... <br>INSTRUMENTATION_STATUS: current=2 <br>INSTRUMENTATION_STATUS_CODE: 1 <br><strong>INSTRUMENTATION_STATUS: annotations=ClearNotifications,Test </strong>INSTRUMENTATION_STATUS_CODE: 1 <br>INSTRUMENTATION_STATUS: numtests=2 <br>INSTRUMENTATION_STATUS: stream=. <br>INSTRUMENTATION_STATUS: id=AndroidJUnitRunner <br>...</pre><p>🔥 You can see a new line there: <strong>INSTRUMENTATION_STATUS: annotations=ClearNotifications,Test</strong></p><h4>Build execution plan</h4><pre>./scripts/4/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><p>The updated script extracts the annotations and takes <em>ClearNotifications</em> into account.</p><p>The printed <strong>execution-plan.txt</strong> will look something like this:</p><pre>com.sromku.sample.runtests.notif.NotifOne#testA <br><strong>clearNotifications </strong><br>com.sromku.sample.runtests.notif.NotifOne#testB</pre><p>You can see the added line <strong>clearNotifications</strong> which says that we need to clear notifications at this point of time.</p><h4>Run the tests</h4><pre>./scripts/4/run.sh artifacts/execution-plan.txt artifacts</pre><p>This is exactly the same script as <em>scripts/3/run.sh</em> except addition checking if line==clearNotifications. If yes, clear notifications by using ADB command and continue to the next line in the execution plan.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0693e569b8c76a2c58920388179657d/href">https://medium.com/media/e0693e569b8c76a2c58920388179657d/href</a></iframe><p>The cleanNotifications bash command is explained <a href="https://www.sromku.com/blog/android-adb-clear-notifications">here</a>.</p><h3>5. Add parameterized support</h3><p>Just add @Parameterized.Repeat(count = 3) and this test will be executed 3 times with index param: 0,1,2. Get the current running index from the test through: Parameterized.getIndex().</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/039bfc5c6c58089d58f84f45049e4245/href">https://medium.com/media/039bfc5c6c58089d58f84f45049e4245/href</a></iframe><blockquote>In this case, you can even see that I combined it with @ClearData which means that before each test iteration, we will clear app data. Cool! 😀</blockquote><p>Create <strong>raw_tests.txt</strong></p><pre>./scripts/5/raw.sh artifacts/raw-tests.txt</pre><p>The <strong>raw-tests.txt</strong> will look like this:</p><pre>...<br>INSTRUMENTATION_STATUS: current=2<br>INSTRUMENTATION_STATUS_CODE: 1<br><strong>INSTRUMENTATION_STATUS: annotations=Repeat:3,Test</strong><br>INSTRUMENTATION_STATUS_CODE: 1<br>INSTRUMENTATION_STATUS: numtests=3<br>INSTRUMENTATION_STATUS: stream=.<br>INSTRUMENTATION_STATUS: id=AndroidJUnitRunner<br>INSTRUMENTATION_STATUS: test=testB<br>...</pre><p>You can see a new annotation called Repeat and number of iterations, 3, in this case.</p><p>To make this happen, we need to update AnnotationsTestPrinter . Check in the source code for <a href="https://github.com/medisafe/run-android-tests/blob/master/app/src/androidTest/java/com/sromku/sample/runtests/AnnotationsTestPrinter.java">full class implementation</a>. The difference:</p><pre>if (annotation instanceof Parameterized.Repeat) {<br>    Parameterized.Repeat repeat = (Parameterized.Repeat) annotation;<br>    stringBuilder.append(&quot;:&quot; + repeat.count());<br>}</pre><h4>Build execution plan</h4><pre>./scripts/5/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><p>The plan will be like this:</p><pre>clearData <br>com.sromku.sample.runtests.param.ParamOne#testA:0 <br>clearData <br>com.sromku.sample.runtests.param.ParamOne#testA:1 com.sromku.sample.runtests.param.ParamOne#testB:0 com.sromku.sample.runtests.param.ParamOne#testB:1 com.sromku.sample.runtests.param.ParamOne#testB:2</pre><p>Now, each executable test, that should be run with param, will have an iteration number at the end of it. This number will be passed as param to the test when running.</p><h4><strong>Run the tests</strong></h4><pre>./scripts/5/run.sh artifacts/execution-plan.txt artifacts</pre><p>This is how we pass param to the test. Taken from <a href="https://github.com/medisafe/run-android-tests/blob/master/scripts/5/run.sh"><strong>./scripts/5/run.sh</strong></a></p><pre><em>adb </em>shell am instrument -w -e class $test <strong>-e paramIndex $index</strong> $package.test/android.support.test.runner.AndroidJUnitRunner <em>&gt; $runningTest</em></pre><p>And on Java side we call: <strong>Parameterized.getIndex();</strong> The <a href="https://github.com/medisafe/run-android-tests/blob/master/app/src/androidTest/java/com/sromku/sample/runtests/Parameterized.java">implementation of this method</a> just takes our param from <strong>InstrumentationRegistry</strong>.</p><pre>InstrumentationRegistry.<em>getArguments</em>().getString(<strong>&quot;paramIndex<em>&quot;</em></strong>);</pre><h3>6. Run tests by #tags</h3><p>Android already has an option to filter tests by:</p><ol><li>Package name</li><li>Custom Annotation</li><li>Or by test size <a href="https://developer.android.com/reference/android/support/test/filters/SmallTest.html">SmallTest</a><strong> or </strong><a href="https://developer.android.com/reference/android/support/test/filters/MediumTest.html">MediumTest</a><strong> or </strong><a href="https://developer.android.com/reference/android/support/test/filters/LargeTest.html">LargeTest</a></li></ol><p>In our case, we want filter and run tests by multiple <strong>features / tags</strong>. For example, we want choose tests that relate to “registration”, “settings”. Why? because this is the only part we have changed in our code. Or, because we had server issues in the middle of tests and we want to re-run only these sections.</p><p>In addition, we run <strong>sanity</strong> tests on every push to master. In this case, I don’t really care for the tests size and features, but run ALL tests tagged as “sanity”.</p><p>Honestly, I tried to split tests by packages, but eventually I found myself refactoring and repackaging multiple times.</p><p>And for test size, really what is ‘Small’ and what is ‘Large’? Why would I think about such splitting in a real world. If I change some area in my app, then I would run ALL tests related to this area. Just couldn’t find the use case for this.</p><h4>Tags</h4><p>So, eventually we wanted something more flexible. And this is how it looks: We simply add annotation: <strong>Tags(tag = {“sanity”, “small”})</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bb6d7fd40d2b70e3a0042357d3e5d53/href">https://medium.com/media/7bb6d7fd40d2b70e3a0042357d3e5d53/href</a></iframe><p>Create <strong>raw_tests.txt</strong></p><pre>./scripts/6/raw.sh artifacts/raw-tests.txt</pre><p>The <strong>raw-tests.txt </strong>will look like this:</p><pre>...<br>INSTRUMENTATION_STATUS: current=1<br>INSTRUMENTATION_STATUS_CODE: 1<br><strong>INSTRUMENTATION_STATUS: tags=sanity,small</strong><br>INSTRUMENTATION_STATUS_CODE: 1<br>INSTRUMENTATION_STATUS: annotations=Tags,Test<br>...</pre><p>New line: <strong>INSTRUMENTATION_STATUS: tags=sanity,small</strong> 🔥</p><p>To make this happen, we need to update AnnotationsTestPrinter one more time. Check in the source code for <a href="https://github.com/medisafe/run-android-tests/blob/master/app/src/androidTest/java/com/sromku/sample/runtests/AnnotationsTestPrinter.java">full class implementation</a>. The difference:</p><pre>if (annotation instanceof Tags) {                <br>    Tags tags = (Tags) annotation; <br>    tagsStr = <strong>buildTags</strong>(tags.tags());           <br>}</pre><blockquote><strong>buildTags</strong> — method that builds tags array as comma separated string.</blockquote><h4>A. Build execution plan : tags “sanity,extreme”</h4><pre>./scripts/6/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt <strong>-t</strong> <strong>&quot;sanity,extreme&quot;</strong></pre><p>The plan will contain tests that have <strong>at least one</strong> of the filtered tags. In our case we say — Select all tests that have “sanity” <strong>or</strong> “extreme” tag.</p><p>The plan will be:</p><pre>com.sromku.sample.runtests.tags.TagsOne#testA com.sromku.sample.runtests.tags.TagsOne#testB com.sromku.sample.runtests.tags.TagsOne#testD com.sromku.sample.runtests.tags.TagsOne#testE</pre><h4>B. Build execution plan : tags “small”</h4><pre>./scripts/6/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt -t &quot;small&quot;</pre><p>The plan will be:</p><pre>com.sromku.sample.runtests.tags.TagsOne#testA com.sromku.sample.runtests.tags.TagsOne#testE</pre><p>You can see that plans are <strong>different</strong> for different tags 🗄️.</p><h4>Run the tests</h4><pre>./scripts/6/run.sh artifacts/execution-plan.txt artifacts</pre><h3>7. Dump network stats, battery, alarms and more.</h3><p>Once we can run any script before the test execution and after, we can add more ADB commands in a very simple way.</p><p>I added these next dumps:</p><ul><li>netstats</li><li>batterystats</li><li>alarm</li></ul><p>Of course, you can add much more. Check for more options <a href="https://source.android.com/devices/tech/debug/dumpsys.html">here</a>. Or run: adb shell dumpsys -l for a complete list of system services.</p><p>Create <strong>raw-tests.txt</strong></p><pre>./scripts/7/raw.sh artifacts/raw-tests.txt</pre><h4>Build execution plan</h4><pre>./scripts/7/plan.sh artifacts/raw-tests.txt artifacts/execution-plan.txt</pre><h4>Run the tests</h4><pre>./scripts/7/run.sh artifacts/execution-plan.txt artifacts</pre><p>New artifacts added:</p><ul><li>dumpsys_netstats.txt</li><li>dumpsys_batterystats.txt</li><li>dumpsys_alarm.txt</li></ul><p>Added lines in <strong>scripts/7/run.sh</strong> on failed test after we dumb the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8523a9cfa69152fbb47a9b8b1fa5ee8a/href">https://medium.com/media/8523a9cfa69152fbb47a9b8b1fa5ee8a/href</a></iframe><h3>All together</h3><p>Let’s say you have test with multiple params you want to check — like Login and validation. Say you want it after cleared data, and you run this test by tag called “sanity”. In that case, this is all we need to do:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c67060d8a39d4dbfbb237c1f79f8214/href">https://medium.com/media/5c67060d8a39d4dbfbb237c1f79f8214/href</a></iframe><h4>Prepare &amp; Run</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c29ad6ad5408abe73742194e7d161983/href">https://medium.com/media/c29ad6ad5408abe73742194e7d161983/href</a></iframe><p>Example of <strong>artifacts</strong> folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TaQjSPoJed9ykpIFJXic7w.png" /><figcaption>logs, preferences, video, db, dumsys files</figcaption></figure><p>That’s it for now. Your feedback is important ;)</p><p><a href="https://twitter.com/MedisafeApp">Medisafe (@MedisafeApp) | Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53e85e5c8da8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8">Running Android UI Tests - Part 1</a> was originally published in <a href="https://medium.com/medisafe-tech-blog">Medisafe Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>