- How do I install Laravel Dusk in a Laravel project?
- Run `composer require laravel/dusk --dev` to install Dusk. Then configure your browser (e.g., Chrome) in `.env` with `DUSK_BROWSER=chrome`. Run `php artisan dusk:chrome-driver` to install Chromedriver automatically. No JDK or Selenium setup is required by default.
- Does Laravel Dusk work with Laravel 13 and PHP 8.5?
- Yes, Dusk officially supports Laravel 10–13 and PHP 8.1–8.5. Ensure your `composer.json` pins compatible versions (e.g., `laravel/framework:^13.0`). Check the [Laravel docs](https://laravel.com/docs/dusk) for version-specific notes.
- Can I use Firefox or Edge instead of Chrome with Dusk?
- Yes, Dusk supports other Selenium drivers. Install the driver (e.g., GeckoDriver for Firefox) and set `DUSK_DRIVER_URL` in `.env` to its path. Example: `DUSK_DRIVER_URL=/path/to/geckodriver`. Refer to the [Selenium docs](https://www.selenium.dev/documentation/) for setup.
- How do I write a basic Dusk test for a Laravel route?
- Generate a test file with `php artisan make:dusk ExampleTest`. Use `visit('/route')` to navigate, then assertions like `$this->assertSee('text')` or `$this->click('#button')`. Example: `public function test_login() { $this->visit('/login')->type('email', 'user@example.com')->press('Login')->assertPathIs('/dashboard'); }`
- Why are my Dusk tests failing intermittently in CI?
- Flaky tests often stem from slow asset loading or network latency. Use `waitFor()` to poll for elements (e.g., `$this->waitFor(5)->assertVisible('#element')`). Increase timeouts in `.env` with `DUSK_TIMEOUT=10`. For CI, use Dockerized Chromedriver or pre-built images like `chromium:latest` to ensure consistency.
- How can I test Vue.js or React SPAs with Laravel Dusk?
- Dusk works with SPAs but may need explicit waits for dynamic content. Use `$this->waitForJavaScript()` or `$this->waitFor(5)->assertSee('data')` to handle async updates. For Vue/React, ensure your tests trigger state changes (e.g., form submissions) and verify the final rendered state.
- Is Laravel Dusk slower than unit tests? How do I optimize performance?
- Yes, Dusk tests are slower due to browser automation. Optimize by running tests in parallel with `--parallel` (PHPUnit) or Pest’s `parallel()` directive. Use Docker for CI to spin up fresh instances per test. Avoid heavy assets (e.g., large images) in test environments.
- Can I integrate Dusk with GitHub Actions or GitLab CI?
- Yes, Dusk works seamlessly with CI. Example GitHub Actions step: `- name: Run Dusk tests run: php artisan dusk --parallel --env=testing`. Use `services` to run Chromedriver in Docker: `services: chromium: image: chromium:latest`. Pre-install dependencies with `composer install --no-interaction --prefer-dist`.
- What’s the difference between Dusk and Pest for Laravel testing?
- Pest is a testing framework (like PHPUnit) with a cleaner syntax, while Dusk is a browser automation tool. Use Pest for unit/feature tests and Dusk for end-to-end UI tests. Example: `composer require pestphp/pest --dev` to pair Pest with Dusk for modern test writing.
- How do I debug failed Dusk tests in production-like environments?
- Enable headless mode in `.env` with `DUSK_HEADLESS=false` to see the browser. Use `php artisan dusk --debug` to log detailed steps. For screenshots, add `$this->takeScreenshot('test-name')` in tests. Store logs in CI with `DUSK_LOG_LEVEL=debug` and integrate with tools like Slack or Sentry for alerts.