Search code examples
iosui-automationxcuitest

Setting accessibility identifier for UI automation testing using XCUITest


I have started writing UI automation tests for IOS using XCUITest. I am a bit confused about setting identifiers to access elements while writing UI tests. Look at the picture.

User interface in Xcode to set identifiers

We have user Defined RunTime Attributes where you can define the key "accessibilityLabel", type as string and value as example "ButtonID". Now, you could use "ButtonID" as the identifier to access the button to write tests.

The second thing I noticed is the Accessibility section where we have a field by name identifier where you could set an identifier and use that identifier to access the UI element.

Could you confirm the difference between the two. Which one of the above two is recommended to set identifiers for UI test.


Solution

  • The effect is the same, by the time your control awakes from nib you'll have your accessibilityIdentifier/Label/etc set up.

    Not sure if one way is "officially" preferred over another but I'd recommend using Accessibility section at least for the following reasons:

    • You are sfae frmo tpyos.
    • User-defined runtime attributes are there for you set up your custom properties. For standard attributes Apple takes care of you by giving you the specific, logically grouped knobs and input fields.
    • What if you specify both? Which one do you expect to be the final value?
      • Answer: the custom one, but I'm not sure that's documented anywhere and therefore you shouldn't rely on that. And even if it is documented, I'd still avoid such redundancy :-)
    • Custom (user-defined) properties are set via KVC, which adds some (negligible) overhead, whereas for Accessibility section there seems to be some shortcut¹ (note the absence of setValue:forKey:):
    * thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
      * frame #0: 0x000000010efe09fb AccessibilityProbe`-[MyButton setAccessibilityIdentifier:](self=0x00007fbb765231d0, _cmd="setAccessibilityIdentifier:", accessibilityIdentifier=@"testID") at MyButton.m:14
        frame #1: 0x0000000112e35538 UIKitCore`-[UIRuntimeAccessibilityConfiguration applyConfiguration] + 153
        frame #2: 0x0000000110825cfd CoreFoundation`-[NSArray makeObjectsPerformSelector:] + 317
        frame #3: 0x0000000112e33258 UIKitCore`-[UINib instantiateWithOwner:options:] + 1717
    

    ¹ Again, implementation details are not something to rely on.