开发者

How to test activity containing WebView?

I have activity which contains WebView loading some url from Internet, I'd like to ensure that expected HTML is present on WebView, how to get this content? I know its possible using JavaScript trick,开发者_StackOverflow中文版 but I don't like to put tricks in production code...


You can create a custom WebViewClient for your WebView. Then you can override shouldInterceptRequest and/or shouldOverrideUrlLoading. This will let you know about all of the network traffic (page loads, Ajax requests, plus more mundane things like images and CSS being loaded) to the page. You could then log this on a debug or info setting, so it is only being logged in development, not in the production build (assuming you set the log levels appropriately for a release build.) You could also build unit test assertions based on the info provided in these methods. Overriding these two methods can also provide you hooks for loading test pages for unit testing purposes. Similarly, you can create a custom WebChromeClient for your WebView. You can override its onConsoleMessage to direct JS logging and warnings/errors to your normal Android log.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜