AI2Playwright is an innovative tool that combines the power of Playwright automation with AI-driven element locator identification. It allows you to write automated tests using natural language commands, which are then interpreted and executed using Playwright.
- π€ Natural Language Processing for test commands
- π― Smart element locator identification
- π Dynamic HTML context analysis
- π Playwright-based automation
- π§ Ollama AI integration
- Node.js (v16 or higher)
- Ollama installed and running locally
- Playwright Test dependencies
-
Download Ollama for Windows:
- Visit Ollama Releases
- Download the latest Windows release
-
Install Ollama:
- Run the downloaded installer
- Follow the installation wizard
- Ollama will be installed as a Windows service
curl -fsSL https://ollama.com/install.sh | shcurl -fsSL https://ollama.com/install.sh | sh-
Start Ollama service:
- Windows: The service starts automatically after installation
- Linux/MacOS: Run
ollama serve
-
Pull and run the required model:
# For CPU-only usage (recommended for most users)
ollama pull codellama
ollama run codellama
# For GPU usage (NVIDIA GPUs, better performance)
ollama pull codellama:7b-code-q4_K_M
ollama run codellama:7b-code-q4_K_MIn src/services/ollamaService.ts, you can configure which model to use:
// Default configuration (CPU)
constructor(
baseUrl: string = 'http://localhost:11434',
model: string = 'codellama' // Using CodeLlama for better code understanding
) {
this.baseUrl = baseUrl;
this.model = model;
}
// For GPU usage, change the model to:
constructor(
baseUrl: string = 'http://localhost:11434',
model: string = 'codellama:7b-code-q4_K_M'
) {
this.baseUrl = baseUrl;
this.model = model;
}We use CodeLlama instead of the standard Llama2 because:
- It's specifically trained on code and programming tasks
- Better understanding of HTML/CSS/JavaScript syntax
- More accurate element locator identification
- Improved code-related reasoning abilities
-
GPU Setup (NVIDIA):
- Requirements: NVIDIA GPU with CUDA support
- Memory: 8GB+ VRAM recommended
- Performance: Much faster inference
- Model: Use
codellama:7b-code-q4_K_M
-
CPU Setup:
- Requirements: No special hardware needed
- Memory: 8GB+ RAM recommended
- Performance: Slower but works everywhere
- Model: Use
codellama
- If you see errors about the model not being found:
# Check if the model is installed
ollama list
# If not, pull it again
ollama pull codellama- For GPU users, verify CUDA is working:
# Should show Ollama process when running
nvidia-smi- If the model is too slow:
- GPU users: Make sure you're using the quantized version (
codellama:7b-code-q4_K_M) - CPU users: Consider using a smaller model or upgrading RAM
- If you get poor locator identification:
- Make sure you're using CodeLlama, not the standard Llama2
- The model is specifically trained for code understanding
- Clone the repository:
git clone https://github.com/yourusername/AI2Playwright.git
cd AI2Playwright- Install dependencies:
npm install- Install Playwright browsers:
npx playwright install- Make sure Ollama is running locally with the default settings.
AI2Playwright/
βββ src/
β βββ actions/
β β βββ actionExecutor.ts # Handles execution of identified actions
β βββ auto/
β β βββ autoCommand.ts # Main command processor
β βββ locators/
β β βββ locatorIdentifier.ts # AI-powered element identification
β βββ services/
β βββ ollamaService.ts # Ollama AI service integration
βββ tests/
β βββ auto.spec.ts # Example test cases
β βββ server.spec.ts # Server test utilities
βββ DemoServer/
βββ startServer.ts # Demo server for testing
import { test } from '@playwright/test';
import { AutoCommand } from '../src/auto/autoCommand';
test('example test', async ({ page }) => {
const autoCommand = new AutoCommand(page);
// Use natural language commands
await autoCommand.auto('Click on counter button');
await autoCommand.auto('Type HELLO in search input');
});id- Element IDstestid- Test attributes (data-testid)name- Name attributestext- Visible text contentrole- ARIA rolesclass- CSS classesxpath- XPath expressionscss- CSS selectors
Run tests in headless mode:
npm testRun tests with browser visible:
npx playwright test --headedRun tests in debug mode:
npx playwright test --debug-
Command Processing: When you send a natural language command, it's processed by the
AutoCommandclass. -
Locator Identification: The
LocatorIdentifierclass:- Captures the current page HTML
- Sends the command and HTML context to Ollama
- Receives and processes AI suggestions for element location
-
Action Execution: The
ActionExecutorclass:- Determines the appropriate Playwright action
- Executes the action using the identified locator
- Handles various action types (click, type, etc.)
The system uses default configurations for:
- Server port: 3001 (configurable in tests)
- Ollama endpoint: http://localhost:11434 (configurable in OllamaService)
-
Element Identification:
- Prefer unique IDs when available
- Use data-testid for testing-specific attributes
- Fallback to other locators when needed
-
Commands:
- Use clear, descriptive commands
- Include the target element in the command
- Specify the action clearly
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Playwright team for the excellent browser automation framework
- Ollama team for the local AI model support