Retrieving a buttons text in UI Testing

Hello,


I'm trying to read a buttons current text, but I can't seem to find a good way to do that. Here's what i have right now:


func testExample() {
  XCUIApplication().buttons["MahButton"].tap()
  let button = XCUIApplication().buttons["NewVCBtn"]
  XCTAssertEqual(button.value as! String, "New VC")
}


MahButton pushes a new VC, and then I'm finding my "NewVCBtn"; this works, .exists returns true if I put that in there, but .value is returning an empty string even though in the storyboard "New VC" is the buttons text.


Has anyone been able to get something similar to work?

Replies

The actual String value for the button might not be visible through button.value.

I find sometimes I'll have to go into button.staticTexts(@"New VC").value or something to find it. Try playing around with it.


I just wanted to add that using XCTAssertEqual to compare strings is not ideal. I found that the binary data is not equal so it returns an error even though the strings match. You'll have to using native Objective C String comparison methods to compare strings then Assert success.

This question is old, but in case it helps anybody, the text on the button can be accessed as follows:


buttons[@"Accessibility Identifier"].label


And can be compared against expected string as


XCTAssert([buttons[@"Accessibility Identifier"].label isEqualToString:anotherString]);


Hope this helps someone