Implementation:Getgauge Taiko Clear
| Knowledge Sources | |
|---|---|
| Domains | Browser_Automation, Form_Interaction |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for removing existing content from input fields provided by the Taiko library.
Description
The clear() function removes all content from a specified input field or the currently focused element. It uses the selectAll+backspace approach, which selects all text in the field and then deletes it. This method correctly triggers DOM event handlers (input, change, keydown, keyup), ensuring that framework-level state management (React, Vue, Angular) updates properly. This is in contrast to direct value manipulation (element.value = ), which does not fire these events.
When called without a selector, clear() operates on the currently focused element, making it convenient to chain with focus() or use after clicking into a field.
Usage
Use clear() before writing new values into text fields to ensure the field is empty. Also use it to test form validation by clearing required fields and verifying that error messages appear. Common in edit/update workflows where existing data must be replaced with new values.
Code Reference
Source Location
- Repository: Taiko
- File:
lib/taiko.js(L1120-1134, public API),lib/actions/clear.js(L25-42, implementation)
Signature
clear(selector, options) -> Promise<void>
Import
const { clear, textBox } = require('taiko');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| selector | Selector |
No | Target input element to clear. If omitted, clears the currently focused element. Typically a textBox() selector.
|
| options | Object |
No | Configuration options for the clear operation. |
| options.force | boolean |
No | If true, bypasses actionability checks. Default false.
|
| options.waitForNavigation | boolean |
No | If true, waits for any navigation triggered by clearing to complete. Default true.
|
| options.navigationTimeout | number |
No | Maximum time in milliseconds to wait for navigation. Default 30000.
|
Outputs
| Name | Type | Description |
|---|---|---|
| return | Promise<void> |
Resolves when the input field has been successfully cleared. Rejects if the element is not found or not editable. |
Usage Examples
Clear the Currently Focused Element
// Clears whatever element currently has focus
await clear();
Clear a Specific Text Box
// Clear a text box identified by its placeholder text
await clear(textBox({placeholder: 'Email'}));
Clear by Label Text
// Clear a text box identified by its label
await clear(textBox('Name'));
Force Clear
// Clear with force option to bypass actionability checks
await clear(textBox('Name'), {force: true});
Clear Before Typing New Value
// Common pattern: clear existing value, then type new one
await clear(textBox('Email'));
await write('newemail@example.com', into(textBox('Email')));