WKWebView JS Execution in test target

My team has a set of XCTestCases which create a new instance of WKWebView, execute some javascript with it, then assert some conditions afterward.

Beginning with iOS 16, these tests never succeed because it seems like the WKWebView is experiencing some sort of crash or malfunction in its content process.

When executing a single test of the type above, the following is present in the console:

[Process] 0x142481000 - [PID=0] WebProcessProxy::didFinishLaunching: Invalid connection identifier (web process failed to launch)

[Process] 0x142481000 - [PID=0] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=Crash

[Process] 0x7f7d86008220 - [pageProxyID=6, webPageID=7, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason=Crash

[Loading] 0x7f7d86008220 - [pageProxyID=6, webPageID=7, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason=Crash

[Loading] 0x7f7d86008220 - [pageProxyID=6, webPageID=7, PID=0] WebPageProxy::dispatchProcessDidTerminate: Not eagerly reloading the view because it is not currently visible

I've tried ensuring that an app host is present for the test target these tests execute in and that had no effect. I've tried adding the webview to the apps view hierarchy and that had no effect.

In iOS 15 and prior these same tests execute and pass fine.

Was there some change in iOS 16 that may have caused this or any ideas on how to rectify this?

  • We have the same issue, in the unit-tests of a swift-package. In our case, moving the tests to a test target under the sample app didn't help. FB8885728.

Add a Comment

Accepted Reply

I've figured out a workaround, but still not entirely sure of the root cause.

The fix does, in fact, seem to be that a host app is required when using WKWebView in a unit test. However, it also seems to require loading or importing WebKit in the host app that is hosting the test.

If my host app is written in Swift, it requires an import WebKit or if my host app is written in Objective-C it requires importing WebKit but then also seems to require loading of WebKit by alloc'ing a WKWebView instance in the didFinishLoad of the AppDelegate. My issue was further complicated by using CocoaPods and its functionality to generate a host app if a test target requires it, but a workaround was found.

This behavior doesn't seem to be documented, and is maybe not even intended?

Replies

The same issue with iOS 16

I've figured out a workaround, but still not entirely sure of the root cause.

The fix does, in fact, seem to be that a host app is required when using WKWebView in a unit test. However, it also seems to require loading or importing WebKit in the host app that is hosting the test.

If my host app is written in Swift, it requires an import WebKit or if my host app is written in Objective-C it requires importing WebKit but then also seems to require loading of WebKit by alloc'ing a WKWebView instance in the didFinishLoad of the AppDelegate. My issue was further complicated by using CocoaPods and its functionality to generate a host app if a test target requires it, but a workaround was found.

This behavior doesn't seem to be documented, and is maybe not even intended?