Get UIVIew from accessibilityLabel for KIF automation
Asked Answered
M

3

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.

Motte answered 10/5, 2013 at 15:35 Comment(2)
u can subclass UIView in ur Custom classes..Bullion
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.Figured
M
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];
Motte answered 16/5, 2013 at 8:58 Comment(2)
I just added keyWindow like this [[[UIApplication sharedApplication] keyWindow] accessibilityElementWithLabel:label]; and this worked for me. Thanks!Dragelin
This functionality is built into KIF, but not very discoverable. See comment by @duncan-babbage for an easier solution.Constitutionality
A
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.

Ambidextrous answered 8/10, 2014 at 17:50 Comment(2)
but it fails if the view does not exist. is there a way to ask for that and NOT fail if not found?Iow
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.Ambidextrous
M
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];
Motte answered 16/5, 2013 at 8:58 Comment(2)
I just added keyWindow like this [[[UIApplication sharedApplication] keyWindow] accessibilityElementWithLabel:label]; and this worked for me. Thanks!Dragelin
This functionality is built into KIF, but not very discoverable. See comment by @duncan-babbage for an easier solution.Constitutionality
T
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.

Tauromachy answered 11/5, 2013 at 15:23 Comment(2)
Thanks David. That's useful but KIF automation framework that I am using is based on accessibility label so I have to use it.Motte
Note that KIF subsequently added the ability to target elements by accessibilityIdentifier as well.Ambidextrous

© 2022 - 2024 — McMap. All rights reserved.