Testing an Activitys WebView using Robolectric, not loading page

2.7k Views Asked by At

I'm trying to test out some content in a WebView using Robolectric, however there is no data coming into it.

I have called the following before loadUrl() on the WebView.

Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
Robolectric.getFakeHttpLayer().interceptResponseContent(true);

And I have also called the following in order to try and flush whatever is queued up in Robolectric-land. I do have to do this in order for me to get a few other things to kick through.

Robolectric.runBackgroundTasks();
Robolectric.runUiThreadTasks();
Robolectric.runUiThreadTasksIncludingDelayedTasks();

Here is the code that loads the url

    WebView webView = new WebView(this);

    webView.getSettings().setUseWideViewPort(false);
    webView.getSettings().setSupportMultipleWindows(false);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(false);
    webView.getSettings().setDomStorageEnabled(false);
    webView.getSettings().setLoadWithOverviewMode(true);

    webView.setInitialScale(1);

    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int newProgress) {
           super.onProgressChanged(view, newProgress);

           Log.d(TAG, newProgress + " URL: " + view.getUrl());//this never happens!
        }
    });

    webView.loadUrl("https://the-url.to/load");

    Log.d(TAG, "Loading: " + url);//i see this..

I really like Robolectric and how it solves the emulator overhead problems, but I need it to work with the WebView implementation above. Any ideas?

1

There are 1 best solutions below

2
On

What will you test? Maybe it's enough to check, that the loadurl was called with expected url.

looks like more is not possible current. https://github.com/robolectric/robolectric/blob/8c85d5ba50b1e06fd2918d48ad6c6487e8340750/robolectric/src/main/java/org/robolectric/shadows/ShadowWebView.java