Xcode UI Testing [xcode7-beta6] - Asserting actual label values when using accessibility labels

wegenmic picture wegenmic · Aug 31, 2015 · Viewed 13.6k times · Source

The question is actually really simple:

Is there a way to assert the displayed value from a specific label (e.g. UILabel) when using an accessibility label on this object?

As far as I see it, all the assertions (e.g. XCTAssertEquals) made in the examples, be it from a WWDC Talk or Blogposts, are only checking if an element exists for a query like XCTAssertEquals(app.staticTexts["myValue"].exists, true) or if the number of cells in a table is correct XCTAssertEquals(app.tables.cells.count, 5). So, when avoiding accessibility labels it's possible to check if an object has a certain value displayed, but not which object / element. And when using accessibility labels, it robs me of the opportunity to query against the displayed values, because app.staticTexts["myValue"] will now fail to deliver a result but app.staticTexts["myAccessibilityLabel"] will hit.

Assuming I want to test my "Add new Cell to Table" functionality, I can test that there is really a new cell added to the list, but I have no idea if the new cell is added at the top or the bottom of the list or somewhere in between.

For me, an easy way to check if a specific element has a certain value should be a no-brainer when it comes to UI Testing.

It is possible that due to the missing documentation I might overlook the obvious. If so, just tell me.

Answer

robert picture robert · Jul 23, 2016

Be sure to set the .accessibilityValue property of the UILabel whenever you set its .text value. Then in UITest, you can test the accessibility value like this:

    let labelElement = app.staticTexts["myLabel"]
    ...
    XCTAssertEqual(labelElement.value as! String, "the expected text")