How to use tableView method of CancellingWKWebViewNavigationDelegate class

Best Swift-snapshot-testing code snippet using CancellingWKWebViewNavigationDelegate.tableView

SnapshotTestingTests.swift

Source:SnapshotTestingTests.swift Github

copy

Full Screen

...318    #if os(iOS)319    class TableViewController: UITableViewController {320      override func viewDidLoad() {321        super.viewDidLoad()322        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")323      }324      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {325        return 10326      }327      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {328        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)329        cell.textLabel?.text = "\(indexPath.row)"330        return cell331      }332    }333    let tableViewController = TableViewController()334    assertSnapshot(matching: tableViewController, as: .image(on: .iPhoneSe))335    #endif336  }337  func testAssertMultipleSnapshot() {338    #if os(iOS)339    class TableViewController: UITableViewController {340      override func viewDidLoad() {341        super.viewDidLoad()342        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")343      }344      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {345        return 10346      }347      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {348        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)349        cell.textLabel?.text = "\(indexPath.row)"350        return cell351      }352    }353    let tableViewController = TableViewController()354    assertSnapshots(matching: tableViewController, as: ["iPhoneSE-image" : .image(on: .iPhoneSe), "iPad-image" : .image(on: .iPadMini)])355    assertSnapshots(matching: tableViewController, as: [.image(on: .iPhoneX), .image(on: .iPhoneXsMax)])356    #endif357  }358  func testTraits() {359    #if os(iOS) || os(tvOS)360    if #available(iOS 11.0, tvOS 11.0, *) {361      class MyViewController: UIViewController {362        let topLabel = UILabel()363        let leadingLabel = UILabel()364        let trailingLabel = UILabel()365        let bottomLabel = UILabel()366        override func viewDidLoad() {367          super.viewDidLoad()368          self.navigationItem.leftBarButtonItem = .init(barButtonSystemItem: .add, target: nil, action: nil)369          self.view.backgroundColor = .white...

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1import UIKit2import WebKit3class ViewController: UIViewController, WKNavigationDelegate {4    override func loadView() {5        webView = WKWebView()6    }7    override func viewDidLoad() {8        super.viewDidLoad()9        webView.load(URLRequest(url: url))10    }11}12extension ViewController: CancellingWKWebViewNavigationDelegate {13    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {14        print("URL: \(navigationAction.request.url?.absoluteString ?? "")")15        decisionHandler(.allow)16    }17}18import UIKit19import WebKit20class ViewController: UIViewController, WKNavigationDelegate {21    override func loadView() {22        webView = WKWebView()23    }24    override func viewDidLoad() {25        super.viewDidLoad()26        webView.load(URLRequest(url: url))27    }28}29extension ViewController: CancellingWKWebViewNavigationDelegate {30    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {31        print("URL: \(navigationAction.request.url?.absoluteString ?? "")")32        decisionHandler(.allow)33    }34}

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1import UIKit2import WebKit3class ViewController: UIViewController {4    override func viewDidLoad() {5        super.viewDidLoad()6    }7}8extension ViewController: UITableViewDataSource, UITableViewDelegate {9    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {10    }11    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {12        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)13    }14    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {15        let vc = CancellingWKWebViewNavigationDelegate()16        self.navigationController?.pushViewController(vc, animated: true)17    }18}19import UIKit20import WebKit21class CancellingWKWebViewNavigationDelegate: UIViewController, WKNavigationDelegate {22    override func viewDidLoad() {23        super.viewDidLoad()24        webView = WKWebView()25        webView.load(URLRequest(url: url))26    }27    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {28        print("Start to load")29    }30    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {31        print("finish to load")32    }33    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {34        print("error to load")35    }36}37import UIKit38import WebKit39class CancellingWKWebViewNavigationDelegate: UIViewController, WKNavigationDelegate {40    override func viewDidLoad() {41        super.viewDidLoad()42        webView = WKWebView()43        webView.load(URLRequest(url: url))44    }45    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {46        print("Start to load")47    }

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {2    let vc = CancellingWKWebViewNavigationDelegate(url: url)3    navigationController?.pushViewController(vc, animated: true)4}5override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {6    let vc = CancellingWKWebViewNavigationDelegate(url: url)7    navigationController?.pushViewController(vc, animated: true)8}9override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {10    let vc = CancellingWKWebViewNavigationDelegate(url: url)11    navigationController?.pushViewController(vc, animated: true)12}13override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {14    let vc = CancellingWKWebViewNavigationDelegate(url: url)15    navigationController?.pushViewController(vc, animated: true)16}17override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {18    let vc = CancellingWKWebViewNavigationDelegate(url: url)19    navigationController?.pushViewController(vc, animated: true)20}21override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {22    let vc = CancellingWKWebViewNavigationDelegate(url: url)23    navigationController?.pushViewController(vc, animated: true)24}25override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {2    if indexPath.section == 0 {3        let vc = CancellingWKWebViewNavigationDelegate()4        navigationController?.pushViewController(vc, animated: true)5    }6}7import UIKit8import WebKit9class CancellingWKWebViewNavigationDelegate: UIViewController {10    override func loadView() {11        webView = WKWebView()12    }13    override func viewDidLoad() {14        super.viewDidLoad()15        webView.load(URLRequest(url: url))16    }17}18extension CancellingWKWebViewNavigationDelegate: WKNavigationDelegate {19    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {20        if let host = url?.host {21            if host.contains("hackingwithswift.com") {22                decisionHandler(.allow)23            }24        }25        decisionHandler(.cancel)26    }27}

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1class ViewController: CancellingWKWebViewNavigationDelegate, WKNavigationDelegate {2    override func viewDidLoad() {3        super.viewDidLoad()4        tableView.reloadData()5    }6    override func viewDidAppear(_ animated: Bool) {7        super.viewDidAppear(animated)8    }9    override func viewWillAppear(_ animated: Bool) {10        super.viewWillAppear(animated)11    }12    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {13        print("didStartProvisionalNavigation")14        tableView.reloadData()15    }16    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {17        print("didCommit")18        tableView.reloadData()19    }20    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {21        print("didFinish")22        tableView.reloadData()23    }24    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {25        print("didFailProvisionalNavigation")26        tableView.reloadData()27    }28    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {29        print("didFail")30        tableView.reloadData()31    }32}33class ViewController: CancellingWKWebViewNavigationDelegate, WKNavigationDelegate {34    override func viewDidLoad() {35        super.viewDidLoad()36        tableView.reloadData()37    }38    override func viewDidAppear(_ animated: Bool) {39        super.viewDidAppear(animated)40        tableView.reloadData()41    }42    override func viewWillAppear(_ animated: Bool) {43        super.viewWillAppear(animated)44        tableView.reloadData()45    }46    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {47        print("didStartProvisionalNavigation")48        tableView.reloadData()49    }50    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {51        print("didCommit")52        tableView.reloadData()53    }54    func webView(_ webView: WKWebView,

Full Screen

Full Screen

tableView

Using AI Code Generation

copy

Full Screen

1override func viewDidLoad() {2  super.viewDidLoad()3  let myRequest = URLRequest(url: myURL!)4  webView.load(myRequest)5  webView.navigationDelegate = CancellingWKWebViewNavigationDelegate()6}7class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {8  func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {9    print("decidePolicyFor navigationAction")10    decisionHandler(.cancel)11  }12}13class CancellingWKWebView: WKWebView {14  override init(frame: CGRect, configuration: WKWebViewConfiguration) {15    super.init(frame: frame, configuration: configuration)16  }17  required init?(coder: NSCoder) {18    super.init(coder: coder)19    let webConfiguration = WKWebViewConfiguration()20    let userContentController = WKUserContentController()21    var meta = document.createElement('meta');22    meta.setAttribute('name', 'viewport');23    meta.setAttribute('content', 'width=device-width');24    document.getElementsByTagName('head')[0].appendChild(meta);25    let userScript = WKUserScript(source: script, injectionTime: .atDocumentEnd, forMainFrameOnly: true)26    userContentController.addUserScript(userScript)27  }28}29class CancellingWKWebViewConfiguration: WKWebViewConfiguration {30  override init() {31    super.init()32    let userContentController = WKUserContentController()33    var meta = document.createElement('meta');34    meta.setAttribute('name', 'viewport');35    meta.setAttribute('content', 'width=device-width');36    document.getElementsByTagName('head')[0].appendChild(meta);37    let userScript = WKUserScript(source: script, injectionTime: .atDocumentEnd, forMainFrameOnly: true)38    userContentController.addUserScript(userScript)39  }40  required init?(coder: NSCoder) {41    fatalError("init(coder:) has not been implemented")

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful