@@ -1111,6 +1111,80 @@ final class SnapshotTestingTests: XCTestCase {
1111
1111
#endif
1112
1112
}
1113
1113
1114
+ #if os(iOS) || os(macOS)
1115
+ final class ManipulatingWKWebViewNavigationDelegate : NSObject , WKNavigationDelegate {
1116
+ func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
1117
+ webView. evaluateJavaScript ( " document.body.children[0].classList.remove( \" hero \" ) " ) // Change layout
1118
+ }
1119
+ }
1120
+ func testWebViewWithManipulatingNavigationDelegate( ) throws {
1121
+ let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate ( )
1122
+ let webView = WKWebView ( )
1123
+ webView. navigationDelegate = manipulatingWKWebViewNavigationDelegate
1124
+
1125
+ let fixtureUrl = URL ( fileURLWithPath: String ( #file) , isDirectory: false )
1126
+ . deletingLastPathComponent ( )
1127
+ . appendingPathComponent ( " __Fixtures__/pointfree.html " )
1128
+ let html = try String ( contentsOf: fixtureUrl)
1129
+ webView. loadHTMLString ( html, baseURL: nil )
1130
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1131
+ assertSnapshot (
1132
+ matching: webView,
1133
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1134
+ named: platform
1135
+ )
1136
+ }
1137
+ _ = manipulatingWKWebViewNavigationDelegate
1138
+ }
1139
+
1140
+ #if os(iOS) || os(macOS)
1141
+ func testWebViewWithRealUrl( ) throws {
1142
+ let manipulatingWKWebViewNavigationDelegate = ManipulatingWKWebViewNavigationDelegate ( )
1143
+ let webView = WKWebView ( )
1144
+ webView. navigationDelegate = manipulatingWKWebViewNavigationDelegate
1145
+
1146
+ webView. load ( URLRequest ( url: URL ( string: " https://www.pointfree.co " ) !) )
1147
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1148
+ assertSnapshot (
1149
+ matching: webView,
1150
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1151
+ named: platform
1152
+ )
1153
+ }
1154
+ _ = manipulatingWKWebViewNavigationDelegate
1155
+ }
1156
+ #endif
1157
+
1158
+ final class CancellingWKWebViewNavigationDelegate : NSObject , WKNavigationDelegate {
1159
+ func webView(
1160
+ _ webView: WKWebView ,
1161
+ decidePolicyFor navigationAction: WKNavigationAction ,
1162
+ decisionHandler: @escaping ( WKNavigationActionPolicy ) -> Void
1163
+ ) {
1164
+ decisionHandler ( . cancel)
1165
+ }
1166
+ }
1167
+ func testWebViewWithCancellingNavigationDelegate( ) throws {
1168
+ let cancellingWKWebViewNavigationDelegate = CancellingWKWebViewNavigationDelegate ( )
1169
+ let webView = WKWebView ( )
1170
+ webView. navigationDelegate = cancellingWKWebViewNavigationDelegate
1171
+
1172
+ let fixtureUrl = URL ( fileURLWithPath: String ( #file) , isDirectory: false )
1173
+ . deletingLastPathComponent ( )
1174
+ . appendingPathComponent ( " __Fixtures__/pointfree.html " )
1175
+ let html = try String ( contentsOf: fixtureUrl)
1176
+ webView. loadHTMLString ( html, baseURL: nil )
1177
+ if !ProcessInfo. processInfo. environment. keys. contains ( " GITHUB_WORKFLOW " ) {
1178
+ assertSnapshot (
1179
+ matching: webView,
1180
+ as: . image( size: . init( width: 800 , height: 600 ) ) ,
1181
+ named: platform
1182
+ )
1183
+ }
1184
+ _ = cancellingWKWebViewNavigationDelegate
1185
+ }
1186
+ #endif
1187
+
1114
1188
@available ( iOS 13 . 0 , * )
1115
1189
func testSwiftUIView_iOS( ) {
1116
1190
#if os(iOS)
0 commit comments