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.

1
import { ai } from '@chekov/core';
2
3
// Single action
4
await ai('Click the login button', { page });
5
6
// Multiple actions in sequence
7
await ai([
8
'Type "user@example.com" in the email field',
9
'Type "password123" in the password field',
10
'Click the Submit button'
11
], { page });
12
13
// Parallel actions
14
await ai([
15
'Verify the header is visible',
16
'Verify the navigation menu is present',
17
'Verify the footer contains copyright text'
18
], {
19
page,
20
parallelism: 3
21
});

Options

The ai function accepts various options to customize its behavior:

1
interface AiOptions {
2
// Required: Playwright page object
3
page: Page;
4
5
// Optional: Number of parallel actions (default: 1)
6
parallelism?: number;
7
8
// Optional: Timeout in milliseconds (default: 5000)
9
timeout?: number;
10
11
// Optional: Number of retry attempts (default: 3)
12
retries?: number;
13
14
// Optional: Custom error handler
15
onError?: (error: Error) => void;
16
17
// Optional: Custom success handler
18
onSuccess?: (result: ActionResult) => void;
19
}

Authentication

Chekov requires an API key for authentication. Set it using environment variables:

1
export CHEKOV_API_KEY=your_api_key_here

Error Handling

Chekov provides detailed error messages and retry mechanisms for robust testing:

1
try {
2
await ai('Click a non-existent button', { page });
3
} catch (error) {
4
if (error instanceof ElementNotFoundError) {
5
console.log('Element not found:', error.message);
6
} else if (error instanceof ActionFailedError) {
7
console.log('Action failed:', error.message);
8
} else {
9
throw error;
10
}
11
}

Advanced Usage

Custom Selectors

You can combine Chekov's AI capabilities with custom selectors for more precise control:

1
test('custom selectors', async ({ page }) => {
2
// Use AI for high-level actions
3
await ai('Navigate to the login page', { page });
4
5
// Mix with custom selectors
6
const emailInput = page.locator('input[name="email"]');
7
await emailInput.fill('user@example.com');
8
9
// Continue with AI
10
await ai('Click the Submit button', { page });
11
});

Conditional Actions

Handle dynamic scenarios with conditional AI actions:

1
test('conditional flow', async ({ page }) => {
2
await page.goto('/dashboard');
3
4
try {
5
// Try to dismiss a popup if it exists
6
await ai('Click the "Accept Cookies" button if present', {
7
page,
8
timeout: 2000
9
});
10
} catch {
11
// Continue if no popup found
12
}
13
14
// Proceed with main test flow
15
await 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

Next Steps