如何解决Xcode UI 测试找不到元素
我正在 Xcode 上尝试 UITesting。 应用程序在点击按钮时显示警报,然后在警报时点击“打开”按钮时打开模态。 UITesting 可以找到该元素,但在显示模态后找不到该元素。 如果从应用程序中删除警报,可以在显示模态后找到元素。 为什么在该应用程序中找不到元素?我该做什么?请帮帮我
struct ContentView: View {
@State var showSheet = false
@State var showAlert = false
var body: some View {
HStack{
Button(action: {
showAlert = true
},label: {
Text("button")
})
.accessibility(identifier: "button")
}
.sheet(isPresented: $showSheet,content: {
Modal(showModal: $showSheet)
})
.alert(isPresented: $showAlert) { () -> Alert in
Alert(title: Text("title"),message: Text("message"),primaryButton: .default(Text("open"),action: {
showSheet = true
}),secondaryButton: .cancel())
}
}
}
struct Modal: View{
@Binding var showModal: Bool
init(showModal: Binding<Bool>){
self._showModal = showModal
}
var body: some View {
return ZStack(alignment: .top) {
Button {
showModal.toggle()
} label: {
Text("back")
}
.accessibility(identifier: "button2")
}
.accessibility(identifier: "modal")
}
}
func testExample() throws {
let app = XCUIApplication()
app.launch()
app.buttons["button"].tap()
sleep(1)
app.buttons["open"].tap()
sleep(1)
let result = app.buttons["button2"]
XCTAssert(result.exists)
}
解决方法
它可以使用标签,例如(使用 Xcode 13beta / iOS 15 测试)
let result = app.buttons["back"]
XCTAssert(result.exists)
似乎还需要覆盖第二个标识符,因此您可以将其删除
return ZStack(alignment: .top) {
Button {
showModal.toggle()
} label: {
Text("back")
}
.accessibility(identifier: "button2")
}
// .accessibility(identifier: "modal") // << PASSED [!]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。