Skip to content

Commit

Permalink
WIP WkWebViewTests
Browse files Browse the repository at this point in the history
  • Loading branch information
lunij committed Mar 22, 2024
1 parent 4f07fa2 commit d06f248
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 96 deletions.
96 changes: 0 additions & 96 deletions Tests/SnapshotTestingTests/SnapshotTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1129,21 +1129,6 @@ final class SnapshotTestingTests: XCTestCase {
""".utf8)
}

#if os(iOS) || os(macOS)
func testWebView() throws {
let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let webView = WKWebView()
webView.load(.init(url: fixtureUrl))
assertSnapshot(
of: webView,
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)),
named: platform
)
}
#endif

func testViewWithZeroHeightOrWidth() {
#if os(iOS) || os(tvOS)
var rect = CGRect(x: 0, y: 0, width: 350, height: 0)
Expand Down Expand Up @@ -1174,87 +1159,6 @@ final class SnapshotTestingTests: XCTestCase {
#endif
}

func testEmbeddedWebView() throws {
#if os(iOS)
let label = UILabel()
label.text = "Hello, Blob!"

let fixtureUrl = URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
let html = try String(contentsOf: fixtureUrl)
let webView = WKWebView()
webView.loadHTMLString(html, baseURL: nil)
webView.isHidden = true

let stackView = UIStackView(arrangedSubviews: [label, webView])
stackView.axis = .vertical

assertSnapshot(
of: stackView,
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)),
named: platform
)
#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(
of: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
}
_ = manipulatingWKWebViewNavigationDelegate
}

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(
of: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
}
_ = cancellingWKWebViewNavigationDelegate
}
#endif

#if os(iOS)
@available(iOS 13.0, *)
func testSwiftUIView_iOS() {
Expand Down
110 changes: 110 additions & 0 deletions Tests/SnapshotTestingTests/Snapshotting/WKWebViewTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import Foundation
import XCTest

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

#if canImport(WebKit)
import WebKit
#endif

#if canImport(UIKit)
import UIKit.UIView
#endif

@testable import SnapshotTesting

final class WKWebViewTests: XCTestCase {
override func setUp() {
super.setUp()
diffTool = "ksdiff"
// isRecording = true
}

override func tearDown() {
isRecording = false
super.tearDown()
}

#if os(iOS) || os(macOS)
func testWebView() throws {
let webView = WKWebView()
webView.load(.init(url: .htmlFixture))
assertSnapshot(
of: webView,
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)),
named: platform
)
}

func testWebViewWithManipulatingNavigationDelegate() throws {
final class ManipulatingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.body.children[0].classList.remove(\"hero\")") // Change layout
}
}
let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate()
let webView = WKWebView()
webView.navigationDelegate = manipulatingWKWebViewNavigationDelegate
webView.load(.init(url: .htmlFixture))
assertSnapshot(
of: webView,
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)),
named: platform
)
_ = manipulatingWKWebViewNavigationDelegate
}

func testWebViewWithCancellingNavigationDelegate() throws {
final class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {
func webView(
_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void
) {
decisionHandler(.cancel)
}
}
let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate()
let webView = WKWebView()
webView.navigationDelegate = cancellingWKWebViewNavigationDelegate
webView.load(.init(url: .htmlFixture))
assertSnapshot(
of: webView,
as: .image(size: .init(width: 800, height: 600)),
named: platform
)
_ = cancellingWKWebViewNavigationDelegate
}
#endif

#if os(iOS)
func testEmbeddedWebView() throws {
let label = UILabel()
label.text = "Hello, Blob!"

let webView = WKWebView()
webView.load(.init(url: .htmlFixture))
webView.isHidden = true

let stackView = UIStackView(arrangedSubviews: [label, webView])
stackView.axis = .vertical

assertSnapshot(
of: stackView,
as: .image(precision: 0.99, perceptualPrecision: 0.99, size: .init(width: 800, height: 600)),
named: platform
)
}
#endif
}

private extension URL {
static var htmlFixture: URL {
URL(fileURLWithPath: String(#file), isDirectory: false)
.deletingLastPathComponent()
.deletingLastPathComponent()
.appendingPathComponent("__Fixtures__/pointfree.html")
}
}
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.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit d06f248

Please sign in to comment.