Selectors
A selector only builds an element object and does not look anything up yet; the lookup happens when you call exists / click / getText / getRect. Compared with findElement it can pick the n-th match and include hidden elements, but it does not wait.
Кратко о модуле
- ·Selector names match the values of findElement's first argument one to one
- ·accessibilityId is the most stable: it does not change with the UI language or copy edits
- ·xpath is flexible but slow — prefer classChain / predicate when they can express the same thing
client.label#
Match elements whose text is exactly the value.
client.label(value: string, visibleOnly?: boolean, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The element's text; must match exactly. |
visibleOnly | boolean | Нет | true | Only match visible elements. Pass false to include hidden ones (covered, or scrolled off screen). |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var ok = client.label('OK');
if (ok.exists()) {
ok.click();
}
var second = client.label('Follow', true, 1); // the second "Follow"См. также client.labelContains、findElement
client.labelContains#
Match elements whose text contains the value.
client.labelContains(value: string, visibleOnly?: boolean, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The text to look for inside the element's text. |
visibleOnly | boolean | Нет | true | Only match visible elements. Pass false to include hidden ones (covered, or scrolled off screen). |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var follow = client.labelContains('Follow');
if (follow.exists()) {
follow.click();
}См. также client.label
client.accessibilityId#
Match by the element's name (its accessibilityIdentifier, set by the app's developer). The most stable way to locate an element: it does not change with the UI language or copy edits.
client.accessibilityId(value: string, visibleOnly?: boolean, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The element's name. Click the element in the console's inspector to see it. |
visibleOnly | boolean | Нет | true | Only match visible elements. Pass false to include hidden ones (covered, or scrolled off screen). |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var submit = client.accessibilityId('submit_button');
if (submit.exists()) {
submit.click();
}См. также findElement
client.xpath#
Match by XPath. Flexible but slow: the whole element tree is serialised before the query runs, which can take seconds on busy screens.
client.xpath(value: string): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The XPath expression. The first match is used; write (//expression)[n] for the n-th. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Примечания
- If classChain or predicate can express it, do not use XPath.
Пример
var btn = client.xpath('//XCUIElementTypeButton[@name="OK"]');
if (btn.exists()) {
btn.click();
}См. также client.classChain、client.predicate
client.classChain#
Match by an iOS class chain — much faster than XPath.
client.classChain(value: string, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The class chain, for example **/XCUIElementTypeCell[`label CONTAINS "General"`]. |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var cell = client.classChain('**/XCUIElementTypeButton[`label == "OK"`]');
if (cell.exists()) {
cell.click();
}См. также client.predicate、client.xpath
client.predicate#
Match with an NSPredicate expression.
client.predicate(value: string, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The predicate, for example label BEGINSWITH 'Con' or type == 'XCUIElementTypeButton' AND visible == true. |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var btn = client.predicate('type == "XCUIElementTypeButton" AND label CONTAINS "Follow"');
if (btn.exists()) {
btn.click();
}См. также client.classChain
client.className#
Match by element type, for example XCUIElementTypeCell or XCUIElementTypeSwitch.
client.className(value: string, index?: number): Element;Параметры
| Параметр | Тип | Обязательный | По умолчанию | Описание |
|---|---|---|---|---|
value | string | Да | — | The element type name. |
index | number | Нет | 0 | Which match to take when there are several, counting from 0. |
Возвращаемое значение
| Тип | Описание |
|---|---|
Element | An element object. Nothing is looked up yet — the lookup happens when you call exists / click / getText / getRect. |
Пример
var firstCell = client.className('XCUIElementTypeCell', 0);
if (firstCell.exists()) {
firstCell.click();
}См. также client.predicate