【发布时间】:2020-10-05 02:49:13
【问题描述】:
我有一个VStack,其代码依赖于.onTapGesture 方法。像这样的:
VStack {
if imageShow {
Image("image1")
}
else {
Image("image2")
}
}
.onTapGesture {
imageShow.toggle()
}
我想使用 XCTest 在 UI 测试中测试此行为。问题是,我不知道如何访问 VStack 以便对其应用.tap()。我似乎找不到附加到app 的方法。使用app.buttons[] 找到了一个按钮,但似乎没有与app.VStack 或app.HStack 等效的按钮。
另外,我尝试转换此代码以将 VStack 包装在 Button 中,但由于某种原因,这会覆盖我的图像,从而扭曲了首选行为。
使用我正在使用的完整 VStack 代码 sn-p 进行更新:
VStack(alignment: .leading, spacing: 0) {
ZStack {
if self.create_event_vm.everyone_toggle == true {
Image("loginBackground")
.resizable()
.accessibility(identifier: "everyone_toggle_background")
}
HStack {
VStack(alignment: .leading, spacing: 0) {
Text("Visible to everyone on Hithr")
.kerning(0.8)
.scaledFont(name: "Gotham Medium", size: 18) .foregroundColor(self.create_event_vm.everyone_toggle ? Color.white : Color.black)
.frame(alignment: .leading)
Text("Public event")
.kerning(0.5)
.scaledFont(name: "Gotham Light", size: 16)
.foregroundColor(self.create_event_vm.everyone_toggle ? Color.white : Color.black)
.frame(alignment: .leading)
}
.padding(.leading)
Spacer()
}
}
}
.frame(maxWidth: .infinity, maxHeight: 74)
.onTapGesture {
self.create_event_vm.everyone_toggle.toggle()
}
.accessibility(identifier: "public_event_toggle")
.accessibility(addTraits: .isButton)
【问题讨论】: