| Install | |
|---|---|
composer require austinw/pest-plugin-browser-recording |
|
| PHP: | ^8.2 |
Interactive browser test recording for Pest v4 - Record user actions and automatically generate clean, maintainable Pest test code.
This plugin extends Pest's browser testing capabilities with interactive recording, allowing you to perform manual browser actions and automatically generate corresponding Pest test code. Perfect for:
composer require austinw/pest-plugin-browser-recording --dev
php artisan pest:install-browserit('can complete user registration', function() {
$page = visit('/register')
->record([
'timeout' => 3600,
'autoAssertions' => true,
'generateComments' => true
]);
// 🎬 Browser opens - perform your actions manually
// ✨ Code is automatically generated and injected here
// Example generated output:
// $page->type('#email', 'user@example.com')
// ->type('#password', 'secure123')
// ->click('#register-button')
// ->assertUrlContains('/dashboard')
// ->assertNoJavascriptErrors();
});
Automatically generates stable, maintainable selectors with intelligent prioritization:
// Priority: data-testid > id > name > class > hierarchy
'[data-testid="submit-button"]' // Highest priority - test-stable
'#email-field' // ID-based - reliable
'[name="user_email"]' // Name-based - form-friendly
'button.btn.btn-primary' // Class-based - styling-aware
'form > div:nth-child(2) > input' // Hierarchical - fallback
Global Configuration (config/recording.php):
<?php
return [
// Session settings
'timeout' => 1800, // 30 minutes
'autoAssertions' => true, // Generate assertions automatically
'generateComments' => true, // Add explanatory comments
// Selector strategy
'selectorPriority' => ['data-testid', 'id', 'name', 'class'],
'useStableSelectors' => true,
'includeAriaAttributes' => true,
// Recording behavior
'includeHoverActions' => false,
'captureKeyboardShortcuts' => false,
'recordScrollPosition' => false,
// File safety (disabled by default for smooth workflow)
'backupFiles' => false, // Enable for extra safety
'backupDirectory' => '.pest-recording-backups',
'maxBackupsPerFile' => 10,
// Code generation
'useTypeForInputs' => true, // Use type() vs fill() for better simulation
'chainMethods' => true, // Enable fluent method chaining
'deviceEmulation' => null, // 'mobile', 'desktop', or null
'colorScheme' => null, // 'dark', 'light', or null
];
Per-Test Configuration:
// Array-based configuration
$page->record([
'timeout' => 3600,
'autoAssertions' => false,
'deviceEmulation' => 'mobile'
]);
// Fluent API configuration
$config = (new RecordingConfig())
->timeout(3600)
->backupFiles(true)
->deviceEmulation('mobile')
->includeHoverActions(true);
Backups are disabled by default for a smooth development experience, but can be enabled for extra safety:
// Enable backups for cautious development
$page->record(['backupFiles' => true]);
// Or globally in config/recording.php
'backupFiles' => true,
'backupDirectory' => '.pest-recording-backups',
'maxBackupsPerFile' => 10,
'autoCleanupBackups' => true,
Device Emulation:
$page->record(['deviceEmulation' => 'mobile']); // Test mobile experience
$page->record(['colorScheme' => 'dark']); // Test dark mode
Custom Selector Strategies:
$page->record([
'selectorPriority' => ['data-cy', 'data-testid', 'id'],
'includeAriaAttributes' => true,
'useStableSelectors' => true
]);
Performance Optimization:
$page->record([
'throttleScrollEvents' => true,
'debounceInputEvents' => true,
'maxActionsPerSession' => 5000
]);
->record() in your test// Manual actions → Generated code
$page->type('#email', 'user@example.com')
->type('#password', 'secure123')
->click('#login-button');
// With auto-assertions enabled
$page->visit('/dashboard')
->click('#settings-link')
->assertUrlContains('/settings')
->assertNoJavascriptErrors();
// Device emulation
$page = visit('/')->on()->mobile();
$page->click('#mobile-menu')
->assertSee('Navigation Menu');
Browser doesn't open:
npx playwright installRecording doesn't start:
->record() is called on a page instanceCode injection fails:
->record() call'backupFiles' => trueGenerated selectors are unreliable:
data-testid attributes to your HTML'selectorPriority' => ['data-testid', 'id']'useStableSelectors' => trueThe plugin includes comprehensive error handling:
Run the test suite:
# All tests
vendor/bin/pest
# Unit tests only
vendor/bin/pest tests/Unit/
# With coverage
vendor/bin/pest --coverage --min=90
Test Statistics: 159+ tests with 559+ assertions covering all components.
record() methodBrowser Actions → JavaScript Recorder → PHP Communication → ActionRecorder
↓
Generated Test Code ← CodeGenerator ← SelectorStrategy ← Structured Actions
↓
FileInjector → Test File (with backups)
git checkout -b feature/amazing-featurevendor/bin/pestgit commit -m 'Add amazing feature'git push origin feature/amazing-featuregit clone https://github.com/AustinW/pest-plugin-browser-recording.git
cd pest-plugin-browser-recording
composer install
vendor/bin/pest
This package is open-sourced software licensed under the MIT license.
Built with ❤️ for the PHP testing community
Make browser testing as enjoyable as writing unit tests!