How to test WKWebview

Now I'm writing the unit tests. There is a problem to test the WKWebView. Do you know how to execute the navigationDelegate methods. I have tried like this, but it crashed. Could you please help me? Thanks in advance. let wkNavigation = WKNavigation() let error = NSError() vc.wkWebView?.navigationDelegate?.webView!(vc.wkWebView!, didFail: wkNavigation, withError: error)

I have tried like this, but it crashed.

That’s kinda vague. Which line specifically crashed? And what did the crash look like?

Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

The crash is bellow.

Thread 1: EXC_BAD_ACCESS(code=1,address=0x0)

The crash is bellow.

Below what?

Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

[For those reading along at home, the above conversation is a little confusing because leaf1 edited their post to fix the problem I was concerned with.]

That’s not what I was looking for, alas. Rather, I was wondering where this crash occurred in your source code (specifically with reference to the three statements you showed in your original post).

Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
    func testWKNavigation() {
        _ = WKNavigation()
    }


This is enough to get the crash: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)

I don't know all the technicalities behind WKNavigation implementation, but it appears that as soon as the control exits the current scope, WKNavigation object gets deallocated automatically -- as if it's set to auto-release itself, and that's why you get the crash. The method signature expects to get a non-nil navigation object (it's explicitly unwrapped).

You need to declare your instance of WKNavigation in a scope that out lives the scope of the test function itself. This will work, though:

Code Block swift
class MyTestCase: XCTestCase {
let navigation = WKNavigation()
func testDelegate() {
// some assertions
delegate.webView(webView, didFail: navigation, withError: error)
// some assertions
}
}



How to test WKWebview
 
 
Q