android_uiautomator_set_text
Set text on Android UI elements by resource ID using UIAutomator automation. Input text into text fields, forms, or input boxes during Android app testing.
Instructions
Set text on a UI element by resource ID using UIAutomator
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| resourceId | Yes | Resource ID of the element (e.g., com.example.app:id/text_input) | |
| text | Yes | Text to set in the element | |
| deviceSerial | No | Specific device serial number to target (optional) |
Implementation Reference
- src/handlers.ts:379-407 (handler)Main handler function that validates input arguments and calls the ADB wrapper to set text on the UI element.export async function uiautomatorSetTextHandler( adb: ADBWrapper, args: any ): Promise<{ content: Array<{ type: string; text: string }> }> { const { resourceId, text, deviceSerial } = args as UIAutomatorSetTextArgs; if (!resourceId || typeof resourceId !== 'string') { throw new Error('Invalid resource ID: resourceId must be a non-empty string'); } if (!text || typeof text !== 'string') { throw new Error('Invalid text: text must be a non-empty string'); } try { await adb.setTextByResourceId(resourceId, text, deviceSerial); return { content: [ { type: 'text', text: `Successfully set text on element with resource-id: ${resourceId}\nText: "${text}"`, }, ], }; } catch (error) { throw new Error(`UIAutomator set text failed: ${error instanceof Error ? error.message : String(error)}`); } }
- src/adb-wrapper.ts:568-602 (helper)Core logic that dumps the UI hierarchy using uiautomator, parses the element bounds by resource ID, clicks to focus, selects and deletes existing text, then inputs the new text.async setTextByResourceId(resourceId: string, text: string, deviceSerial?: string): Promise<void> { const device = await this.getTargetDevice(deviceSerial); const hierarchyFile = '/sdcard/window_dump.xml'; // Get the hierarchy to find the element await this.exec(['shell', 'uiautomator', 'dump', hierarchyFile], device); const { stdout } = await this.exec(['shell', 'cat', hierarchyFile], device); await this.exec(['shell', 'rm', hierarchyFile], device); // Extract bounds from XML to identify the element type const boundsRegex = new RegExp(`resource-id="${resourceId}"[^>]*bounds="\\[(\\d+),(\\d+)\\]\\[(\\d+),(\\d+)\\]"`); const match = stdout.match(boundsRegex); if (match) { const x1 = parseInt(match[1], 10); const y1 = parseInt(match[2], 10); const x2 = parseInt(match[3], 10); const y2 = parseInt(match[4], 10); // Click at the center to focus the element const centerX = Math.floor((x1 + x2) / 2); const centerY = Math.floor((y1 + y2) / 2); await this.touch(centerX, centerY, 100, device); // Clear existing text await this.sendKeyEvent('KEYEVENT_CTRL_A', device); await this.sendKeyEvent('KEYEVENT_DEL', device); // Input the new text await this.inputText(text, device); } else { throw new Error(`Element with resource-id ${resourceId} not found in UI hierarchy`); } }
- src/index.ts:230-251 (schema)MCP tool schema definition, including name, description, and input schema with required fields resourceId and text.{ name: 'android_uiautomator_set_text', description: 'Set text on a UI element by resource ID using UIAutomator', inputSchema: { type: 'object', properties: { resourceId: { type: 'string', description: 'Resource ID of the element (e.g., com.example.app:id/text_input)', }, text: { type: 'string', description: 'Text to set in the element', }, deviceSerial: { type: 'string', description: 'Specific device serial number to target (optional)', }, }, required: ['resourceId', 'text'], }, },
- src/index.ts:482-483 (registration)Tool dispatch registration in the switch statement of the CallToolRequestSchema handler.case 'android_uiautomator_set_text': return await uiautomatorSetTextHandler(this.adb, args);