UITests: How to find a UIBarButtonItem using an accessibilityIdentifier with a predicate?

This is how I installed it in the 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: 

Confirmation error: UI testing error - No matches found for "userBarButtonItem" Other

Is there a way to find this, for example, using a predicate?

+5
source share
2 answers

UIBarButtonItem does not implement UIAccessibilityIdentification , so setting accessibilityIdentifier does not work.

Try instead

userBarButtonItem.accessibilityLabel = "userBarButtonItem"

And then in the test case

XCUIApplication().buttons["userBarButtonItem"]

That should work.

UPDATE:

Now UIBarButtonItem corresponds to UIAccessibilityIdentification, so all this is not required.

+2
source

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 through

 app.navigationBars.buttons["registration-submit-button"] 
+1
source

All Articles