- Can I use phpunit/phpunit-selenium for Laravel end-to-end testing alongside Laravel Dusk?
- Yes, but they serve different purposes. Dusk is Laravel-native and uses ChromeDriver directly, while this package uses Selenium 2’s older API. For new projects, consider Dusk or modern alternatives like Playwright, but this package works if you need Selenium 2 compatibility. Avoid mixing them unless you have a specific reason.
- What’s the best way to install and configure phpunit/phpunit-selenium in a Laravel project?
- Run `composer require --dev phpunit/phpunit-selenium`, then extend `Selenium2TestCase` in your test class. Configure Selenium Server separately (e.g., via Docker or a cloud provider). Ensure your `phpunit.xml` includes the test suite. Avoid installing WebDriver binaries globally; use Docker or Laravel Sail for isolation.
- Does phpunit/phpunit-selenium work with Laravel’s latest PHPUnit setup (PHP 8.1+)?
- No, this package only supports PHP 7.3+ via PHPUnit 9.x/8.x. For PHP 8.1+, consider migrating to Selenium 4 or a Laravel-native alternative like Laravel Dusk or Playwright. The package explicitly drops support for newer PHP versions, so check compatibility before upgrading.
- How do I handle flaky Selenium tests in CI/CD for Laravel?
- Use explicit waits, headless browsers (Chrome/Firefox), and parallel execution to reduce flakiness. Configure retries in your CI pipeline (e.g., GitHub Actions) and isolate Selenium tests in a separate stage. Avoid blocking deployments; treat them as a secondary validation layer. Dockerized Selenium Server helps stabilize environments.
- Is phpunit/phpunit-selenium maintained, or should I look for alternatives like Playwright?
- This package is outdated (Selenium 2 is end-of-life) and lacks active maintenance. For new Laravel projects, prioritize modern tools like Playwright or Cypress, which offer better performance, reliability, and Laravel integration. Use this only if you’re locked into Selenium 2 for legacy reasons.
- Can I run phpunit/phpunit-selenium tests in parallel for faster CI execution?
- Yes, but you’ll need to manage Selenium Server instances manually or use a cloud provider like BrowserStack. Parallelize tests at the class or method level in PHPUnit, but ensure each test suite gets its own WebDriver session. Docker Compose or Kubernetes can help scale Selenium Server instances dynamically.
- How do I mock APIs or external services in Selenium tests for Laravel?
- Use Laravel’s built-in HTTP testing helpers (e.g., `actingAs()`, `json()`) alongside Selenium for hybrid tests. For external APIs, mock responses with Laravel’s `Http::fake()` or a service like WireMock. Avoid hardcoding test data; use Laravel’s factories or seeders to generate consistent test environments.
- What’s the performance impact of running Selenium tests in Laravel CI compared to unit tests?
- Selenium tests are significantly slower (minutes vs. seconds) due to browser spin-up and network overhead. Run them in a separate CI stage or pipeline, or limit them to critical user flows. Use headless browsers and parallel execution to mitigate delays. Avoid running them on every commit.
- How do I set up Selenium Server for local Laravel development with phpunit/phpunit-selenium?
- Use Docker for simplicity: Run `docker run -d -p 4444:4444 selenium/standalone-chrome`. Configure your test class to point to `http://localhost:4444/wd/hub`. For Laravel Sail, add Selenium to your `docker-compose.yml`. Avoid manual downloads of WebDriver binaries; containerization ensures consistency across environments.
- Are there Laravel-specific best practices for organizing Selenium tests in the test suite?
- Group Selenium tests in `tests/Feature/Selenium/` and extend a base `SeleniumTestCase` for shared setup (e.g., browser config, base URLs). Use Laravel’s test helpers (e.g., `refreshDatabase()`) alongside Selenium assertions. Avoid mixing UI and unit tests; treat Selenium as a separate validation layer for critical flows.