Android | Espresso — How to automate Tests for Native Apps involving WebViews?

Suparna Khamaru
4 min readAug 22, 2020

Espresso is known to be the fastest automation tool in the industry used for testing of native android apps.

The purpose of this article is not only to educate readers but also, help teams overcome a common mistake that they might encounter while automating native android apps using Espresso.

UI Automator to be used in addition to the above in Espresso test framework

Problem Statement

What to do when you are unable to detect any element on webViews within the app?

What to do if the entire webView in the app is found displaying blank data when inspected and no web elements are found to be detectable for any further automation in the webView?

Well, a Test Professional always believes and knows there has to be a way to automate almost everything in the app, when you are using a powerful tool like Espresso in the project. 😃

Solution

One common reason for inability of detection or inspection of web-view elements within the native app is because web-view itself is treated as a second app (Chrome / Web) and Espresso is for single app testing.

That means, we have to combine UI Automator & Web inside Espresso framework.

It may sound little weird in the beginning, but works perfectly fine when combined. Off-course UI Automator has a separate set of syntaxes that pretty much matches a lot with Selenium syntaxes, but thats okay right? At least there’s a way to automate everything that deals with the android apps testing!!

This can be little frustrating in some cases to a point where team may start believing that the webViews in native apps cannot be tested at all using Espresso — which is not true as this article debunks that mindset now.

Step by Step Implementation of the Solution

Pre-conditions & Assumptions:

The existing Espresso test framework should be in working condition while automating tests on native android app, before attempting the below. Yes, this is the next level.. 😬

1. Make sure to go to app/build.gradle in the android project, and add espresso libraries for uiautomator as well as web as highlighted below

2. Go to dependencies.gradle and add web in the existing test directories for respective version usages

3. By making the above 2 additions in the android project, by now the WebViews shall no longer remain undetectable — that means any element on the webView can now be detected in your automation scripts too - Yay! 🥳

4. Now its time to see if the above can be scripted along with the existing Espresso framework

We navigate to the page object class in the test framework, and start scripting as per ui-automator syntaxes on the existing espresso test framework itself.

public class demo {

private final UiSelector select = new UiSelector();    
private final UiDevice device;

}

As can be observed, from the above code snippet, which is pretty much self-explanatory to a major extent, also works and aligns perfectly fine in the existing espresso framework.

5. Let us have a look at the Test Class now

Pattern of writing a test while using Fluent page object design pattern in test class:

Above testing style could be a controversial subject in the developer community. However, is very encouraged in the testing community and is widely used as a preferred way of replicating end user journeys, especially while automating Large & Medium tests.

Thats how we solved the “webView not getting inspected” issue in one of our android project few months back.

Let me know, if your team faced the same challenge when inspecting and automating on webViews while using Espresso and thought it was not possible too!!

Thanks for reading this article! Leave a comment below if you have any questions. Be sure to click on the 👏 icon to let me know that you encourage my writing.

Connect with me:

--

--