3

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?

Tyler
  • 19,113
  • 19
  • 94
  • 151

1 Answers1

2

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

nenick
  • 7,340
  • 3
  • 31
  • 23
  • I was hoping to inject click events on DOM elements, and then validate the behavior. – Tyler Nov 19 '14 at 15:50
  • Perhaps can you use an extra tool for testing web view content. I'm not so familiar with web test but found http://htmlunit.sourceforge.net – nenick Nov 19 '14 at 17:57