如何解决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 举报,一经查实,本站将立刻删除。