# Selectors

The selector builders (`text`, `res`, `desc`, …), the `Selector` they return and the match
modes. Guide: [Selectors](../../guide/selectors.md).

## <kbd>module</kbd> `tap_e2e.selectors`
Selector builders. A Selector builds the protocol's AST (tap.v1.Selector), a small expression tree the server forwards to the driver unchanged, so what a test builds is validated identically by the server and the driver; ``render()`` shows it and errors quote it. Building one does no I/O; every action resolves it again on the device. 

---

### <kbd>function</kbd> `text`

```python
text(value: 'str', mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>) → Selector
```

Visible text; Compose ``Text`` and View ``TextView``/``Button`` both expose it. 

---

### <kbd>function</kbd> `text_contains`

```python
text_contains(value: 'str') → Selector
```

Text containing ``value``. 

---

### <kbd>function</kbd> `text_starts_with`

```python
text_starts_with(value: 'str') → Selector
```

Text starting with ``value``. 

---

### <kbd>function</kbd> `text_matches`

```python
text_matches(re2: 'str') → Selector
```

Text fully matching the RE2 regular expression ``re2``. 

---

### <kbd>function</kbd> `desc`

```python
desc(value: 'str', mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>) → Selector
```

``contentDescription`` (View) / ``contentDescription`` semantics (Compose). 

---

### <kbd>function</kbd> `res_id`

```python
res_id(package_name: 'str', name: 'str') → Selector
```

Exactly the Android View resource id ``package:id/name``. 

---

### <kbd>function</kbd> `res`

```python
res(name: 'str') → Selector
```

Resource id ``name`` in any package (``<package>:id/name``), or a Compose ``testTag`` ``name`` under ``testTagsAsResourceId``. Under ``App.element`` only that app's nodes match; under ``Screen.element`` any visible package's. Use ``res_id`` to name the id's package. 

---

### <kbd>function</kbd> `class_name`

```python
class_name(
    value: 'str',
    mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>
) → Selector
```

Widget class name, e.g. ``android.widget.EditText``. 

---

### <kbd>function</kbd> `hint`

```python
hint(value: 'str', mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>) → Selector
```

Hint of an empty text field (an empty ``EditText`` shows its hint as its text). 

---

### <kbd>function</kbd> `clickable`

```python
clickable() → Selector
```

Any clickable node; combine with ``and_text`` etc. 

---

### <kbd>function</kbd> `scrollable`

```python
scrollable() → Selector
```

Any scrollable node; combine with ``and_res`` etc. 

---

### <kbd>function</kbd> `any_of`

```python
any_of(first: 'Selector', *rest: 'Selector') → Selector
```

Any of the selectors may match: ``any_of(text("OK"), text("Allow"), desc("Accept"))``. Scope and match choice come from ``first``; the others follow the operand rules of ``|``. 

---

### <kbd>function</kbd> `all_of`

```python
all_of(first: 'Selector', *rest: 'Selector') → Selector
```

All of the selectors must hold on one node. Scope and match choice come from ``first``; the others follow the operand rules of ``&``. 

---

### <kbd>class</kbd> `Selector`
An immutable description of one accessibility node. 

Build one with the module functions (``text``, ``res``, ``desc``, ...), narrow it with the methods below and combine selectors with ``&`` / ``|``; each returns a new Selector. Bind it with ``App.element`` for one package or ``Screen.element`` for whatever is visible. Selectors are values: equal when their protos are equal, and hashable (usable in sets and as dict keys). 

Composition never silently drops a part: an operand of ``&``, ``|`` or a ``has_*`` relation, and the target of ``descendant``/``child``, must not carry a different scope, and only the selector that ends up as the result may carry a match choice (``first``/``at``); anything else raises ``ValueError``. 

### <kbd>method</kbd> `Selector.__init__`

```python
__init__(_proto: 'pb.Selector') → None
```

---

#### <kbd>method</kbd> `Selector.and_class_name`

```python
and_class_name(
    value: 'str',
    mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>
) → Selector
```

Also require this widget class name. 

---

#### <kbd>method</kbd> `Selector.and_desc`

```python
and_desc(
    value: 'str',
    mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>
) → Selector
```

Also require this content description. 

---

#### <kbd>method</kbd> `Selector.and_hint`

```python
and_hint(
    value: 'str',
    mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>
) → Selector
```

Also require this hint (empty text fields). 

---

#### <kbd>method</kbd> `Selector.and_res`

```python
and_res(package_name_or_name: 'str', name: 'str | None' = None) → Selector
```

Also require a resource id, like Kotlin ``andRes``: ``and_res("pkg", "name")`` is exactly ``pkg:id/name``; ``and_res("name")`` is ``name`` in any package (see ``res``). 

---

#### <kbd>method</kbd> `Selector.and_text`

```python
and_text(
    value: 'str',
    mode: 'MatchMode' = <MatchMode.EXACT: 'EXACT'>
) → Selector
```

Also require this text (``mode``: ``EXACT``, ``CONTAINS``, ``STARTS_WITH``, ``ENDS_WITH``, ``REGEX``). 

---

#### <kbd>method</kbd> `Selector.at`

```python
at(index: 'int') → Selector
```

Accept the ``index``-th match (0-based) in accessibility order instead of requiring exactly one. 

---

#### <kbd>method</kbd> `Selector.checkable`

```python
checkable(value: 'bool' = True) → Selector
```

Require ``isCheckable == value``. 

---

#### <kbd>method</kbd> `Selector.checked`

```python
checked(value: 'bool' = True) → Selector
```

Require ``isChecked == value``. 

---

#### <kbd>method</kbd> `Selector.child`

```python
child(other: 'Selector') → Selector
```

Match the direct child ``other`` of this node; same rules as ``descendant``. 

---

#### <kbd>method</kbd> `Selector.clickable`

```python
clickable(value: 'bool' = True) → Selector
```

Require ``isClickable == value``. 

---

#### <kbd>method</kbd> `Selector.descendant`

```python
descendant(other: 'Selector') → Selector
```

An element matching ``other`` somewhere below this one. The result keeps ``other``'s match choice and this selector's scope. This selector must not carry a match choice: the ancestor is a bare predicate, so ``lst.at(2).descendant(x)`` would otherwise search under every list; pick among the results instead (``lst.descendant(x).at(2)``). Raises ``ValueError`` for a picked receiver or an ``other`` with a different scope. 

---

#### <kbd>method</kbd> `Selector.enabled`

```python
enabled(value: 'bool' = True) → Selector
```

Require ``isEnabled == value``. 

---

#### <kbd>method</kbd> `Selector.first`

```python
first() → Selector
```

Accept the first match in accessibility order instead of requiring exactly one. 

---

#### <kbd>method</kbd> `Selector.focusable`

```python
focusable(value: 'bool' = True) → Selector
```

Require ``isFocusable == value``. 

---

#### <kbd>method</kbd> `Selector.focused`

```python
focused(value: 'bool' = True) → Selector
```

Require ``isFocused == value``. 

---

#### <kbd>classmethod</kbd> `Selector.from_proto`

```python
from_proto(message: 'pb.Selector') → Selector
```

The Selector for a ``tap.v1.Selector`` message (``tap_e2e.proto``), e.g. one a ``ScreenNode`` or a stored recording holds; the message is copied. 

---

#### <kbd>method</kbd> `Selector.has_ancestor`

```python
has_ancestor(other: 'Selector') → Selector
```

Keep matching this node, but only when an ancestor matches ``other``. 

---

#### <kbd>method</kbd> `Selector.has_child`

```python
has_child(other: 'Selector') → Selector
```

Keep matching this node, but only when a direct child matches ``other``. 

---

#### <kbd>method</kbd> `Selector.has_descendant`

```python
has_descendant(other: 'Selector') → Selector
```

Keep matching this node, but only when some descendant matches ``other``. 

---

#### <kbd>method</kbd> `Selector.has_parent`

```python
has_parent(other: 'Selector') → Selector
```

Keep matching this node, but only when its parent matches ``other``. 

---

#### <kbd>method</kbd> `Selector.long_clickable`

```python
long_clickable(value: 'bool' = True) → Selector
```

Require ``isLongClickable == value``. 

---

#### <kbd>method</kbd> `Selector.render`

```python
render() → str
```

The selector as protobuf text, exactly as the device will see it. 

---

#### <kbd>method</kbd> `Selector.scrollable`

```python
scrollable(value: 'bool' = True) → Selector
```

Require ``isScrollable == value``. 

---

#### <kbd>method</kbd> `Selector.selected`

```python
selected(value: 'bool' = True) → Selector
```

Require ``isSelected == value``. 

---

#### <kbd>method</kbd> `Selector.to_proto`

```python
to_proto() → pb.Selector
```

A copy of the ``tap.v1.Selector`` message this Selector sends. 

