【发布时间】:2021-09-09 10:19:18
【问题描述】:
我正在 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)
}
【问题讨论】: