You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to snapshot several SwiftUI views, some of which contain a WKWebView subclass. Now I've been trying to follow every piece of advice on the topic, however when the WebView is a subview I can't seem to get the snapshot (I get a test timeout).
Here's the code that works:
lethtml="..."letexp1=XCTestExpectation(description:"webview loaded")letsvgview=WKWebView(frame:CGRect(origin:.zero, size:CGSize(width:392, height:852)))letd=NavigationDelegate()
d.callback ={
exp1.fulfill()}
svgview.navigationDelegate = d
svgview.loadHTMLString(html, baseURL:nil)awaitfulfillment(of:[exp1], timeout:5) // without this, the test would timeout
letfailure=verifySnapshot(of: svgview, as:.image)
// ...
plus the Snapshotting extension:
classNavigationDelegate:NSObject,WKNavigationDelegate{varcallback:(()->Void)?func webView(_ webView:WKWebView, didFinish navigation:WKNavigation!){self.callback?()}}extensionSnapshottingwhere Value ==WKWebView, Format ==UIImage{publicstaticletimage=Snapshotting(
pathExtension:"png",
diffing:.image,
asyncSnapshot:{ webView inAsync{ callback inletconfig=WKSnapshotConfiguration()
config.afterScreenUpdates =false // magical flag, if it's not `false` the test would timeout
webView.takeSnapshot(with: config){ image, error iniflet image = image {callback(image)}iflet error = error {print("Could not take snapshot: \(error)")}}}})}
Using Xcode 16.4, Swift 5.9, swift-snapshot-testing 1.18.4
Any pointers to what changes are needed to get this setup working for any WKWebView subclass that is a subview of the main view being snapshotted?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to snapshot several SwiftUI views, some of which contain a WKWebView subclass. Now I've been trying to follow every piece of advice on the topic, however when the WebView is a subview I can't seem to get the snapshot (I get a test timeout).
Here's the code that works:
plus the Snapshotting extension:
Using Xcode 16.4, Swift 5.9, swift-snapshot-testing 1.18.4
Any pointers to what changes are needed to get this setup working for any WKWebView subclass that is a subview of the main view being snapshotted?
Beta Was this translation helpful? Give feedback.
All reactions