Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Espresso wait for WebView to finish loading

Is there a reliable way to make Espresso wait for WebViews to finish loading?

I've tried the approach outlined here but found it unreliable. It also has other drawbacks:

  • It relies on replacing the WebView's WebChromeClient. Any existing WebChromeClient can't be wrapped either, since WebViewrt doesn't have a getWebChromeClient() method for some reason.
  • It requires a specific WebView instance, so every time I start an Activity with a WebView I have to get the WebView instance and register a new WebviewIdlingResource for it.

I'm hoping someone has a solution without any of these drawbacks. I had hopes that the espresso-web package might provide a solution, but it doesn't appear to offer anything relating to loading.

like image 852
vaughandroid Avatar asked Aug 18 '15 10:08

vaughandroid


People also ask

What is the use of WebView in Android?

WebView is a special view provided by android to display web pages inside the application. WebView does not provide all the features of a full-fledged browser application like chrome and firefox. However, it provides complete control over the content to be shown and exposes all the android features to be invoked inside the web pages.

How do I handle waiting for a view to show up?

While waiting for a view to show up is usually handled by espresso itself, there might be cases where your app is doing work on another thread (fetching data from a server, calculating something) and therefore espresso does not know about it. There are different ways to handle this, most commonly idling resources.

How to test a WebView using atom?

onWebView () − Similar to onView (), it exposes a set of API to test a WebView. withElement () − One of the several methods used to locate web elements inside a web page using Atom and returns WebInteration object, which is similar to ViewInteraction. perform () − Executes the action inside a web page using Atom and returns WebInteraction.


1 Answers

As you mentioned, there is an espresso add-on called espresso-web for dealing with webviews and their content.

"finish loading" is a bit of a vague concept. It will depend on what specifically to you want to finish.

If you use onWebView().check(<the you want to finish is in your webview>) that will return only after the web view has loaded and the check has succeeded.

Espresso-web is an entry point to work with WebViews on Android. It uses Atoms from the popular WebDriver API to introspect into and control the behavior of a WebView.

Similar to onData, WebView interactions are actually composed of several View Atoms. An Atom can be seen as a ViewAction, a self contained unit which performs an action in your UI. However, they need to be properly orchestrated and are quite verbose. Web and WebInteraction wrap this boilerplate and give an Espresso-like feel to interacting with WebViews.

https://developer.android.com/training/testing/espresso/web

like image 90
yogurtearl Avatar answered Oct 14 '22 06:10

yogurtearl