// AI agents can autonomously write and run testsimport { test } from 'vitest';import { chrome } from 'testdriverai/presets';test('ai-generated test', async (context) => { const { testdriver } = await chrome(context, { url: 'https://example.com' }); await testdriver.find('More information link').click(); await testdriver.assert('IANA page is visible');});
AI-powered element location, assertions, and instructions:
Element Location
Assertions
Instructions
Copy
// Describe elements naturallyawait testdriver.find('submit button');await testdriver.find('email input in the login form');await testdriver.find('delete button in the top right corner');await testdriver.find('first product card in the grid');
// Future MCP interactivityawait testdriver.mcp.query("What is the CPU usage of my app?");await testdriver.mcp.query("Show me memory leaks in the last test run");await testdriver.mcp.query("What tests are flakiest this week?");await testdriver.mcp.query("Compare performance between branches");
MCP (Model Context Protocol) integration is planned for Q1 2025.
// AI analyzes how close the match wastry { await testdriver.find('submit button');} catch (error) { if (error.similarity > 0.8) { console.log('Very close match - likely wrong selector specificity'); } else if (error.similarity > 0.5) { console.log('Partial match - element might be present but different'); } else { console.log('No match - element likely not on screen'); }}
Visual Debugging
Copy
// Get debug screenshot for AI analysistry { await testdriver.find('button');} catch (error) { // AI can analyze the debug screenshot const screenshot = error.debugScreenshot; // Base64 image // Send to vision model for analysis const analysis = await visionModel.analyze(screenshot, "Why can't we find the submit button?" );}
// ✅ Good - specific and contextualawait testdriver.find('blue submit button below the login form');// ❌ Bad - too vagueawait testdriver.find('button');
2. Always Use secret: true for Passwords
Copy
// ✅ Protected from loggingawait testdriver.type('MyPassword123', { secret: true });// ❌ Password will be loggedawait testdriver.type('MyPassword123');
3. Handle Errors Gracefully
Copy
// ✅ Try alternative approachestry { await testdriver.find('optional element').click();} catch (error) { console.log('Element not found, trying alternative...'); await testdriver.find('alternative element').click();}
4. Save Sandbox IDs
Copy
// ✅ Enable reconnectionconst instance = await testdriver.connect();console.log('Sandbox ID:', instance.instanceId);// Save for debugging later