Implementation:Getgauge Taiko ElementWrapper
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Element_Selection |
| Last Updated | 2026-02-12 03:00 GMT |
Overview
Concrete tool for providing the base element wrapper pattern used by all element selectors in the Taiko browser automation library.
Description
Deprecation Notice: The get(retryInterval, retryTimeout) method on this class is deprecated since Taiko v1.0.3. Use elements() instead. Calling get() will emit a console warning: "DEPRECATED use .elements()".
ElementWrapper is the foundational base class from which all Taiko element selectors inherit. It does not target a specific HTML element type on its own; instead, it provides the shared infrastructure that every concrete wrapper (such as `ButtonWrapper`, `LinkWrapper`, `TextWrapper`, etc.) builds upon.
The constructor accepts five parameters: `elementType` (a string label like `"Button"` or `"Link"`), `query` (the query type such as `"label"` or `"text"`), `attrValuePairs` (the user-supplied selector value), `_options` (configuration options), and variadic `...args` (relative search elements for proximity filtering). It validates that an `ElementWrapper` instance is not accidentally passed as a selector value, then delegates to `prepareParameters()` to normalize the selector and options into a consistent internal form.
The class exposes the following methods:
- `get(retryInterval, retryTimeout)` -- Deprecated since v1.0.3. Delegates to `elements()` with a console warning.
- `description` -- Property getter that returns the human-readable description of the selector, matching what is printed in the Taiko REPL.
- `exists(retryInterval, retryTimeout)` -- Checks whether the element exists in the DOM. Returns `true` or `false`. Emits success events via `descEvent`.
- `text()` -- Returns the `innerText` of the first matched element.
- `isVisible(retryInterval, retryTimeout)` -- Uses `IntersectionObserver` in the browser context to determine whether the first matched element is fully visible (intersection ratio of 1).
- `isDisabled(retryInterval, retryTimeout)` -- Checks if the first matched element is disabled.
- `isDraggable(retryInterval, retryTimeout)` -- Checks if the first matched element has the HTML `draggable` attribute.
- `attribute(name)` -- Returns the value of the specified attribute on the first matched element.
- `elements(retryInterval, retryTimeout)` -- Returns an array of all matching `Element` objects, waiting implicitly with configurable retry intervals and timeouts.
- `element(index, retryInterval, retryTimeout)` -- Returns a single `Element` at the specified zero-based index, throwing an error if the index is out of range.
Subclasses must define the `_get` method (either as an async function or via `getElementGetter()`) to implement the actual DOM query logic.
Usage
`ElementWrapper` is not used directly as a selector in test scripts. Instead, it is the internal base class that every Taiko element selector extends. Understanding its API is essential for working with any element returned by Taiko selectors, since methods like `exists()`, `text()`, `isVisible()`, `elements()`, and `attribute()` are all inherited from this class.
Code Reference
Source Location
- Repository: Getgauge_Taiko
- File: lib/elementWrapper/elementWrapper.js
- Lines: 1-224
Signature
class ElementWrapper {
constructor(elementType, query, attrValuePairs, _options, ...args) { ... }
async get(retryInterval, retryTimeout) { ... } // deprecated
get description() { ... }
async exists(retryInterval, retryTimeout) { ... }
async text() { ... }
async isVisible(retryInterval, retryTimeout) { ... }
async isDisabled(retryInterval, retryTimeout) { ... }
async isDraggable(retryInterval, retryTimeout) { ... }
async attribute(name) { ... }
async elements(retryInterval, retryTimeout) { ... }
async element(index, retryInterval, retryTimeout) { ... }
}
Import
// Internal import used by subclasses
const ElementWrapper = require('./elementWrapper');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| elementType | string | Yes | A human-readable label for the element type (e.g., `"Button"`, `"Link"`, `"ListItem"`). Used in error messages and descriptions. |
| query | string | Yes | The query property name (e.g., `"label"`, `"text"`, `"query"`). Used to build the human-readable description. |
| attrValuePairs | string / Object / RegExp / RelativeSearchElement | Yes | The selector value. Can be a label string, attribute-value pair object, regular expression, or a relative search element. Passing an `ElementWrapper` instance throws a `TypeError`. |
| _options | Object / RelativeSearchElement | No | Configuration options or a relative search element. If a `RelativeSearchElement` is passed here, it is moved to the args array and options defaults to `{}`. |
| ...args | RelativeSearchElement[] | No | Zero or more relative search elements for proximity-based filtering. |
Outputs
| Name | Type | Description |
|---|---|---|
| ElementWrapper | ElementWrapper | The constructed wrapper instance. All query methods (`exists()`, `text()`, `elements()`, etc.) return Promises that resolve to their respective types. |
Method Details
| Method | Parameters | Return Type | Description |
|---|---|---|---|
| `get()` | retryInterval, retryTimeout | `Promise<Element[]>` | Deprecated. Delegates to `elements()`. |
| `description` | (property) | `string` | Returns the human-readable description string. |
| `exists()` | retryInterval, retryTimeout | `Promise<boolean>` | Returns `true` if at least one matching element is found, `false` otherwise. |
| `text()` | (none) | `Promise<string>` | Returns the `innerText` of the first matched element. |
| `isVisible()` | retryInterval, retryTimeout | `Promise<boolean>` | Returns `true` if the first matched element has an intersection ratio of 1 (fully visible in viewport). |
| `isDisabled()` | retryInterval, retryTimeout | `Promise<boolean>` | Returns `true` if the first matched element is disabled. |
| `isDraggable()` | retryInterval, retryTimeout | `Promise<boolean>` | Returns `true` if the first matched element has the `draggable` attribute. |
| `attribute()` | name | `Promise<string>` | Returns the value of the named attribute on the first matched element. |
| `elements()` | retryInterval, retryTimeout | `Promise<Element[]>` | Returns all matching elements as an array. |
| `element()` | index, retryInterval, retryTimeout | `Promise<Element>` | Returns the element at the specified zero-based index. Throws if index is out of range. |
Usage Examples
Basic Usage
const { openBrowser, goto, link, closeBrowser } = require('taiko');
(async () => {
await openBrowser();
await goto('https://example.com');
// ElementWrapper methods are available on all selectors
const exists = await link('About').exists();
const text = await link('About').text();
const visible = await link('About').isVisible();
console.log('Exists:', exists, 'Text:', text, 'Visible:', visible);
await closeBrowser();
})();
Iterating Over All Matched Elements
const { openBrowser, goto, $, closeBrowser } = require('taiko');
(async () => {
await openBrowser();
await goto('https://example.com');
const allLinks = await $('a').elements();
for (const el of allLinks) {
console.log(await el.text());
}
await closeBrowser();
})();
Accessing a Specific Element by Index
const { openBrowser, goto, $, closeBrowser } = require('taiko');
(async () => {
await openBrowser();
await goto('https://example.com');
const thirdLink = await $('a').element(2);
console.log(await thirdLink.text());
await closeBrowser();
})();