【发布时间】:2020-07-18 14:22:53
【问题描述】:
我有一个 SwiftUI 视图,它有一个包含 DatePicker 的表单:
struct GettingUpTimeSettingView: View {
@State private var viewModel = GettingUpTimeSettingViewModel()
var body: some View {
VStack {
Form {
Spacer()
Text(viewModel.questionString)
.accessibility(identifier: "Question")
Spacer()
DatePicker("Alarm Time",
selection: $viewModel.gettingUpTime,
displayedComponents: .hourAndMinute)
.accessibility(identifier: "Time")
.animation(.easeInOut)
Spacer()
Text(viewModel.explanationString)
.accessibility(identifier: "Explanation")
}
}
}
}
还有一个用于 UI 测试的 XCTestCase 类:
class SleepyGPIntroUITests: XCTestCase {
private var app: XCUIApplication!
override func setUp() {
continueAfterFailure = false
app = XCUIApplication()
app.launch()
greeting = app.staticTexts["Greeting"]
}
override func tearDown() {
app = nil
}
func test_InitialScreen_ChangesTo_GettingUpScreen_Automatically() {
//Given
let questionText = "What time do you want to get up?"
let explanationText = "This should be the same time every day, including at weekends and on days off. Our best chance of great sleep comes when we have a regular routine."
let timeText = "7:00am"
let question = app.staticTexts["Question"]
let explanation = app.staticTexts["Explanation"]
let time = app.staticTexts["Time"]
//When
_ = time.waitForExistence(timeout: 2)
//Then
XCTAssertFalse(greeting.exists)
XCTAssertEqual(question.label, questionText, "Should show question at top of view")
XCTAssertEqual(explanation.label, explanationText, "Should show explanation in view")
XCTAssertEqual(time.label, timeText, "Should show the correct default time")
}
当我运行测试时,它失败并给我这个消息:
未能获得匹配的快照:从输入 {( StaticText,标识符:'问题',标签:'你想几点起床?', StaticText, identifier: 'Explanation', label: '这应该是每天的同一时间,包括周末和休息日。当我们有规律的作息时,我们最好的睡眠机会就来了。 )}
我不确定这是因为 DatePicker() 包含在表单中,还是因为我没有使用正确的 XCUIElementQuery 来查找它?
当我将 DatePicker 移到表单之外时,我可以找到它,但只能使用它的标签,而不是它的可访问性标识符。
可访问性标识符对于 Text 对象工作正常。
我也可以使用
app.datePickers.firstMatch
当它在表单之外时,而不是当它包含在表单中时。
我发现this 的答案描述了当 SwiftUI 对象包含在表单中时的一些奇怪行为,但仍然无法解决我的问题。
非常感谢。
tl:dr 如果 DatePicker 包含在 SwiftUI 表单中,则在 UITesting 时无法让 XCUIElementQuery 返回 DatePicker 元素。
【问题讨论】: