@@ -1061,6 +1061,80 @@ final class SnapshotTestingTests: XCTestCase {
1061
1061
#endif
1062
1062
}
1063
1063
1064
+ #if os(iOS) || os(macOS)
1065
+ final class ManipulatingWKWebViewNavigationDelegate : NSObject , WKNavigationDelegate {
1066
+ func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
1067
+ webView. evaluateJavaScript ( " document.body.children[0].classList.remove( \" hero \" ) " ) // Change layout
1068
+ }
1069
+ }
1070
+ func testWebViewWithManipulatingNavigationDelegate( ) throws {
1071
+ let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate ( )
1072
+ let webView = WKWebView ( )
1073
+ webView. navigationDelegate = manipulatingWKWebViewNavigationDelegate
1074
+
1075
+ let fixtureUrl = URL ( fileURLWithPath: String ( #file) , isDirectory: false )
1076
+ . deletingLastPathComponent ( )
1077
+ . appendingPathComponent ( " __Fixtures__/pointfree.html " )
1078
+ let html = try String ( contentsOf: fixtureUrl)
1079
+ webView. loadHTMLString ( html, baseURL: nil )
1080
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1081
+ assertSnapshot (
1082
+ matching: webView,
1083
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1084
+ named: platform
1085
+ )
1086
+ }
1087
+ _ = manipulatingWKWebViewNavigationDelegate
1088
+ }
1089
+
1090
+ #if os(iOS) || os(macOS)
1091
+ func testWebViewWithRealUrl( ) throws {
1092
+ let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate ( )
1093
+ let webView = WKWebView ( )
1094
+ webView. navigationDelegate = manipulatingWKWebViewNavigationDelegate
1095
+
1096
+ webView. load ( URLRequest ( url: URL ( string: " https://www.pointfree.co " ) !) )
1097
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1098
+ assertSnapshot (
1099
+ matching: webView,
1100
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1101
+ named: platform
1102
+ )
1103
+ }
1104
+ _ = manipulatingWKWebViewNavigationDelegate
1105
+ }
1106
+ #endif
1107
+
1108
+ final class CancellingWKWebViewNavigationDelegate : NSObject , WKNavigationDelegate {
1109
+ func webView(
1110
+ _ webView: WKWebView ,
1111
+ decidePolicyFor navigationAction: WKNavigationAction ,
1112
+ decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void
1113
+ ) {
1114
+ decisionHandler ( . cancel)
1115
+ }
1116
+ }
1117
+ func testWebViewWithCancellingNavigationDelegate( ) throws {
1118
+ let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate ( )
1119
+ let webView = WKWebView ( )
1120
+ webView. navigationDelegate = cancellingWKWebViewNavigationDelegate
1121
+
1122
+ let fixtureUrl = URL ( fileURLWithPath: String ( #file) , isDirectory: false )
1123
+ . deletingLastPathComponent ( )
1124
+ . appendingPathComponent ( " __Fixtures__/pointfree.html " )
1125
+ let html = try String ( contentsOf: fixtureUrl)
1126
+ webView. loadHTMLString ( html, baseURL: nil )
1127
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1128
+ assertSnapshot (
1129
+ matching: webView,
1130
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1131
+ named: platform
1132
+ )
1133
+ }
1134
+ _ = cancellingWKWebViewNavigationDelegate
1135
+ }
1136
+ #endif
1137
+
1064
1138
@available ( iOS 13 . 0 , * )
1065
1139
func testSwiftUIView_iOS( ) {
1066
1140
#if os(iOS)
0 commit comments