Implementation:Getgauge Taiko Text Exists
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Testing |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for checking whether visible text content exists on a web page, provided by the Taiko library.
Description
The text().exists() API is a two-part composition: the text() selector function creates a TextWrapper that identifies DOM elements by their text content, and the .exists() method on ElementWrapper polls the page for the presence of matching elements within a configurable timeout window.
The text() function (defined in lib/taiko.js at lines 1978-1984) accepts a string or regular expression and returns a TextWrapper instance. The wrapper encapsulates the search criteria, including optional exact-match semantics and proximity selectors (such as near, above, below, toLeftOf, toRightOf). When exactMatch is false (the default), the selector matches any element whose text content contains the given string. When true, only elements whose full text content equals the given string are matched.
The .exists() method (defined in lib/elementWrapper/elementWrapper.js at lines 76-88) is inherited by TextWrapper from the base ElementWrapper class. It attempts to locate the first matching element by delegating to the internal firstElement helper. This helper retries the element lookup at a configurable polling interval (retryInterval, default 100ms) until either the element is found or the retry timeout (retryTimeout, default 10000ms) expires. If the element is found, it emits a "success" event with the message "Exists" and returns true. If the timeout expires and the element is not found, it catches the "not found" error, emits "Does not exist", and returns false. Any other error is re-thrown.
Usage
Use text().exists() to assert or conditionally check whether specific text content is visible on the current page. This is commonly used in test assertions to verify that expected text has rendered, in conditional branching to handle different page states, or with shortened timeouts (exists(0, 0)) to perform an immediate non-waiting check.
Code Reference
Source Location
- Repository: Taiko
- File (text selector):
lib/taiko.js - Lines: L1978-1984
- File (exists method):
lib/elementWrapper/elementWrapper.js - Lines: L76-88
Signature
// text() selector
function text(
text: string | RegExp,
_options?: { exactMatch?: boolean },
...args: RelativeSearchElement[]
) -> TextWrapper
// .exists() method on ElementWrapper (inherited by TextWrapper)
async exists(
retryInterval?: number,
retryTimeout?: number
) -> Promise<boolean>
Import
const { text } = require('taiko');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| text | string or RegExp | Yes | The text content to search for in the page DOM. Strings perform a contains match by default; RegExp values perform pattern matching. |
| _options.exactMatch | boolean | No (default: false) | When true, requires the element's full text content to exactly equal the search string rather than merely containing it. |
| ...args | RelativeSearchElement[] | No | Proximity selectors (near, above, below, toLeftOf, toRightOf) to narrow down the search to elements near a reference element.
|
| retryInterval | number | No (default: 100) | Polling interval in milliseconds between successive element lookup attempts. |
| retryTimeout | number | No (default: 10000) | Maximum wait time in milliseconds before returning false if the element is not found. |
Outputs
| Name | Type | Description |
|---|---|---|
| return (text) | TextWrapper | The text() function returns a TextWrapper instance that can be chained with .exists() and other methods.
|
| return (exists) | Promise<boolean> | Resolves to true if a matching element is found within the timeout period, false otherwise.
|
Usage Examples
Basic Text Existence Check
// Check if text exists anywhere on the page
await text('Welcome').exists();
// Returns: true or false
Exact Match
// Only match elements whose full text is exactly 'Welcome'
await text('Welcome', { exactMatch: true }).exists();
Regular Expression Match
// Match text using a regex pattern
await text(/Welcome \w+/).exists();
With Proximity Selector
const { text, below } = require('taiko');
// Check if 'Price' text exists below the 'Product Name' element
await text('Price', below('Product Name')).exists();
As a Test Assertion
const assert = require('assert');
// Assert that a success message is visible
const found = await text('Success').exists();
assert.ok(found, 'Success message should be visible');
Immediate Non-Waiting Check
// Short-circuit: check existence without waiting (no retries)
// Only use when confident no network calls or reloads are pending
const isPresent = await text('Loading...').exists(0, 0);
Custom Retry Configuration
// Wait up to 30 seconds, polling every 500ms
const appeared = await text('Data loaded').exists(500, 30000);