【问题标题】:Xcode UI Testing can't find elementsXcode UI 测试找不到元素
【发布时间】: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)
    }

【问题讨论】:

    标签: ios swift swiftui xctest


    【解决方案1】:

    它适用于标签的使用,例如(使用 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 [!]
    

    【讨论】:

      猜你喜欢
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多