Updated recently
Last updated:
Quick Answer: GitHub Copilot generates Playwright and Cypress test code from natural language prompts. Cursor and Claude Code offer deeper context awareness. Use prompt templates that specify the framework, assertion style, and page object pattern for best results.
What is AI-Assisted Test Automation?
AI-assisted test automation uses large language models (LLMs) like GPT-4, Claude, and Copilot to generate, refactor, and maintain test code. Instead of writing every selector and assertion from scratch, you describe what you want tested and the AI generates the implementation.
Setting Up GitHub Copilot for Test Generation
Install the Copilot extension in VS Code or your preferred IDE. Ensure your project has Playwright or Cypress installed. Copilot learns from your existing test files, so the more tests you have, the better its suggestions.
Prompt Template 1: Generate a Playwright Test from a User Story
User Story: “As a user, I want to log in with my email and password so that I can access my dashboard.”
Prompt:
Write a Playwright test in TypeScript that:
1. Navigates to /login
2. Fills the email field with "[email protected]"
3. Fills the password field with "SecurePass123"
4. Clicks the login button
5. Asserts the URL changes to /dashboard
6. Asserts the welcome message contains "Welcome"
Use Page Object Model pattern with a LoginPage class.
Expected Output:
import { test, expect } from '@playwright/test';
import { LoginPage } from '../pages/LoginPage';
test('should login successfully with valid credentials', async ({ page }) => {
const loginPage = new LoginPage(page);
await loginPage.goto();
await loginPage.fillEmail('[email protected]');
await loginPage.fillPassword('SecurePass123');
await loginPage.clickLogin();
await expect(page).toHaveURL(/.*dashboard/);
await expect(page.locator('.welcome-message')).toContainText('Welcome');
});
Prompt Template 2: Generate Cypress Tests with API Interception
Prompt:
Write a Cypress test in JavaScript that:
1. Mocks the POST /api/login endpoint to return 200 with { token: "abc123" }
2. Visits /login
3. Fills email and password fields
4. Submits the form
5. Asserts the stubbed API was called
6. Asserts redirect to /dashboard
Use cy.intercept() for mocking.
Prompt Template 3: Generate Data-Driven Tests
Prompt:
Write a Playwright test that validates the registration form with 5 test cases:
- Empty email → shows "Email required"
- Invalid email format → shows "Invalid email"
- Short password → shows "Password must be 8+ characters"
- Valid data → redirects to /welcome
- Duplicate email → shows "Email already registered"
Use test.describe.parametrize() for data-driven testing.
Prompt Template 4: Generate API Tests
Prompt:
Write a Playwright API test that:
1. Sends GET /api/courses with Authorization header
2. Asserts response status is 200
3. Asserts response body is an array
4. Asserts each item has "id", "title", and "price" fields
5. Sends POST /api/courses with invalid data
6. Asserts response status is 400
7. Asserts error message contains "validation"
Using Cursor for Deeper Context
Cursor excels when you need the AI to understand your entire codebase. Unlike Copilot (which suggests based on open files), Cursor indexes your project and can reference existing page objects, fixtures, and test utilities when generating new tests.
Cursor Prompt:
Based on the existing LoginPage and DashboardPage objects in src/pages/,
write a complete login flow test that covers:
1. Successful login
2. Invalid credentials (expect error toast)
3. Session timeout (expect redirect to /login)
4. Remember me checkbox persistence
Using Claude Code for Complex Logic
Claude Code (Anthropic’s CLI tool) excels at generating test logic for complex business scenarios. It handles multi-step workflows, conditional assertions, and edge cases better than Copilot for intricate test scenarios.
Best Practices for AI Test Generation
- Be specific: Include framework, language, assertion style, and pattern (Page Object, fixtures)
- Provide context: Reference existing page objects, API endpoints, and test data
- Review carefully: AI-generated code needs human review for correctness and security
- Iterate: Refine prompts based on the output quality — small changes in phrasing make big differences
- Test the generated code: Never commit AI-generated tests without running them first
Limitations of AI Test Generation
- AI generates boilerplate well but may miss edge cases
- Complex business logic still requires human understanding
- Generated selectors may not be the most robust — review and improve
- AI doesn’t understand your application’s specific business rules
Learn the Fundamentals First
AI tools amplify your skills — they don’t replace the need to understand testing fundamentals. Build strong Playwright or Cypress knowledge before relying on AI generation. Explore Playwright Training at SkilBrill.
FAQ
Can Copilot write entire test suites?
Copilot can generate individual test cases and page objects, but building a complete, maintainable test suite requires human architecture decisions: test organization, data management, CI/CD integration, and flaky test handling.
Which AI tool is best for test generation?
GitHub Copilot for quick, inline suggestions. Cursor for project-wide context awareness. Claude Code for complex business logic scenarios. Use all three for different situations.
Do AI-generated tests need review?
Always. AI generates plausible code that may have subtle issues: incorrect selectors, missing assertions, or security vulnerabilities. Human review is mandatory before committing.
