User Agents

Web browsers and everyday web tech, explained

Scale Laravel Dusk Browser Tests with Headless Chrome

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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) and php artisan dusk:install run.
  • Chrome or Chromium installed (or use chromedriver via php 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

  1. Install and configure Dusk. Ensure DuskServiceProvider is registered only in local and testing environments. In app/Providers/DuskServiceProvider.php, wrap registration with if ($this->app->environment('local', 'testing')).
  2. Set up a dedicated testing database. In phpunit.xml (or DuskTestCase.php), set environment variables like DB_CONNECTION=mysql, DB_DATABASE=dusk_test, DB_USERNAME=root, etc. Use a separate database to avoid wiping development data.
  3. 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.
  4. 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']])
        )
    );
  5. Start ChromeDriver manually or via CI. In CI, run php artisan dusk:chrome-driver to download the correct driver, then start it with chromedriver --port=9515 in the background. Locally, you can use php artisan dusk which auto-starts the driver.
  6. Run tests in parallel. Use PHPUnit's parallel testing feature (PHPUnit 9.3+). In phpunit.xml, set processIsolation="true" and use --parallel with --testsuite when running. Example command:
    php artisan dusk --parallel --testsuite=Dusk
    Or use vendor/bin/paratest for more control. Ensure each process uses a unique database or use transactions.
  7. Isolate test data. Use DatabaseTransactions trait in Dusk tests, but note that Dusk runs in a separate browser process, so transactions won't roll back automatically. Instead, use RefreshDatabase with a per-test unique database name (e.g., dusk_test_1, dusk_test_2) by setting the DB name in DuskTestCase::setUp() based on the process ID or environment variable.
  8. 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 --filter or by directory.
  9. 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.
  10. 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 --verbose to see which test is running and debug parallel output.
  • Set APP_URL in phpunit.xml to http://127.0.0.1:8000 and run php artisan serve in CI before tests.
  • Use Dusk::assertSee() instead of assertSee() to ensure the browser has loaded the page.
  • Take screenshots on failure by calling $this->captureFailuresFor() in tearDown().
  • 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-driver to 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 DuskTestCase by setting protected $timeout = 10;. Also use waitForText() instead of assertSee() for async content.
  • Headless Chrome cannot access localhost. Use http://127.0.0.1:8000 instead of localhost to 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.