SwiftUI XCTest如何访问ScrollView子级?

问题描述

我们正在尝试为SwiftUI应用程序编写XCTest案例,并且很难找到滚动视图的子级。

给出以下简单视图:

struct ContentView: View {
    var body: some View {
        vstack {
            vstack {
                Text("Hello")
                Text("World")
            }

            ScrollView {
                Text("Goodbye")
                Text("Cruel")
                Text("World!")
            }
        }
    }
}

为什么以下XCTestCase找不到“再见”元素?

import XCTest
class ScrollViewUITests: XCTestCase {

    func testScrollChildren() throws {
        let app = XCUIApplication()
        app.launch()

        XCTAssertTrue(app.staticTexts["Hello"].exists,"Hello not there")
        XCTAssertTrue(app.staticTexts["World"].exists,"World not there")

        let scroll = app.scrollViews.firstMatch
        XCTAssertTrue(scroll.exists,"Scroll not there")

        XCTAssertTrue(app.staticTexts["Goodbye"].exists,"Goodbye not there")
        XCTAssertTrue(app.staticTexts["Cruel"].exists,"Cruel not there")
        XCTAssertTrue(app.staticTexts["World!"].exists,"World! not there")
    }
}

未嵌入滚动视图的文本字段已成功找到。交互式运行该应用程序将在屏幕上显示文本字段。如果将ScrollView替换为vstack,则测试用例将能够找到静态文本。

代码针对使用xcode 12的iOS 13,但我们也尝试针对iOS 14。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)