Android UI testing with magical tips

Manish Sharma
CodeX
Published in
4 min readMar 23, 2021

There is a big difference between a developer and an efficient developer. A developer can write a code which is working but an efficient developer not just write a working code but also uses powerful tools which improves the overall quality of work and efficiency of the involved stakeholders. There are so many powerful tools out there by which a developer can reduce his grunt work to a great extent but if you are not aware of those tools you are just wasting your time in doing stuff that you are not supposed to do.

As a good developer your code should be robust enough that QA/testing team should literally pass the build without raising a single issue. Is it possible??? Yes undoubtedly, even in some companies there are no QAs/Testing team at all, they believes in that a developer is QA and QA is developer. So question is how are they managing? Do such developments (where developer is doing QA) took so much of time to release? So to answer this let me tell you clearly that these developers are efficient developers and they are aware of the tools and frameworks which empower their work.

What are those tools or frameworks which a mobile platform developer should know for covering test cases and UI testing?

Let’s talk about Android, if you are developing android app using Java or Kotlin then there are testing frameworks available which takes care of unit testing and instrumental testing i.e Junit ,espresso etc. Now when you know the details of your feature that you are going to develop do not ever jump to coding immediately, plan your code first by thinking how should you write the functions such that you can involve them in your unit testing. It become very difficult to involve unit testing in a code where unit testing was not foreseen and then a developer starts re-writing their code to support unit testing or end writing unit tests which are not worth using.

Instrumental testing in android using espresso is very much faster if you are aware of Espresso Test plugin. There are two options for writing instrumental tests one is by writing whole code by yourself which takes huge time while if you use Espresso test plugin then it takes few minutes. For me it took 2 days for writing UI test for covering user journey where as when I used this Espresso Test plugin for recording my test it took around 30–35 minutes.

How to use Espresso in Android for unit testing and Instrumental testing?

In your app’s build.gradle file make these changes:

in defaultConfig make these changes:

defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"..

}

in your dependencies make these changes:

dependencies {
...

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


}

You are all set for running your unit and instrumental tests. Clean, build and Run your android project on a physical device or on an emulator. Now enable Test recorder plugin by going to Preferences>Plugin and search for Test recorder and then enable it. Refer screenshot below:

Enable Test recorder plugin

Magical tip

With the help of Record espresso test you will be able to mock your app as an user is using your app by doing interactions.

Record espresso test

Above mentioned step will auto generate code for your project be it in Java or Kotlin.

Java code generated automatically after enabling Record espresso test

Cherry on the cake tip

Since you are mocking your app so the waiting time or pause you are taking between two interactions is not registering or not recording so you need to provide some waiting time in your code before or after any performClick. So if your action requires some waiting time like waiting for results from an api call or animation transition time so you need to provide waiting time manually to view that actual behaviour.

appCompatButton.perform(click());Thread.sleep(1000);
//Adding thread.sleep will wait 1 second before clicking back button and it will be visible to your eyes
pressBack();

--

--

Manish Sharma
CodeX
Writer for

Currently working in Flutter framework. Being into mobile apps development since 2012 mostly Android but also developed apps and libraries for iOS and Windows