12

Here is a pic of the table I'm working with.

enter image description here

I'm just trying to get the color of "requested" (gray) or "draft" (orange). I'm able to get the actual string of "requested" or "draft" by

var timeSheetStatus = 
    app.tables.element.cells.elementBoundByIndex(0).staticTexts.elementBoundByIndex(1).label

but this is just a string and not a UIlabel (if it were a UILabel I would be able to do label.textColor I believe). How do I get the color of this string so I can assert that it is indeed gray or orange?

user3613290
  • 461
  • 6
  • 18
  • Of course it's a label. A string can't just magically show up in the interface; it has to be drawn by some interface object. – matt Jun 10 '16 at 18:14
  • @matt Oh sorry I started learning about this from scratch starting a couple of days ago. I meant that the code in my question returns a string, not a UILabel, so I guess I'm just confused on how to get the UILabel of this element to work with it. – user3613290 Jun 10 '16 at 18:17
  • You're going to need to write a unit test for the coloring of the UILabel, where you will be able to access the color attribute of the UILabel. – Oletha Jun 11 '16 at 09:23

2 Answers2

15

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
8

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 = .yellow myLabel.accessibilityLabel = "Draft (Yellow)"

Chris Garrett
  • 4,824
  • 1
  • 34
  • 49
  • 3
    Of course you have to be a little careful because a VoiceOver user could end up hearing your accessibility label. You don't want to put anything in there that you wouldn't want to make public. – matt Nov 29 '18 at 02:45
  • This is a much better answers and if you do it good you improve accesibility – doozMen Mar 20 '21 at 15:36