API Overview
Learn about Chekov's core API functions and how to use them effectively in your tests.
Core Functions
ai Function
The main function for interacting with web elements using natural language instructions.
1import { ai } from '@chekov/core';23// Single action4await ai('Click the login button', { page });56// Multiple actions in sequence7await ai([8'Type "user@example.com" in the email field',9'Type "password123" in the password field',10'Click the Submit button'11], { page });1213// Parallel actions14await ai([15'Verify the header is visible',16'Verify the navigation menu is present',17'Verify the footer contains copyright text'18], {19page,20parallelism: 321});
Options
The ai function accepts various options to customize its behavior:
1interface AiOptions {2// Required: Playwright page object3page: Page;45// Optional: Number of parallel actions (default: 1)6parallelism?: number;78// Optional: Timeout in milliseconds (default: 5000)9timeout?: number;1011// Optional: Number of retry attempts (default: 3)12retries?: number;1314// Optional: Custom error handler15onError?: (error: Error) => void;1617// Optional: Custom success handler18onSuccess?: (result: ActionResult) => void;19}
Authentication
Chekov requires an API key for authentication. Set it using environment variables:
1export CHEKOV_API_KEY=your_api_key_here
Error Handling
Chekov provides detailed error messages and retry mechanisms for robust testing:
1try {2await ai('Click a non-existent button', { page });3} catch (error) {4if (error instanceof ElementNotFoundError) {5console.log('Element not found:', error.message);6} else if (error instanceof ActionFailedError) {7console.log('Action failed:', error.message);8} else {9throw error;10}11}
Advanced Usage
Custom Selectors
You can combine Chekov's AI capabilities with custom selectors for more precise control:
1test('custom selectors', async ({ page }) => {2// Use AI for high-level actions3await ai('Navigate to the login page', { page });45// Mix with custom selectors6const emailInput = page.locator('input[name="email"]');7await emailInput.fill('user@example.com');89// Continue with AI10await ai('Click the Submit button', { page });11});
Conditional Actions
Handle dynamic scenarios with conditional AI actions:
1test('conditional flow', async ({ page }) => {2await page.goto('/dashboard');34try {5// Try to dismiss a popup if it exists6await ai('Click the "Accept Cookies" button if present', {7page,8timeout: 20009});10} catch {11// Continue if no popup found12}1314// Proceed with main test flow15await ai('Click the Settings button', { page });16});
Rate Limits
- Free tier: 100 API calls per month
- Pro tier: 10,000 API calls per month
- Enterprise tier: Custom limits available
Best Practices
- Use clear, descriptive instructions for better accuracy
- Group related actions for improved performance
- Set appropriate timeouts for your application
- Implement proper error handling
- Use parallel execution when possible
