首页 > 解决方案 > 使用 XCTestCases 类中的可访问性标识符访问 NavigationLink - SwiftUI

问题描述

我最近开始研究 SwiftUI 框架,我正在使用 XCTest 编写 UI 测试用例。

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

在 XCTestCase 类中,我尝试了以下代码来点击 NavigationLink:

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

我收到以下错误,并且测试用例失败:

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

我在 XCTestCase 类中搜索与 NavigationLink 访问相关的内容,但没有找到任何有效/正确的文档。

如果有人已经在这个主题上工作过,请你帮我解决这个问题。

标签: iosswiftswiftuiswiftui-navigationlinkxctestcase

解决方案


推荐阅读