Hello, espresso! Part 5 Automating WebViews 🕸️🌎
In the last part Hello, espresso! Part 4 Working with Idling resources 😴, We understood how to use espresso idling resources to achieve synchronization when the app is running background tasks that espresso is not aware of. Go ahead and have a read in case you missed it.
Automating Hybrid apps with WebViews
Switching gears to a different topic now, many android apps expose certain business logic in WebViews
within Native apps. You can think of it as a mobile browser like chrome/firefox that is able to render web pages.
Its quite important to cover the interaction your native app has with these Web components to ensure there are no missed regressions in your app releases.
Some common examples when an app might have a WebView are:
- There may be help or support pages for your business that needs to be dynamic and change whenever a new page/option is added
- Or pages to host marketing content is launched
- Or even lengthy terms and conditions for a new feature
Having a WebView is great since its much faster to make changes and roll out web app changes to production and developers are not restricted with Google play store approvals and roll outs
What tests to cover and what not? 🤔
Espresso framework provides a library espresso-web
that provides an "espresso like" API interface overSelenium WebDriver API
When should you write an espresso web test? ✅
If your app has user journeys between native app and webview, and you want to make sure these interactions work then it surely makes sense to cover this as part of an Espresso test
When you should not? ❌
However, if you only want to verify the WebView
content or functionality without any native interaction checks, then its much better to write an automated Web Test instead using Selenium WebDriver or equivalent library as that would be much faster and easier to write, maintain and run
How does this work?
- Espresso web uses a Javascript bridge to interact with the WebDriver framework and usesatoms
- You can consider atoms as equivalent to
ViewMatcher
andViewAction
classes - Espresso web wraps these atoms with
Web
andWeb.WebInteraction
classes that makes writing an Espresso web test feel similar to writing a test for a native app
we use findElement()
or getElement()
methods with certain locators (like ID, XPATH, CSS, CLASS NAME etc) to find an element and then perform an action or assertion on top of it
Writing your first Web test
Let’s write a test to see this in action
Adding dependencies
We need to add espresso-web
to our app/build.gradle
file
And add below library versions in root build.gradle
file
Understanding app under test
Let’s understand the app flows that we would be automating before we jump into the code
Below screens show the app for above scenarios:
Above Figure: WHEN the user enters a text in textbox
Above Figure: AND the user taps on “Change Text” button THEN the label displays the entered test
Above Figure: THEN the web page redirects to another page with a label displaying the entered test
Finding locators using Chrome debug tools
If you try to use the Android studio “Layout Inspector”, then you’ll be disappointed to see that it does not show the Component tree like native apps to enable us to find the desired locator, it only shows the containing WebView
Above Figure: Trying to use layout inspector
So, whats the solution?
We need to use Chrome remote debugging tool to inspect our app.
Follow below steps:
- Connect your device or emulator via USB
- Ensure developer options are enabled
- Verify that USB debugging is enabled and the connected laptop trusts your android device
Once done, you can type chrome://inspect
in your chrome browser tab and this would open chrome debugging tools like below, Tap on Inspect button
Above Figure: Chrome inspect devices screen
Once you tap on inspect it should bring up Chrome developer tools window and you can go to Elements tab and then use the inspect button to look at the HTML/CSS structure of the web page
Above Figure: Chrome dev tools showing the webview and its associated DOM for home page
Once you enter some text and tap on “Change text and submit” button, you’ll see a screen like below
Above Figure: Chrome dev tools showing the 2nd web page after user taps on “Change text and Submit”
Enabling Chrome debugging + JavaScript (JS) Execution on the app
We need to add few lines in our app source code to enable chrome debugging.
Please ensure you add the line: WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG);
in your MainActivity, this would enable Chrome debug tools to inspect your WebView only for debug versions of the app
This method was also added in Android Kit kat thus we need to add an annotation like@RequiresApi(api = Build.VERSION_CODES.KITKAT)
to our onCreate
method as well
We also add additional logic to enable JS execution
Below you can see the complete onCreate()
method
If you are not familiar with how to find web locators, search with keywords “locators xpath” or “locators css” in google and you should be able to find tons of resources.
Alternatively, You could refer to Selenium Tips: Better Locators in Selenium post from Sauce Labs as a starter
Let’s write our test
You can see the complete test below:
Let’s unpack this and understand in more detail
We start by launching our Activity using ActivityScenarioRule
and specify it as a JUnit rule
Since we are using Javascript to drive the browser, we need to enable it on the WebView, To do so, we’ll add a method to run before any test and annotate it with @Before
To start our web test, we first specify the WebView
we want to work with using the onWebView()
method
If we had a single WebView on the activity then we could skip adding the
withId()
method (Here we keep it to be specific), this method returns aWebInteraction
object that exposes the Web API actions to drive ourWebView
Next, we want to be able to find our text box
- We do so by using
withElement()
method that takes anAtom<ElementReference>
as input to find an element, - We use
findElement()
that takes first argument as the Locator, followed by the actual locator
We could use any of the below locators to identify our elements:
The full statement looks like below:
We want to make sure we clear any existing text from the text box and type a desired text, we do so using clearElement()
and webKeys(text)
method:
We then use similar methods to find the button element and perform a click on it
And finally, we check that the label has the desired text that we entered by adding:
If you notice, most of the methods have a similar structure to native app espresso actions and assertions, most of our methods have “web” prefix, you could use this intuition to find the relevant methods using Android studio for your specific test cases
We can then easily write the 2nd test using similar commands (see the above)
If we run both these tests, we should see them launch our activity and drive our WebView
similar to a native app. Hopefully you understand the general structure well enough to go ahead and write your own tests.
Resources 📘
- You can find the app and test code for this post on Github under
automationhacks/testing-samples
: - Please read Espresso Web on android developers for some more context
Conclusion ✅
Hopefully, this post gives you an idea of how to work with WebViews
in espresso. Stay tuned for the next post where we'll dive into how to automate and work with WebViews with espresso
As always, Do share this with your friends or colleagues and if you have thoughts or feedback, I’d be more than happy to chat over on Twitter or comments. Until next time. Happy Testing and learning.
Originally published at https://automationhacks.io on May 19, 2022.