Does Laravel Work With Playwright?
Laravel and Playwright work excellently together for end-to-end testing of PHP web applications with no framework-specific barriers.
Quick Facts
How Laravel Works With Playwright
Laravel and Playwright integrate seamlessly because Playwright operates as an external browser automation tool that doesn't require framework-specific bindings. You run your Laravel application normally (via `php artisan serve` or on a deployed server), then point Playwright at it—whether you're using Node.js, Python, or other Playwright implementations. The typical workflow involves starting your Laravel dev server, optionally seeding test data via Laravel's database factories and seeders, then writing Playwright tests that navigate your app as a real user would. This separation of concerns means you test the actual rendered output rather than mocking, catching integration bugs that unit tests miss. For CI/CD pipelines, you'd typically spin up the Laravel container, run migrations and seeders, start the server, then run Playwright tests—all orchestrated through Docker or GitHub Actions. The main advantage over Laravel's built-in testing tools is cross-browser testing and visual regression detection, making it ideal for complex UIs where you need confidence across Chrome, Firefox, Safari, and mobile viewports.
Best Use Cases
Basic Laravel + Playwright Test
npm install -D @playwright/testimport { test, expect } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('http://localhost:8000');
});
test('user can register and login', async ({ page }) => {
await page.click('a:has-text("Register")');
await page.fill('input[name="name"]', 'John Doe');
await page.fill('input[name="email"]', 'john@example.com');
await page.fill('input[name="password"]', 'password123');
await page.click('button[type="submit"]');
await expect(page).toHaveURL('http://localhost:8000/dashboard');
await expect(page.locator('text=Welcome, John')).toBeVisible();
});Known Issues & Gotchas
Laravel's dev server isn't always ready when tests start in CI environments, causing connection timeouts
Fix: Use wait utilities or health check endpoints before starting tests; services like `wait-on` npm package help here
CSRF tokens and session cookies require explicit handling; Playwright doesn't automatically manage Laravel's session state between requests
Fix: Use Playwright's context and cookie management, or leverage Laravel's test helpers to generate valid tokens for POST requests
Database state pollution across tests—Playwright doesn't have native transaction rollback like Laravel's test suite
Fix: Seed a fresh database state before each test using database factories, or wrap in transactions at the application level
Debugging failures is harder because you're testing the HTTP layer, not running in-process
Fix: Use Playwright's debug mode (`--debug` flag), screenshots on failure, and trace files for post-mortem analysis
Alternatives
- •Laravel Dusk + PHPUnit: Laravel's native E2E testing solution using ChromeDriver, tightly integrated but limited to single browser
- •Cypress + Laravel: Similar developer experience to Playwright with excellent debugging, but heavier setup and no Safari support
- •Selenium + Laravel: More mature but significantly more verbose, slower, and harder to maintain than Playwright
Resources
Related Compatibility Guides
Explore more compatibility guides