Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITests: How to find UIBarButtonItem by accessibilityIdentifier with predicate?

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?

like image 212
Bartłomiej Semańczyk Avatar asked Aug 28 '15 09:08

Bartłomiej Semańczyk


2 Answers

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"]
like image 23
Snowman Avatar answered Nov 18 '22 15:11

Snowman


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.

like image 106
Sandy Avatar answered Nov 18 '22 16:11

Sandy