iosxcode-ui-testing

Identify UISearchBar with accessibility identifier during UITestCase


I have one UISearchBar on my one of UIViewController. But UISearchBar doesn't have accessibility identifier by default so I have tried to define manual accessibility identifier using "User Define Runtime Attribute".

enter image description here

But during UITestCase writing I can not access or identify UISearchBar by using "User Define Runtime Attribute". But I can't access it with identifier. I always have to go with placeholder text that I don't want to use because it causes issue while have "multi-language" support.

I have refer stack overflow answer regarding this issue, but none of them meet to my requirement.

Please help to provide solution for it. Thank you.


Solution

  • In an application when I set the accessibilityLabel of my UISearchBar to MySearchBar using the User Define Runtime Attribute, if you look at the application's UI hierarchy you can see the following:

    Attributes: Application, 0x604000198050, pid: 18393, {{0.0, 0.0}, {414.0, 736.0}}, label: 'MyApp'
    Element subtree:
     →Application, 0x604000198050, pid: 18393, {{0.0, 0.0}, {414.0, 736.0}}, label: 'MyApp'
        Window, 0x6000001993d0, Main Window, {{0.0, 0.0}, {414.0, 736.0}}
          Other, 0x604000199a50, traits: 8589934592, {{0.0, 0.0}, {414.0, 736.0}}
            Other, 0x600000197aa0, traits: 8589934592, {{0.0, 20.0}, {414.0, 56.0}}, identifier: 'MySearchBar'
              Image, 0x604000198390, traits: 8589934596, {{0.0, 0.0}, {414.0, 76.0}}
              SearchField, 0x60000019a4e0, traits: 146031248384, {{8.0, 30.0}, {398.0, 36.0}}
    

    That should give you an idea on how to find your searchbar. First search an element of type other that has the identifier that you set to the search bar, then find child of this element of type searchBar.

    let searchField = XCUIApplication().otherElements["MySearchBar"].searchFields.firstMatch
    searchField.tap()
    searchField.typeText("Hello")