Skip to content

Commit

Permalink
Fix #440 Webview loading without delegate (option 2) (#443)
Browse files Browse the repository at this point in the history
* Fix #440 - Improve WKWebView navigation delegate

* Fix #440 - Replace WKWebView navigation delegate by observing wkWebView.isLoading

Co-authored-by: Tieme van Veen <Tieme.vanVeen@triodos.com>
Co-authored-by: Stephen Celis <stephen@stephencelis.com>
  • Loading branch information
3 people authored Aug 16, 2021
1 parent 012fd98 commit 6bc120b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 20 deletions.
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")!))
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.

0 comments on commit 6bc120b

Please sign in to comment.