UITests: How to find UIBarButtonItem by accessibilityIdentifier with predicate?
Asked Answered
M

2

7

This is how i set it in code:

let userBarButtonItem = UIBarButtonItem(image: userIcon, 
                                        style: .Plain, 
                                        target: self, 
                                        action: Selector("userButtonTapped:"))
userBarButtonItem.accessibilityIdentifier = "userBarButtonItem"

And then inside UITestCase I need to find this using:

XCUIApplication().otherElements["userBarButtonItem"] //doesnt work, and the reason is:

Assertion Failure: UI Testing Failure - No matches found for "userBarButtonItem" Other

Is there a way how to find this for instance using predicate?

Minny answered 28/8, 2015 at 9:37 Comment(2)
Did you get a answer to it ?Lectra
Unfortunately not, still looking for answer...Incommensurable
L
3

UIBarButtonItem does not implement UIAccessibilityIdentification, so setting accessibilityIdentifier doesn't work.

Instead try

userBarButtonItem.accessibilityLabel = "userBarButtonItem"

And then in test case

XCUIApplication().buttons["userBarButtonItem"]

This should work.

UPDATE :

Now UIBarButtonItem does conforms to UIAccessibilityIdentification, so all these not required.

Lectra answered 2/9, 2015 at 22:30 Comment(4)
Now UIBarButtonItem does conforms to UIAccessibilityIdentification. developer.apple.com/reference/uikit/…Lectra
UIBarButtonItem does implement UIAccessibilityIdentification, so you can add accessibilityIdentifier from User Defined runtime attributes.Platus
Well, on developer.apple.com/documentation/uikit/uibarbuttonitem definitely says that if conforms, but in code it actually doesn't :(Dmz
Actually you are right, accessibilityIdentifier still doesn't work, Thanks :)Idleman
G
2

This worked for me:

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:UIString(@"Sign In") style:UIBarButtonItemStyleDone target:self action:@selector(submitPressed:)];

 self.navigationItem.rightBarButtonItem.accessibilityLabel = @"registration-submit-button";

Then I found it via

app.navigationBars.buttons["registration-submit-button"]
Genesia answered 14/10, 2015 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.