我已启用自动化测试的可访问性。我已为所有元素添加了可访问性标签,但 NavigationLink 内的元素的可访问性为 false。
var body: some View {
NavigationView {
List(todoItems) { todoItem in
NavigationLink(destination: TodoDetailView(todoItem: todoItem)) {
HStack {
Image(systemName: "heart.fill")
.accessibilityLabel(Text("heart.fill"))
VStack {
Text(todoItem.action + "1")
.accessibilityLabel(Text(todoItem.action + "3"))
Text(todoItem.action + "2")
.accessibilityLabel(Text(todoItem.action + "4"))
}
}
}
.accessibilityElement(children: .contain)
}.navigationBarTitle(Text("Todo Items"))
}
}
无法在辅助功能中访问图像
我尝试在辅助功能中访问 NavigationLink 的各个项目
我更改了您的视图并为
NavigationLink
添加了辅助功能标签,如下所示:
var body: some View {
NavigationView {
List(todoItems) { todoItem in
NavigationLink(destination: TodoDetailView(todoItem: todoItem)) {
HStack {
Image(systemName: "heart.fill")
.accessibilityLabel(Text("heart.fill"))
VStack {
Text(todoItem.action + "1")
.accessibilityLabel(Text(todoItem.action + "3"))
Text(todoItem.action + "2")
.accessibilityLabel(Text(todoItem.action + "4"))
}
}
}
.accessibilityElement(children: .contain)
.accessibilityLabel("navigationLink")
}
.navigationBarTitle(Text("Todo Items"))
}
}
然后您就可以像这样访问 UI 测试中的导航链接:
let app = XCUIApplication()
app.launch()
let navigationLink = app.otherElements["navigationLink"]
也可以通过
Image
元素访问 NavigationLink
:
let image = navigationLink.images["heart.fill"]
我测试了它,我能够在测试中找到元素并可以断言其存在