Relational Selectors
Identify elements by position (above, below, leftOf, rightOf) or parent-child relationships (containsChild, childOf, containsDescendants).
Relational Selectors allow you to identify UI elements based on their relationship to other views on the screen. This is extremely useful when elements have generic names (like multiple "Buy" buttons) or when you want to target a view that lacks a stable text or id but is always located near a known anchor.
Overview
Selector
Description
above
Matches a view located vertically above a specified anchor element.
below
Matches a view located vertically below a specified anchor element.
leftOf
Matches a view located to the left of a specified anchor element.
rightOf
Matches a view located to the right of a specified anchor element.
containsChild
Matches a parent element that has a direct child matching specific criteria.
childOf
Matches an element that is a child of a specified parent element.
containsDescendants
Matches a view that contains a specific set of descendant elements.
Alignment selectors
The vertical alignment selectors above and below identify views located above or below a reference element.
# Tap a view located below the "Email" label
- tapOn:
below: Email
# Tap a view located above the "Checkout" title
- tapOn:
above: CheckoutSimilarly, horizontal alignment selectors leftOf and rightOf are used to identify elements positioned side-by-side, such as icons next to labels.
These alignment selectors use screen bounds; for precision when multiple elements match, combine them with other selectors (see Compound Selectors in the tip above).
To assert vertical hierarchy
You can stack above or below to assert the vertical order of elements:
Parent-Child Relationships
These selectors navigate the accessibility tree rather than relying on screen position.
containsChild (Direct child only)
This matches a parent element that has a direct child matching the given selector.
childOf (Direct parent only)
This matches an element that is a direct child of a specific parent. This is useful when the same child element exists in multiple containers.
containsDescendants
This matches a view that contains all specified descendant elements, at any depth in its hierarchy. This is not limited to direct children. Use containsDescendants for complex components like cards, rows, or grouped layouts where relevant elements may be nested multiple levels deep.
Last updated