Scale Laravel Dusk Browser Tests with Headless Chrome
In this article
Scaling Laravel Dusk browser tests with headless Chrome lets you run more tests in parallel, reduce CI time, and catch front-end regressions faster. This guide shows you how to configure Dusk for parallel execution, manage databases, and avoid flaky tests when running headless.
What You'll Need
- A Laravel application with Dusk installed (
composer require laravel/dusk --dev) andphp artisan dusk:installrun. - Chrome or Chromium installed (or use
chromedriverviaphp artisan dusk:chrome-driver). - PHP 7.3+ and Composer.
- Access to your CI environment (e.g., GitHub Actions, GitLab CI) or local machine with multiple CPU cores.
Step-by-Step
- Install and configure Dusk. Ensure
DuskServiceProvideris registered only inlocalandtestingenvironments. Inapp/Providers/DuskServiceProvider.php, wrap registration withif ($this->app->environment('local', 'testing')). - Set up a dedicated testing database. In
phpunit.xml(orDuskTestCase.php), set environment variables likeDB_CONNECTION=mysql,DB_DATABASE=dusk_test,DB_USERNAME=root, etc. Use a separate database to avoid wiping development data. - Run migrations before each test suite. In
DuskTestCase::setUp(), call$this->artisan('migrate:fresh')once per process, not per test. Use a static flag or run migrations in CI before starting tests. - Configure headless Chrome options. In
DuskTestCase::driver(), set--headless,--disable-gpu,--no-sandbox(for CI), and--window-size=1920,1080. Example:return RemoteWebDriver::create( 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability( ChromeOptions::fromArray(['args' => ['--headless', '--disable-gpu', '--no-sandbox', '--window-size=1920,1080']]) ) ); - Start ChromeDriver manually or via CI. In CI, run
php artisan dusk:chrome-driverto download the correct driver, then start it withchromedriver --port=9515in the background. Locally, you can usephp artisan duskwhich auto-starts the driver. - Run tests in parallel. Use PHPUnit's parallel testing feature (PHPUnit 9.3+). In
phpunit.xml, setprocessIsolation="true"and use--parallelwith--testsuitewhen running. Example command:php artisan dusk --parallel --testsuite=Dusk
Or usevendor/bin/paratestfor more control. Ensure each process uses a unique database or use transactions. - Isolate test data. Use
DatabaseTransactionstrait in Dusk tests, but note that Dusk runs in a separate browser process, so transactions won't roll back automatically. Instead, useRefreshDatabasewith a per-test unique database name (e.g.,dusk_test_1,dusk_test_2) by setting the DB name inDuskTestCase::setUp()based on the process ID or environment variable. - Run tests on multiple CI jobs. In GitHub Actions, split your Dusk test files into groups and run each group as a separate job. Use a matrix strategy with environment variables like
TEST_GROUP=1,TEST_GROUP=2, etc. In each job, run only the tests in that group using--filteror by directory. - Optimize ChromeDriver for concurrency. If you run multiple Dusk processes on one machine, each needs its own ChromeDriver port. Set the port dynamically in
DuskTestCase::driver()using an environment variable, e.g.,getenv('CHROME_DRIVER_PORT') ?: 9515. - Handle file uploads and downloads. In headless mode, set download directory to a temp folder and ensure file permissions are writable. For uploads, use absolute paths.
Common Mistakes
- Using the same database for all parallel processes. This causes collisions and test failures. Always use separate databases or unique schemas per process.
- Not disabling GPU. Headless Chrome on CI often fails with GPU errors unless you pass
--disable-gpu. - Forgetting
--no-sandbox. In Docker or CI containers, Chrome needs this to run as root. - Running migrations inside each test. This slows down execution and can cause deadlocks. Migrate once per process.
- Hard-coding the ChromeDriver port. When running multiple processes, port conflicts occur. Use dynamic ports.
Tips
- Use
--verboseto see which test is running and debug parallel output. - Set
APP_URLinphpunit.xmltohttp://127.0.0.1:8000and runphp artisan servein CI before tests. - Use
Dusk::assertSee()instead ofassertSee()to ensure the browser has loaded the page. - Take screenshots on failure by calling
$this->captureFailuresFor()intearDown(). - Group tests by feature to run them in parallel without conflicts (e.g., user auth tests vs. admin tests).
Troubleshooting
- Chrome fails to start with "Connection refused". Ensure ChromeDriver is running and the port matches. Run
php artisan dusk:chrome-driverto update the driver version. - Tests pass locally but fail on CI. Check for missing system dependencies (e.g.,
libnss3,libx11). Install them via your CI's package manager. - Parallel tests cause database deadlocks. Use separate databases per process and run migrations once per process. Avoid using shared tables.
- Flaky timeouts. Increase the default wait time in
DuskTestCaseby settingprotected $timeout = 10;. Also usewaitForText()instead ofassertSee()for async content. - Headless Chrome cannot access localhost. Use
http://127.0.0.1:8000instead oflocalhostto avoid IPv6 issues.
Frequently Asked Questions
Can I run Dusk tests in parallel without multiple databases?
Yes, but you must use transactions or unique data per test. However, Dusk's browser process runs separately, so transactions won't roll back. It's safer to use separate databases.
How many parallel tests can I run?
It depends on your CI machine's CPU and memory. A good starting point is 2-4 processes per core. Monitor resource usage and adjust.
Do I need to start ChromeDriver manually for each parallel process?
Yes, if you run multiple processes on the same machine. Use a unique port per process. In CI, you can start multiple ChromeDriver instances with different ports.
Why do my tests fail with "Chrome failed to start: exited normally"?
This often means Chrome crashed. Check that you have all required system libraries and that you're passing --no-sandbox in root environments.
Can I use Laravel Dusk with other browsers like Firefox?
Yes, but this guide focuses on Chrome. For Firefox, you'd need geckodriver and adjust capabilities. Headless Chrome is the most common and stable choice.
How do I run only a subset of tests in parallel?
Use PHPUnit's --filter option or group tests with annotations. For example, php artisan dusk --filter=testLogin --parallel.