开发者

How to test activity containing WebView?

开发者 https://www.devze.com 2023-03-13 05:04 出处:网络
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 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

精彩评论

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