2

I have recently started work on the SwiftUI framework, I'm writing UI Test Cases using XCTest.

NavigationLink("Login", destination: TabBarDashboardView())
          .accessibility(identifier: "linkLogin")
          .frame(width: 200, height: 50, alignment: .center)
          .foregroundColor(.white)
          .background(Color.red)
          .padding([.top, .bottom], 40)

In the XCTestCase class, I tried the following code to tap NavigationLink:

func testLoginActionAndNavigation() throws {
        let linkLogin = self.app.navigationBars.links["linkLogin"]
        linkLogin.tap()
        XCTAssertEqual(linkLogin.title, "linkLogin")
    }

I'm getting the following error and test case is getting failed:

Failed to get matching snapshot: No matches found for Descendants matching type Link from input {( NavigationBar, identifier: '_TtGC7SwiftUI19UIHosting' )}

I search related to NavigationLink access in XCTestCase class, but didn't find any valid/proper documentation.

Would you please help me on this if anyone already worked on this topic.

Amol Chaudhari
  • 496
  • 4
  • 5

1 Answers1

0

Instead of looking for a matching xxx.links[...] element [which I believe is actually for clicking links inside a WebView], try:

 let linkLogin = self.app.otherElements.buttons["linkLogin"]

(I basically hit your same issue, and after trying a bunch of things finally ended up doing a recording and seeing what it same back with).

tiritea
  • 1,229
  • 13
  • 18