Relational Accessor Functions
Relational accessor functions state that an element should be searched for in relation to another element, instead of by its own attributes. They make a locator more robust when the target element has no stable id, name, or text of its own — typically inside tables, menus, and repeated card layouts.
Summary
| Accessor | Finds the element… | Typical use |
|---|---|---|
_in | inside another element | A link or label inside a specific container |
_near | nearest to another element (by distance) | An icon or button next to a known label |
_above | above another element | A column header for a cell below it |
_below | below another element | A value under a known header |
_rightOf | to the right of another element | The cell beside a row label |
_leftOf | to the left of another element | The row label beside a known cell |
_in
Searches for the element within another element. Scope a generic locator to the region of the page where it is expected, which removes ambiguity when the same text or tag appears several times.
// the "Products" link that lives inside the /products link
click(span("Products", _in(link("/products"))));
_near
Searches for the element near another element. The accessor works from the centre point of the nearby elements and calculates the distance between the centre of the reference point and the surrounding objects in every direction.
The number written before near sets the order in which the system picks near objects: 0 is the nearest, 1 the second nearest, and so on.
// nearest element to the "Label Page" link
link(0, near("Label Page"));
// second nearest element to the same reference point
link(1, near("Label Page"));
_near and _below resolve positions with x/y coordinates, so they are the two accessors most sensitive to a changed page layout.
_above and _below
Searches for the element above or below another element. Both are most useful when working with tables, where the row label and the value share no attributes.
_above strong("Type", _above(strong("Size")))
_below strong("Size", _below(strong("Type")))
_rightOf and _leftOf
Searches for the element to the right or to the left of another element. Same syntax as the other relational accessors.
_rightOf cell("Tote", _rightOf(strong("Type")))
_leftOf strong("Type", _leftOf(cell("Tote")))
Example — all six accessors on one page
navigateTo("https://demosite.appvance.com/");
click(span("Ruby on Rails Tote"));
setDescription("Using in Relational Accessor");
click(span("Products", _in(link("/products"))));
click(span("Ruby on Rails Tote"));
setDescription("Using rightOf Relational Accessor");
assertExists(cell("Tote", _rightOf(strong("Type"))));
setDescription("Using leftOf Relational Accessor");
assertExists(strong("Type", _leftOf(cell("Tote"))));
setDescription("Using above Relational Accessor");
assertExists(strong("Type", _above(strong("Size"))));
setDescription("Using below Relational Accessor");
assertExists(strong("Size", _below(strong("Type"))));