Skip to content

Fix #440 Webview loading without delegate (option 2) #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions Sources/SnapshotTesting/Common/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,6 @@ extension View {
#if os(iOS) || os(macOS)
if let wkWebView = self as? WKWebView {
return Async<Image> { callback in
let delegate = NavigationDelegate()
let work = {
if #available(iOS 11.0, macOS 10.13, *) {
inWindow {
Expand All @@ -762,7 +761,6 @@ extension View {
return
}
wkWebView.takeSnapshot(with: nil) { image, _ in
_ = delegate
callback(image!)
}
}
Expand All @@ -776,8 +774,14 @@ extension View {
}

if wkWebView.isLoading {
delegate.didFinish = work
wkWebView.navigationDelegate = delegate
var subscription: NSKeyValueObservation?
subscription = wkWebView.observe(\.isLoading, options: [.initial, .new]) { (webview, change) in
subscription?.invalidate()
subscription = nil
if change.newValue == false {
work()
}
}
} else {
work()
}
Expand All @@ -796,22 +800,6 @@ extension View {
#endif
}

#if os(iOS) || os(macOS)
private final class NavigationDelegate: NSObject, WKNavigationDelegate {
var didFinish: () -> Void

init(didFinish: @escaping () -> Void = {}) {
self.didFinish = didFinish
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState") { _, _ in
self.didFinish()
}
}
}
#endif

#if os(iOS) || os(tvOS)
extension UIApplication {
static var sharedIfAvailable: UIApplication? {
Expand Down
74 changes: 74 additions & 0 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,80 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

#if os(iOS) || os(macOS)
final class ManipulatingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.body.children[0].classList.remove(\"hero\")") // Change layout
}
}
func testWebViewWithManipulatingNavigationDelegate() throws {
let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate()
let webView = WKWebView()
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let html = try String(contentsOf: fixtureUrl)
webView.loadHTMLString(html, baseURL: nil)
if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
assertSnapshot(
matching: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
}
_ = manipulatingWKWebViewNavigationDelegate
}

#if os(iOS) || os(macOS)
func testWebViewWithRealUrl() throws {
let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate()
let webView = WKWebView()
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate

webView.load(URLRequest(url: URL(string: "https://www.pointfree.co")!))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loads an actual URL. Is that something you want to prevent in the unit tests? I can image you want to avoid it, but it's the best real life example..

if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
assertSnapshot(
matching: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
}
_ = manipulatingWKWebViewNavigationDelegate
}
#endif

final class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
) {
decisionHandler(.cancel)
}
}
func testWebViewWithCancellingNavigationDelegate() throws {
let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate()
let webView = WKWebView()
webView.navigationDelegate = cancellingWKWebViewNavigationDelegate

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let html = try String(contentsOf: fixtureUrl)
webView.loadHTMLString(html, baseURL: nil)
if !ProcessInfo.processInfo.environment.keys.contains("GITHUB_WORKFLOW") {
assertSnapshot(
matching: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
}
_ = cancellingWKWebViewNavigationDelegate
}
#endif

@available(iOS 13.0, *)
func testSwiftUIView_iOS() {
#if os(iOS)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.