How to use viewWillDisappear method of CancellingWKWebViewNavigationDelegate class

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

SnapshotTestingTests.swift

Source:SnapshotTestingTests.swift Github

copy

Full Screen

...750    class ViewController: UIViewController {751      let viewDidLoadExpectation: XCTestExpectation752      let viewWillAppearExpectation: XCTestExpectation753      let viewDidAppearExpectation: XCTestExpectation754      let viewWillDisappearExpectation: XCTestExpectation755      let viewDidDisappearExpectation: XCTestExpectation756      init(viewDidLoadExpectation: XCTestExpectation,757           viewWillAppearExpectation: XCTestExpectation,758           viewDidAppearExpectation: XCTestExpectation,759           viewWillDisappearExpectation: XCTestExpectation,760           viewDidDisappearExpectation: XCTestExpectation){761        self.viewDidLoadExpectation = viewDidLoadExpectation762        self.viewWillAppearExpectation = viewWillAppearExpectation763        self.viewDidAppearExpectation = viewDidAppearExpectation764        self.viewWillDisappearExpectation = viewWillDisappearExpectation765        self.viewDidDisappearExpectation = viewDidDisappearExpectation766        super.init(nibName: nil, bundle: nil)767      }768      required init?(coder: NSCoder) {769        fatalError("init(coder:) has not been implemented")770      }771      override func viewDidLoad() {772        super.viewDidLoad()773        viewDidLoadExpectation.fulfill()774      }775      override func viewWillAppear(_ animated: Bool) {776        super.viewWillAppear(animated)777        viewWillAppearExpectation.fulfill()778      }779      override func viewDidAppear(_ animated: Bool) {780        super.viewDidAppear(animated)781        viewDidAppearExpectation.fulfill()782      }783      override func viewWillDisappear(_ animated: Bool) {784        super.viewWillDisappear(animated)785        viewWillDisappearExpectation.fulfill()786      }787      override func viewDidDisappear(_ animated: Bool) {788        super.viewDidDisappear(animated)789        viewDidDisappearExpectation.fulfill()790      }791    }792    let viewDidLoadExpectation = expectation(description: "viewDidLoad")793    let viewWillAppearExpectation = expectation(description: "viewWillAppear")794    let viewDidAppearExpectation = expectation(description: "viewDidAppear")795    let viewWillDisappearExpectation = expectation(description: "viewWillDisappear")796    let viewDidDisappearExpectation = expectation(description: "viewDidDisappear")797    viewWillAppearExpectation.expectedFulfillmentCount = 4798    viewDidAppearExpectation.expectedFulfillmentCount = 4799    viewWillDisappearExpectation.expectedFulfillmentCount = 4800    viewDidDisappearExpectation.expectedFulfillmentCount = 4801    let viewController = ViewController(802      viewDidLoadExpectation: viewDidLoadExpectation,803      viewWillAppearExpectation: viewWillAppearExpectation,804      viewDidAppearExpectation: viewDidAppearExpectation,805      viewWillDisappearExpectation: viewWillDisappearExpectation,806      viewDidDisappearExpectation: viewDidDisappearExpectation807    )808    assertSnapshot(matching: viewController, as: .image)809    assertSnapshot(matching: viewController, as: .image)810    wait(for: [811      viewDidLoadExpectation,812      viewWillAppearExpectation,813      viewDidAppearExpectation,814      viewWillDisappearExpectation,815      viewDidDisappearExpectation,816    ], timeout: 1.0, enforceOrder: true)817    #endif818  }819  func testCALayer() {820    #if os(iOS)821    let layer = CALayer()822    layer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)823    layer.backgroundColor = UIColor.red.cgColor824    layer.borderWidth = 4.0825    layer.borderColor = UIColor.black.cgColor826    assertSnapshot(matching: layer, as: .image)827    #endif828  }...

Full Screen

Full Screen

SnapshotKitTests.swift

Source:SnapshotKitTests.swift Github

copy

Full Screen

...428        class ViewController: UIViewController {429            let viewDidLoadExpectation: XCTestExpectation430            let viewWillAppearExpectation: XCTestExpectation431            let viewDidAppearExpectation: XCTestExpectation432            let viewWillDisappearExpectation: XCTestExpectation433            let viewDidDisappearExpectation: XCTestExpectation434            init(435                viewDidLoadExpectation: XCTestExpectation,436                viewWillAppearExpectation: XCTestExpectation,437                viewDidAppearExpectation: XCTestExpectation,438                viewWillDisappearExpectation: XCTestExpectation,439                viewDidDisappearExpectation: XCTestExpectation440            ) {441                self.viewDidLoadExpectation = viewDidLoadExpectation442                self.viewWillAppearExpectation = viewWillAppearExpectation443                self.viewDidAppearExpectation = viewDidAppearExpectation444                self.viewWillDisappearExpectation = viewWillDisappearExpectation445                self.viewDidDisappearExpectation = viewDidDisappearExpectation446                super.init(nibName: nil, bundle: nil)447            }448            @available(*, unavailable)449            required init?(coder _: NSCoder) {450                fatalError("init(coder:) has not been implemented")451            }452            override func viewDidLoad() {453                super.viewDidLoad()454                viewDidLoadExpectation.fulfill()455            }456            override func viewWillAppear(_ animated: Bool) {457                super.viewWillAppear(animated)458                viewWillAppearExpectation.fulfill()459            }460            override func viewDidAppear(_ animated: Bool) {461                super.viewDidAppear(animated)462                viewDidAppearExpectation.fulfill()463            }464            override func viewWillDisappear(_ animated: Bool) {465                super.viewWillDisappear(animated)466                viewWillDisappearExpectation.fulfill()467            }468            override func viewDidDisappear(_ animated: Bool) {469                super.viewDidDisappear(animated)470                viewDidDisappearExpectation.fulfill()471            }472        }473        let viewDidLoadExpectation = expectation(description: "viewDidLoad")474        let viewWillAppearExpectation = expectation(description: "viewWillAppear")475        let viewDidAppearExpectation = expectation(description: "viewDidAppear")476        let viewWillDisappearExpectation = expectation(description: "viewWillDisappear")477        let viewDidDisappearExpectation = expectation(description: "viewDidDisappear")478        viewWillAppearExpectation.expectedFulfillmentCount = 2479        viewDidAppearExpectation.expectedFulfillmentCount = 2480        viewWillDisappearExpectation.expectedFulfillmentCount = 2481        viewDidDisappearExpectation.expectedFulfillmentCount = 2482        let viewController = ViewController(483            viewDidLoadExpectation: viewDidLoadExpectation,484            viewWillAppearExpectation: viewWillAppearExpectation,485            viewDidAppearExpectation: viewDidAppearExpectation,486            viewWillDisappearExpectation: viewWillDisappearExpectation,487            viewDidDisappearExpectation: viewDidDisappearExpectation488        )489        assertSnapshot(matching: viewController, as: .image)490        assertSnapshot(matching: viewController, as: .image)491        wait(492            for: [493                viewDidLoadExpectation,494                viewWillAppearExpectation,495                viewDidAppearExpectation,496                viewWillDisappearExpectation,497                viewDidDisappearExpectation498            ],499            timeout: 1,500            enforceOrder: true501        )502    }503    func testCALayer() {504        let layer = CALayer()505        layer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)506        layer.backgroundColor = UIColor.red.cgColor507        layer.borderWidth = 4.0508        layer.borderColor = UIColor.black.cgColor509        assertSnapshot(matching: layer, as: .image)510    }...

Full Screen

Full Screen

viewWillDisappear

Using AI Code Generation

copy

Full Screen

1override func viewWillDisappear(_ animated: Bool) {2    super.viewWillDisappear(animated)3    webView?.stopLoading()4}5override func viewWillDisappear(_ animated: Bool) {6    super.viewWillDisappear(animated)7    webView?.stopLoading()8}9override func viewWillDisappear(_ animated: Bool) {10    super.viewWillDisappear(animated)11    webView?.stopLoading()12}13override func viewWillDisappear(_ animated: Bool) {14    super.viewWillDisappear(animated)15    webView?.stopLoading()16}17override func viewWillDisappear(_ animated: Bool) {18    super.viewWillDisappear(animated)19    webView?.stopLoading()20}21override func viewWillDisappear(_ animated: Bool) {22    super.viewWillDisappear(animated)23    webView?.stopLoading()24}25override func viewWillDisappear(_ animated: Bool) {26    super.viewWillDisappear(animated)27    webView?.stopLoading()28}29override func viewWillDisappear(_ animated: Bool) {30    super.viewWillDisappear(animated)31    webView?.stopLoading()32}33override func viewWillDisappear(_ animated: Bool) {34    super.viewWillDisappear(animated)35    webView?.stopLoading()36}37override func viewWillDisappear(_ animated: Bool) {38    super.viewWillDisappear(animated)

Full Screen

Full Screen

viewWillDisappear

Using AI Code Generation

copy

Full Screen

1import UIKit2import WebKit3class ViewController: UIViewController {4    override func viewDidLoad() {5        super.viewDidLoad()6        webView.navigationDelegate = CancellingWKWebViewNavigationDelegate()7        webView.load(request)8    }9    override func viewWillDisappear(_ animated: Bool) {10        super.viewWillDisappear(animated)11        webView.stopLoading()12    }13}14class CancellingWKWebViewNavigationDelegate: NSObject, WKNavigationDelegate {15    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {16        print("Start loading")17    }18    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {19        print("Commit loading")20    }21    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {22        print("Finish loading")23    }24    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {25        print("Fail loading")26    }27}

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