使用XCTestCases类SwiftUI中的可访问性标识符访问NavigationLink



我最近开始研究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访问相关的文档,但没有找到任何有效/正确的文档。

如果有人已经研究过这个话题,你能帮我吗。

与其寻找匹配的xxx.links[...]元素[我认为它实际上是用于点击WebView中的链接],不如尝试:

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

(我基本上遇到了你同样的问题,在尝试了很多事情之后,最终录制了一段录音,并看到了它的原貌(。

最新更新