6

I am using the KIF Framework for functional UI testing. Let's say I am on a current iPad screen where many views (labels, buttons, textfields etc) have unique accessibility labels assigned. If I have the accessibilityLabel string handy, can I get a reference to the associated UIView from current screen using it?

For example, [[UIView alloc] viewWithTag:5] returns UIVIew of provided tag. I am looking for something like [[UIView alloc] viewWithAccessiblityLabel:@"my label"].

P.S: I know the brute-force method would be to iterate all views in self.subviews recursively, and compare accessibility label to find what am I searching for. I am looking for a better approach.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
  • u can `subclass` `UIView` in ur `Custom` classes.. – Ishank May 10 '13 at 15:37
  • The only way to do what you want is to iterate though the subviews and check. This type of behavior probably means you have poor design in your class. Anything you'll need to refer to later could be stored in a property, or if it's created dynamically, in an NSDictionary which is stored as a property. – Aaron Brager May 10 '13 at 15:52

3 Answers3

11

There is actually an incredibly simple way to achieve what you describe when using KIF for UI automation, though KIF doesn't make it obvious that this is possible. waitForViewWithAccessibilityLabel returns a reference to the view when it is found:

Swift

 let view = tester().waitForView(WithAccessibilityLabel: "My label")

Objective-C

UIView *view = [tester waitForViewWithAccessibilityLabel:@"My label"];

Hugely useful, once discovered.

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
  • but it fails if the view does not exist. is there a way to ask for that and NOT fail if not found? – zaxy78 Mar 15 '17 at 10:03
  • I am not aware of a way to ask for the view but have it not fail if not found, and as you've obviously found, "waitForViewWithAccessibiltyLabel" will time out causing the test to fail if the view is not present. This is a downside to this approach. – Duncan Babbage Mar 20 '17 at 00:32
9

I am using KIF for UI automation! Here are the steps to get view from given accessibilityLabel. Method viewContainingAccessibilityElement:element is extension method to UIAccessibilityElement class.

UIAccessibilityElement *element = [[[UIApplication sharedApplication] keyWindow] accessibilityElementWithLabel:label];
UIView *view = (UIView*)[UIAccessibilityElement viewContainingAccessibilityElement:element];
Yuriy Kolbasinskiy
  • 3,791
  • 3
  • 16
  • 33
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
  • 2
    I just added `keyWindow` like this `[[[UIApplication sharedApplication] keyWindow] accessibilityElementWithLabel:label];` and this worked for me. Thanks! – SirRupertIII Oct 15 '13 at 16:51
  • This functionality is built into KIF, but not very discoverable. See comment by @duncan-babbage for an easier solution. – Mark Suman Mar 24 '15 at 19:35
2

It sounds to me (from your comment: "I need this functionality in automating UI tests") like you are looking for the accessibilityIdentifier. From the documentation:

The UIAccessibilityIdentification protocol is used to associate a unique identifier with elements in your user interface. You can use the identifiers you define in UI Automation scripts because the value of accessibilityIdentifier corresponds to the return value of the name method of UIAElement.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205