gpt4 book ai didi

swift - 如何从 UI 测试用例类访问 SwiftUI View 的属性?

转载 作者:行者123 更新时间:2023-11-30 10:30:59 25 4
gpt4 key购买 nike

我正在编写 UI 测试,我需要访问 View 的 @State 属性,只要该 View 内有一个按钮即可。

struct CircleImage: View {

@State var imageName: String = ""

var body: some View {
// ...
Button(action: {}) {}
.accessibility(identifier: "myButton")
}
}

我可以使用 let myButton = app.buttons["myButton"] 轻松获取按钮。但出于测试目的,我还需要访问 imageName 属性。我尝试在 ContentView 中添加 CircleImage().accessibility(identifier: "circleImageView") ,但它会覆盖 CircleImage View (包括按钮)内的所有可访问性(identifier: )。

最佳答案

UI 测试不测试内部状态。他们只测试 UI 可见的行为。要针对此类型编写 UI 测试,imageName 应该导致某种行为,然后测试该行为是否可见。在这种情况下,您可以将 imageName 转换为辅助功能标签,这将允许 VoiceOver 读取它(顺便也允许 UI 测试访问它)。

The best way to make an app UI-testable is to first make it accessible.使用 VoiceOver 试用您的应用程序。如果它在那里运行良好,那么它也可能适用于 UI 测试。

关于swift - 如何从 UI 测试用例类访问 SwiftUI View 的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59237968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com