clearText
Remove text from the currently focused input field on Android or iOS devices using this mobile automation tool.
Instructions
Clear text from the currently focused input field
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| platform | Yes | Platform of the device |
Implementation Reference
- src/features/action/ClearText.ts:8-108 (handler)ClearText class containing the core logic for clearing text in the focused input field. Uses view hierarchy to find focused element text length and sends delete keyevents.export class ClearText extends BaseVisualChange { private elementUtils: ElementUtils; constructor(device: BootedDevice, adb: AdbUtils | null = null, axe: Axe | null = null) { super(device, adb, axe); this.elementUtils = new ElementUtils(); } async execute(progress?: ProgressCallback): Promise<ClearTextResult> { return this.observedInteraction( async (observeResult: ObserveResult) => { try { if (!observeResult.viewHierarchy) { // Fallback: if we can't get view hierarchy, use a reasonable default await this.clearWithDeletes(200); return { success: true }; } let textLength = 0; // Look for focused elements first by traversing and checking attributes textLength = this.findFocusedElementTextLength(observeResult.viewHierarchy); // TODO: Move cursor to the end of the text if (textLength > 0) { await this.clearWithDeletes(textLength); } return { success: true }; } catch (error) { return { success: false, error: "Failed to clear text" }; } }, { changeExpected: false, // TODO: can only make this true once we know for sure there was text in the text field tolerancePercent: 0.00, timeoutMs: 100, progress } ); } private findFocusedElementTextLength(viewHierarchy: any): number { let textLength = 0; const rootNodes = this.elementUtils.extractRootNodes(viewHierarchy); for (const rootNode of rootNodes) { this.elementUtils.traverseNode(rootNode, (node: any) => { const nodeProperties = this.elementUtils.extractNodeProperties(node); if ((nodeProperties.focused === "true" || nodeProperties.focused === true) && nodeProperties.text && typeof nodeProperties.text === "string") { textLength = Math.max(textLength, nodeProperties.text.length); } }); } return textLength; } private findAnyTextInputLength(viewHierarchy: any): number { let textLength = 0; const rootNodes = this.elementUtils.extractRootNodes(viewHierarchy); // Common input field classes const inputClasses = [ "android.widget.EditText", "android.widget.AutoCompleteTextView", "android.widget.MultiAutoCompleteTextView", "androidx.appcompat.widget.AppCompatEditText" ]; for (const rootNode of rootNodes) { this.elementUtils.traverseNode(rootNode, (node: any) => { const nodeProperties = this.elementUtils.extractNodeProperties(node); if (nodeProperties.class && inputClasses.some(cls => nodeProperties.class.includes(cls)) && nodeProperties.text && typeof nodeProperties.text === "string") { textLength = Math.max(textLength, nodeProperties.text.length); } }); } return textLength; } private async clearWithDeletes(count: number): Promise<void> { // Move to end of field first to ensure we're deleting from the right position await this.adb.executeCommand("shell input keyevent KEYCODE_MOVE_END"); // Send KEYCODE_DEL commands with no delay for (let i = 0; i < count; i++) { await this.adb.executeCommand("shell input keyevent KEYCODE_DEL"); } } }
- src/server/interactionTools.ts:256-269 (handler)Tool handler function that instantiates ClearText and executes it, returning formatted response.const clearTextHandler = async (device: BootedDevice, args: ClearTextArgs, progress?: ProgressCallback) => { try { const clearText = new ClearText(device); const result = await clearText.execute(progress); return createJSONToolResponse({ message: "Cleared text from input field", observation: result.observation, ...result }); } catch (error) { throw new ActionableError(`Failed to clear text: ${error}`); } };
- Zod schema defining input arguments for the clearText tool (platform only).export const clearTextSchema = z.object({ platform: z.enum(["android", "ios"]).describe("Platform of the device") });
- src/server/interactionTools.ts:666-672 (registration)Registers the clearText tool with ToolRegistry, providing name, description, schema, and handler.ToolRegistry.registerDeviceAware( "clearText", "Clear text from the currently focused input field", clearTextSchema, clearTextHandler, true // Supports progress notifications );
- src/models/ClearTextResult.ts:6-10 (schema)TypeScript interface defining the return type of the clearText operation.export interface ClearTextResult { success: boolean; observation?: ObserveResult; error?: string; }