首页 > 解决方案 > 测试view的前景颜色

问题描述

如何测试foregroundColorUITest 中的视图?我有一个Text例子:

Text("this can have different colors").foregroundColor(textColor)

在 UITest 我想知道 Text 有哪种颜色,例如:

XCTAssertEquals(app.staticTexts["..."].color, Color.white)

但是没有字段.color

这同样适用于Images。

标签: iosxcodeswiftuixctestxcuitest

解决方案


这在 UI 测试中是不可能的,它不会真正检查视觉效果,而是使用可访问性标识符与应用程序交互。

如果您想测试视图的颜色,您应该查看快照测试。您可以使用支持 SwiftUI 的 Point-Free 的 SnapshotTesting: https ://github.com/pointfreeco/swift-snapshot-testing


推荐阅读