有人知道如何正确编写一个切换按钮的UI测试吗?即使在一个新项目中,只有一个切换按钮而没有其他任何UI元素,在进行测试时也会出现这种错误:
Failed to get matching snapshot: Multiple matching elements found for <XCUIElementQuery: 0x60000108c410>.
Sparse tree of matches:
→Application, pid: 26580, label: 'TestToggle'
↳Window (Main)
↳Other
↳Other
↳Other
↳Other
↳Switch, label: 'Test switch', value: 1
↳Switch, label: 'Test switch', value: 1
UI的外观如下:
struct ContentView: View {
@State private var toggleValue = true
var body: some View {
Toggle("Test switch", isOn: $toggleValue)
.padding()
}
}
测试看起来像这样(这两行都给我同样的错误):
func testExample() throws {
let app = XCUIApplication()
app.launch()
XCTAssertTrue(app.switches["Test switch"].value as? String == "1")
// XCTAssertTrue(app.switches["Test switch"].isEnabled)
}
我一定是做错了什么。如果只有一个,怎么会出现两个开关?我看到的在线文章似乎没有提到这一点。感谢任何帮助。谢谢 :)
isEnabled
返回“元素是否启用用户交互”。如果用户可以与之交互,则isEnabled
返回true
,即使切换关闭。 - Ruben Veldman