|
| 1 | +# Laravel Dusk Parallel |
| 2 | + |
| 3 | +Run your Laravel Dusk browser tests in parallel using [ParaTest](https://github.com/paratestphp/paratest). |
| 4 | + |
| 5 | +> **⚠️ This package is currently in beta.**, It may contain bugs and the API is subject to change. |
| 6 | +
|
| 7 | +## Requirements |
| 8 | + |
| 9 | +- PHP 8.1+ |
| 10 | +- Laravel 10+ |
| 11 | +- Laravel Dusk 7+ |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require --dev jackbayliss/laravel-dusk-parallel |
| 17 | +``` |
| 18 | + |
| 19 | +## Setup |
| 20 | + |
| 21 | +### 1. Install ParaTest |
| 22 | + |
| 23 | +```bash |
| 24 | +composer require --dev brianium/paratest |
| 25 | +``` |
| 26 | + |
| 27 | +### 2. Start ChromeDriver instances |
| 28 | + |
| 29 | +You need one ChromeDriver instance per parallel process. By default the package uses ports starting from `9515`: |
| 30 | + |
| 31 | +```bash |
| 32 | +chromedriver --port=9515 & |
| 33 | +chromedriver --port=9516 & |
| 34 | +``` |
| 35 | + |
| 36 | +### 3. Run your tests |
| 37 | + |
| 38 | +```bash |
| 39 | +php artisan dusk:parallel |
| 40 | +``` |
| 41 | + |
| 42 | +By default, this runs with 2 parallel processes. You can change this: |
| 43 | + |
| 44 | +```bash |
| 45 | +php artisan dusk:parallel --processes=4 |
| 46 | +``` |
| 47 | + |
| 48 | +## Configuration |
| 49 | + |
| 50 | +### Changing the base port |
| 51 | + |
| 52 | +If port `9515` is already in use, set `DUSK_DRIVER_BASE_PORT` in your `.env`: |
| 53 | + |
| 54 | +```env |
| 55 | +DUSK_DRIVER_BASE_PORT=9600 |
| 56 | +``` |
| 57 | + |
| 58 | +Worker processes will then use ports `9600`, `9601`, etc. Remember to start ChromeDriver on those ports instead. |
| 59 | + |
| 60 | +### Using a custom driver URL |
| 61 | + |
| 62 | +If you're using a remote WebDriver such as Selenium Grid or BrowserStack, set `DUSK_DRIVER_URL` in your `.env` and the port logic will be bypassed entirely: |
| 63 | + |
| 64 | +```env |
| 65 | +DUSK_DRIVER_URL=http://selenium-grid:4444 |
| 66 | +``` |
| 67 | + |
| 68 | +### Customising Chrome options |
| 69 | + |
| 70 | +If you need to customise Chrome options, extend the package's `TestCase` in your `tests/DuskTestCase.php`: |
| 71 | + |
| 72 | +```php |
| 73 | +use JackBayliss\DuskParallel\TestCase as ParallelTestCase; |
| 74 | + |
| 75 | +abstract class DuskTestCase extends ParallelTestCase |
| 76 | +{ |
| 77 | + protected function driver(): RemoteWebDriver |
| 78 | + { |
| 79 | + $options = (new ChromeOptions)->addArguments([ |
| 80 | + '--headless=new', |
| 81 | + '--no-sandbox', |
| 82 | + '--disable-dev-shm-usage', |
| 83 | + ]); |
| 84 | + |
| 85 | + return RemoteWebDriver::create( |
| 86 | + ParallelDriver::resolveDriverUrl(), |
| 87 | + DesiredCapabilities::chrome()->setCapability( |
| 88 | + ChromeOptions::CAPABILITY, |
| 89 | + $options |
| 90 | + ) |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +> **Note:** Extending the packages `TestCase` is entirely optional. The package works with a standard Laravel Dusk setup out of the box. |
| 97 | +
|
| 98 | +## CI Usage |
| 99 | + |
| 100 | +### GitHub Actions |
| 101 | + |
| 102 | +```yaml |
| 103 | +- name: Start ChromeDriver |
| 104 | + run: | |
| 105 | + chromedriver --port=9515 & |
| 106 | + chromedriver --port=9516 & |
| 107 | + sleep 2 |
| 108 | +
|
| 109 | +- name: Run Dusk tests |
| 110 | + run: php artisan dusk:parallel --processes=2 |
| 111 | +``` |
| 112 | +
|
| 113 | +## How it works |
| 114 | +
|
| 115 | +ParaTest splits your test suite across multiple worker processes. Each worker receives a `TEST_TOKEN` environment variable (`0`, `1`, `2`...) which the package uses to route that worker to its own ChromeDriver instance on a unique port. This means each worker gets a completely independent browser session with no shared state between processes. |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +MIT |
0 commit comments