4

I have a button with dynamic title text, set up as follows:

button.label = @"Chosen Date Button";
[button setTitle:stringOfDateChosenByUser
        forState:UIControlStateNormal];

In my UI test, I want to verify this title text. I managed to access the button using:

XCUIElement *button = app.buttons[@"Chosen Date Button"];

But I can't seem to access the title property of the button (contents of stringOfDateChosenByUser) in any of the following fields I've tried. I've included the respective outputs in comments:

    NSLog(@"strings to examine: %@ %@ %@ %@ %@ %@",
      (NSString *)button.value, //<empty string>
      button.title, //<empty string>
      button.label, //@"Chosen Date Button"
      button.identifier, //<empty string>
      button.accessibilityLabel, //null
      button.accessibilityValue); //null

Since the title is visible on screen, I'm guessing that there should be a way to access it. Is there?

Yoga
  • 1,186
  • 1
  • 13
  • 24
  • When is your debug NSLog being called? Is there a reason your app isn't setting the title text in one of the accessibility properties? – bneely May 11 '16 at 23:19
  • It's called a few seconds after the button's label and title are set and are visible on screen. I'm looking to access the stringOfDateChosenByUser from the tests. – Yoga May 12 '16 at 00:26
  • Does the button have an attributed title set? I was just checking `UIButton` class reference and it states that the attributed title is preferred if it is set. If that were set to an empty string, that might explain what you're seeing. Also I would expect an empty string if the button you're inspecting is not in the normal state (selected, focused, disabled are other states it might be in). Maybe try tapping the button before checking its title. – bneely May 12 '16 at 00:32
  • I'd recommend as a debugging step setting the title for every state instead of just for the Normal state: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/index.html#//apple_ref/c/tdef/UIControlState if you do that and it still gives the empty string for the title in the test, I have some more ideas, but they're less plausible! – Aaron Sofaer May 19 '16 at 23:22

0 Answers0