An intelligent test automation tool that leverages Google's Gemini AI to automatically generate Playwright test scripts. Simply provide a URL and requirements, and the AI will generate clean, production-ready test code for you.
- π― AI-Powered Test Generation - Uses Google Gemini 2.5 Flash model to intelligently generate tests
- π Playwright Integration - Generates modern Playwright test syntax with best practices
- π Smart Locators - Automatically creates proper element selectors
- β Built-in Assertions - Includes relevant assertions and error handling
- π¨ Clean Code Output - Generates well-formatted, readable test code
- β‘ Performance Testing - Can include performance tests when applicable
- π§ Configurable - Easy to customize prompts and requirements
- Node.js 18+
- npm or yarn
- Google Gemini API key (free tier available at Google AI Studio)
- Clone the repository
git clone https://github.com/yourusername/ai-playwright-genAI.git
cd ai-playwright-genAI- Install dependencies
npm install- Set up environment variables
# Copy the example file
cp .env.example .env
# Add your Gemini API key to .env
echo "GEMINI_API_KEY=your_api_key_here" >> .env
echo "BASE_URL=https://demoqa.com" >> .env- Get your Gemini API Key
- Visit Google AI Studio
- Click "Get API Key"
- Create a new API key (free tier available)
- Copy and paste into your
.envfile
Run the default test generation:
npm startThis will generate a test for the URL specified in your .env file's BASE_URL variable.
Edit the bottom of gen-ai/ai-generate-test.js to customize:
generateTest(
"https://your-website.com",
"Verify login functionality and test the dashboard"
);Then run:
npm startThe script will generate a file at gen-ai/output/generated-test.spec.js:
import { test, expect } from '@playwright/test';
const BASE_URL = 'https://demoqa.com';
test.describe('Main Page', () => {
test('should have correct page title', async ({ page }) => {
await page.goto(BASE_URL);
await expect(page).toHaveTitle(/DemoQA/);
});
test('should have interactive elements', async ({ page }) => {
await page.goto(BASE_URL);
const mainButton = page.locator('button:has-text("Elements")');
await expect(mainButton).toBeVisible();
await expect(mainButton).toBeEnabled();
});
});ai-playwright-genAI/
βββ gen-ai/
β βββ ai-generate-test.js # Main script
β βββ output/ # Generated test files
βββ .env.example # Environment variables template
βββ .env # Local environment (not in git)
βββ package.json # Project dependencies
βββ README.md # This file
Create a .env file in the root directory:
# Required: Your Google Gemini API key
GEMINI_API_KEY=your_api_key_here
# Optional: Base URL for tests (default: https://demoqa.com)
BASE_URL=https://your-website.comYou can change the Gemini model in gen-ai/ai-generate-test.js:
const genAIModel = "gemini-2.5-flash"; // Change this to another modelAvailable models: gemini-2.5-flash, gemini-2.0-pro, etc.
- @google/generative-ai - Google's Generative AI SDK
- playwright - Web automation and testing library
- dotenv - Environment variable management
- You provide: A URL and test requirements
- AI analyzes: The prompt and requirements
- AI generates: Clean, working Playwright test code
- Output saved: To
gen-ai/output/generated-test.spec.js
- Generated tests should be reviewed and validated before running in CI/CD pipelines
- The AI does its best to create functional code, but complex scenarios may need manual adjustments
- Always test generated code locally first
- The free Gemini API tier has rate limits - be mindful of usage
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
See CONTRIBUTING.md for more details.
This project is licensed under the ISC License - see the LICENSE file for details.
- Make sure you've created a
.envfile - Verify your
GEMINI_API_KEYis correctly set - Check that the key is valid and not expired
- Ensure
"type": "module"is set inpackage.json - Delete
node_modulesand runnpm installagain
- Check that the
gen-ai/output/directory is writable - Check console for error messages
- Verify your Gemini API key has quota remaining
- Support for multiple test files generation
- Visual element detection using screenshots
- Integration with CI/CD pipelines
- Test execution and reporting
- Support for other testing frameworks (Cypress, etc.)
- Web UI for easier test generation
Happy Testing! π