UITest color of a label (not UI label) UITest color of a label (not UI label) swift swift

UITest color of a label (not UI label)


I'm afraid you're not going to be able to do this via UI Testing. UI Testing can "see" the interface only to the extent that Accessibility exposes it. Thus you can "see" the text but you cannot "see" that this thing is a UILabel; UI Testing knows nothing of that. Thus you cannot explore its nature as a UILabel.


You can't get the color directly, but if you need to test your UI to make sure your labels are highlighted properly you can set your accessibility label to include information such as the color or the fact that it is highlighted.

This will also improve the usability of the app, since visually impaired users won't see the color anyway. Then you can write tests to ensure that at least the accessibility label is correct. There is of course still the possibility that the developer makes the label color green and the accessibility label "yellow", but that is a general risk of using accessibility to query UI elements.

For example, in your app code:

myLabel.text = "Draft" myLabel.color = .yellowmyLabel.accessibilityLabel = "Draft (Yellow)"