我正在Xcode上尝试UITesting。应用程序在点击按钮时显示Alert,然后在点击时打开Modal;打开";按钮处于警报状态。UITesting可以找到该元素,但在show modal之后找不到元素。如果从应用程序中删除警报,则可以在显示模态之后找到元素。为什么在该应用程序中找不到元素?我该怎么办?请帮我
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 [!]