Best Practices
Learn how to write effective, maintainable, and reliable tests with Chekov.
Writing Clear Instructions
The quality of your test automation depends heavily on how well you communicate with Chekov's AI. Here are some guidelines for writing effective instructions:
1// ❌ Unclear instructions2await ai('click it', { page });3await ai('type something', { page });45// ✅ Clear, specific instructions6await ai('Click the "Sign Up" button in the header', { page });7await ai('Type "test@example.com" in the email input field', { page });
Organizing Tests
Structure your tests logically and use descriptive names for better maintainability:
1// tests/auth/login.spec.ts2import { test } from '@playwright/test';3import { ai } from '@chekov/core';45test.describe('Authentication', () => {6test('successful login', async ({ page }) => {7await page.goto('/login');89await ai([10'Type "user@example.com" in the email field',11'Type "password123" in the password field',12'Click the Login button'13], { page });1415await ai('Verify we are redirected to the dashboard', { page });16});1718test('invalid credentials', async ({ page }) => {19await page.goto('/login');2021await ai([22'Type "invalid@example.com" in the email field',23'Type "wrongpassword" in the password field',24'Click the Login button'25], { page });2627await ai('Verify the error message "Invalid credentials" is shown', { page });28});29});
Optimizing Performance
Parallel Actions
Use parallel execution for independent actions to speed up your tests:
1test('parallel verifications', async ({ page }) => {2await page.goto('/dashboard');34// Run multiple checks in parallel5await ai([6'Verify the user profile section is visible',7'Verify the sidebar navigation is present',8'Verify the notifications panel exists',9'Verify the search bar is accessible'10], {11page,12parallelism: 4 // Execute all checks simultaneously13});14});
Reusable Functions
Create helper functions for common sequences of actions:
1// helpers/auth.ts2export async function login(page, email: string, password: string) {3await page.goto('/login');45await ai([6`Type "${email}" in the email field`,7`Type "${password}" in the password field`,8'Click the Login button'9], { page });1011await ai('Verify we are redirected to the dashboard', { page });12}1314// tests/profile.spec.ts15test('update profile', async ({ page }) => {16await login(page, 'user@example.com', 'password123');1718await ai([19'Click the Profile Settings button',20'Update the display name to "John Doe"',21'Click Save Changes'22], { page });23});
Error Handling
Implement robust error handling to make your tests more reliable:
1test('handle dynamic content', async ({ page }) => {2await page.goto('/dashboard');34try {5// Try to dismiss any popups that might appear6await ai('Click the "Accept Cookies" button if present', {7page,8timeout: 20009});10} catch {11// Continue if no popup found12}1314try {15await ai('Click the Settings button', { page });16} catch (error) {17console.error('Failed to access settings:', error);1819// Try an alternative approach20await ai('Click the user menu', { page });21await ai('Click Settings in the dropdown menu', { page });22}23});
Test Data Management
Keep your test data organized and maintainable:
1// fixtures/users.ts2export const testUsers = {3admin: {4email: 'admin@example.com',5password: 'admin123',6role: 'admin'7},8customer: {9email: 'customer@example.com',10password: 'customer123',11role: 'customer'12}13};1415// tests/admin.spec.ts16import { testUsers } from '../fixtures/users';1718test('admin features', async ({ page }) => {19const { email, password } = testUsers.admin;20await login(page, email, password);2122await ai([23'Verify the Admin Dashboard is accessible',24'Verify the User Management section is visible',25'Verify the System Settings are available'26], { page });27});
Key Takeaways
- Write clear, specific instructions for the AI
- Organize tests logically and use descriptive names
- Leverage parallel execution for better performance
- Create reusable helper functions
- Implement robust error handling
- Manage test data effectively
