Learn how to handle various types of errors and implement robust error recovery in your Chekov tests.
| Error | Description | How to Handle |
|---|---|---|
| Rate Limit (429) | Too many requests | Implement backoff and retry |
| Authentication (401/403) | Invalid or missing API key | Verify API key configuration |
| Service Error (500) | Internal server error | Retry with exponential backoff |
import { test } from '@playwright/test';
import { ai } from '@chekov/core';
test('handle basic errors', async ({ page }) => {
try {
await ai('Click the submit button', { page });
} catch (error) {
// Handle specific error types
if (error.message.includes('Element not found')) {
console.log('Button not found, retrying after wait...');
await page.waitForTimeout(1000);
await ai('Click the submit button', { page });
} else {
throw error; // Re-throw unhandled errors
}
}
});async function withRetry(action: () => Promise<void>, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
await action();
return;
} catch (error) {
if (i === maxRetries - 1) throw error;
await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
}
}
}test('retry example', async ({ page }) => {
await withRetry(async () => {
await ai('Click the dynamic button', { page });
});
});test('handle dynamic content', async ({ page }) => {
// Wait for content to load
await ai('Wait for the dashboard to load', { page });
try {
await ai('Click the first item in the list', { page });
} catch (error) {
if (error.message.includes('Element not found')) {
// Refresh and retry
await page.reload();
await ai('Wait for the dashboard to load', { page });
await ai('Click the first item in the list', { page });
}
}
});test('handle network issues', async ({ page }) => {
// Simulate offline mode
await page.context().setOffline(true);
try {
await ai('Submit the form', { page });
} catch (error) {
// Handle network error
await page.context().setOffline(false);
await ai('Verify the error message is shown', { page });
await ai('Wait for network recovery', { page });
await ai('Submit the form again', { page });
}
});test('progressive enhancement', async ({ page }) => {
// Try modern approach first
try {
await ai('Click the React button', { page });
} catch {
// Fall back to traditional approach
await ai('Click the regular button', { page });
}
});test('recover from error state', async ({ page }) => {
try {
await ai('Complete the purchase', { page });
} catch (error) {
// Save current state
const cartItems = await page.evaluate(() =>
localStorage.getItem('cart')
);
// Refresh page
await page.reload();
// Restore state
await page.evaluate(
(items) => localStorage.setItem('cart', items),
cartItems
);
// Retry operation
await ai('Complete the purchase', { page });
}
});page.screenshot() to capture the state when errors occurPWDEBUG=1Remember: Good error handling makes your tests more reliable and easier to maintain. Always plan for failure cases and implement appropriate recovery mechanisms.