How to grab a path for app.navigationBars

40 views Asked by At

I faced an issue that is combined with UI testing of the view. I have an issue with an incorrect path to the element that is the text field, which is Search. I would like to achieve the state when the code for app.navigationBars will be resistant for different languages. That's why right now it looks the following way:

var airportsSearchTextField: XCUIElement { app.navigationBars["Airports"].searchFields["Search"] }

This won't work properly for another language like English. I know that it could be possible to add accessibilityIdentifier, but I'm not sure where and maybe for navigationBar is there any other way to implement a better solution to grab this element within UI tests?

func selectFlight() { airportsSearchTextField.tap() }

1

There are 1 answers

0
Mike Collins On

Without identifiers (the correct solution) you'd need to maintain a list of string translations and locate those elements based on the language selected.

If you felt you could rely on the UI's architecture not changing you can always reference elements by their index (i.e. app.navigationBars.firstMatch.searchFields.elementBoundByIndex(1) would get you the second search field in the first navigation bar returned). I don't recommended doing this unless absolutely necessary.