TestDriver’s element finding system uses AI to locate elements on screen using natural language descriptions. The find() method returns an Element object that you can interact with.
Locate an element on screen using a natural language description.
Copy
const element = await testdriver.find(description)
Parameters:
description (string) - Natural language description of the element to find
Returns:Promise<Element> - Element instance that has been locatedExample:
Copy
// Find a buttonconst submitButton = await testdriver.find('the submit button');// Find an input field with contextconst emailField = await testdriver.find('email input field in the login form');// Find an element by visual characteristicsconst redButton = await testdriver.find('red button in the top right corner');
Be specific in your descriptions. Include visual details, location context, or nearby text to improve accuracy.
Re-locate the element, optionally with a new description.
Copy
await element.find(newDescription)
Parameters:
newDescription (string, optional) - New description to search for
Returns:Promise<Element> - This element instanceExample:
Copy
// Re-locate if the UI changedconst element = await testdriver.find('submit button');// ... page updates ...await element.find(); // Re-locate with same description// Or update the descriptionawait element.find('blue submit button'); // Now looking for blue button
Direct access to coordinate values. Always available after successful find().
Copy
element.x // Top-left X coordinate (number)element.y // Top-left Y coordinate (number)element.centerX // Center X coordinate (number)element.centerY // Center Y coordinate (number)
Example:
Copy
const button = await testdriver.find('submit button');console.log(`Button at: (${button.x}, ${button.y})`);console.log(`Button center: (${button.centerX}, ${button.centerY})`);// Use for custom mouse operationsawait testdriver.click(button.centerX, button.centerY);
Complete bounding box information including position and dimensions.
Copy
element.boundingBox
Returns:Object | null - Bounding box with all dimension data
Copy
{ x: number, // Top-left X y: number, // Top-left Y width: number, // Width in pixels height: number // Height in pixels}
Example:
Copy
const element = await testdriver.find('dialog box');if (element.boundingBox) { const { x, y, width, height } = element.boundingBox; console.log(`Dialog: ${width}x${height} at (${x}, ${y})`); // Calculate if element is in viewport const rightEdge = x + width; const bottomEdge = y + height; console.log(`Element extends to (${rightEdge}, ${bottomEdge})`);}
// Element that moves or changesconst notification = await testdriver.find('success notification');// Do something that might cause it to moveawait testdriver.scroll('down', 300);// Re-locate the elementawait notification.find();if (notification.found()) { await notification.click();}
Include visual details, position context, and nearby text:
Copy
// ❌ Too vagueawait testdriver.find('button');// ✅ Specificawait testdriver.find('blue submit button below the email field');
Check if element was found
Always verify elements were located before interacting:
Copy
const element = await testdriver.find('submit button');if (!element.found()) { throw new Error('Submit button not found');}await element.click();
Use polling for dynamic content
Don’t assume elements are immediately visible:
Copy
let element;for (let i = 0; i < 30; i++) { element = await testdriver.find('loading complete message'); if (element.found()) break; await new Promise(resolve => setTimeout(resolve, 1000));}
Reuse element references when possible
If you need to interact with the same element multiple times, reuse the reference:
Copy
const input = await testdriver.find('search input');await input.click();await testdriver.type('first search');await testdriver.pressKeys(['enter']);// Re-use the same element referenceawait input.click();await testdriver.pressKeys(['ctrl', 'a']); // Select allawait testdriver.type('second search');