首页 > 解决方案 > 无法使用 doubleTap() 进行简单的 UI 测试 (macOS)

问题描述

ContentView在 Mac App 项目上有这个简单的:

struct ContentView: View {
    @State var isPresented: Bool = false
    var body: some View {
        HStack {
            CustomView()
                .frame(minWidth: 150, idealWidth: 150, maxWidth: 150, minHeight: 100, idealHeight: 100, maxHeight: 100, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                .contentShape(Rectangle())
                .accessibility(identifier: "touch1")
                .onTapGesture(count: 2){
                    isPresented = true
                }
        }
        .alert(isPresented: $isPresented) {
            let alert = Alert(title: Text("Double Clicked"))
            return alert
        }
    }
}

struct CustomView: View {
    var body: some View {
        VStack{
            Image(systemName: "cursorarrow.click.2")
        }
    }
}

然后我想为它写一个 UI 测试:

    func testExample() throws {
        // UI tests must launch the application that they test.
        let app = XCUIApplication()
        app.launch()

        XCTAssert(app.images["touch1"].isHittable)
        
        app.images["touch1"].doubleTap()

        // Use recording to get started writing UI tests.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
        
        XCTAssertTrue(app.alerts["Double Clicked"].waitForExistence(timeout: 1))
    }

当我运行测试时,警报不显示,但鼠标光标在我的按钮中间移动,这让我认为app.images["touch1"]是有效的。

在此处输入图像描述

有人用doubleTap()吗?没有它我如何 UI 测试我的应用程序?

完整的日志如下:

2021-03-15 15:09:16.836806+0100 DoubleClickUITests-Runner[4333:80088] Running tests...
Test Suite 'Selected tests' started at 2021-03-15 15:09:17.019
Test Suite 'DoubleClickUITests.xctest' started at 2021-03-15 15:09:17.020
Test Suite 'DoubleClickUITests' started at 2021-03-15 15:09:17.021
Test Case '-[DoubleClickUITests.DoubleClickUITests testExample]' started.
    t =     0.00s Start Test at 2021-03-15 15:09:17.025
    t =     0.43s Set Up
2021-03-15 15:09:17.567336+0100 DoubleClickUITests-Runner[4333:80593] [Arbitration] starting DTServiceHub child handshake.0 (send: 0x5d03, receive: 0x5c3f)
2021-03-15 15:09:17.567677+0100 DoubleClickUITests-Runner[4333:80088] [Arbitration] attempting connection to singleton: 2568 with send port: 0x5e03
2021-03-15 15:09:17.568590+0100 DoubleClickUITests-Runner[4333:80088] [Arbitration] handshake SUCCESSFUL (child: 4335 -> singleton: 2568)
    t =     0.55s Open org.tynsoe.DoubleClick
    t =     0.72s     Launch org.tynsoe.DoubleClick
    t =     2.52s         Wait for accessibility to load
    t =     2.96s         Setting up automation session
    t =     2.99s         Wait for org.tynsoe.DoubleClick to idle
    t =     3.18s Find the "touch1" Image
    t =     3.39s Double tap "touch1" Image
    t =     3.39s     Wait for org.tynsoe.DoubleClick to idle
    t =     3.54s     Find the "touch1" Image
    t =     3.73s     Synthesize event
    t =     4.60s     Wait for org.tynsoe.DoubleClick to idle
    t =     4.76s Waiting 1.0s for "Double Clicked" Alert to exist
    t =     5.76s     Checking `Expect predicate `exists == 1` for object "Double Clicked" Alert`
    t =     5.76s         Checking existence of `"Double Clicked" Alert`
    t =     5.87s         Capturing element debug description
    t =     5.87s Checking existence of `"Double Clicked" Alert`
/Users/yann/Desktop/DoubleClick/DoubleClickUITests/DoubleClickUITests.swift:36: error: -[DoubleClickUITests.DoubleClickUITests testExample] : XCTAssertTrue failed
    t =     6.14s Tear Down
Test Case '-[DoubleClickUITests.DoubleClickUITests testExample]' failed (6.346 seconds).
Test Suite 'DoubleClickUITests' failed at 2021-03-15 15:09:23.368.
     Executed 1 test, with 1 failure (0 unexpected) in 6.346 (6.347) seconds
Test Suite 'DoubleClickUITests.xctest' failed at 2021-03-15 15:09:23.369.
     Executed 1 test, with 1 failure (0 unexpected) in 6.346 (6.348) seconds
Test Suite 'Selected tests' failed at 2021-03-15 15:09:23.369.
     Executed 1 test, with 1 failure (0 unexpected) in 6.346 (6.350) seconds

标签: swiftswiftuiuitest

解决方案


推荐阅读