Running Laravel Dusk tests with Docker using DDEV

Part of my “From Zero to Hero” course covers testing with Laravel Dusk. Homestead makes using Dusk really easy since there’s a configuration option. However, I’m now using Docker for my local development and I’m using DDEV to help me with this.

If you’re also running DDEV or you’re thinking about using it and need to run Laravel Dusk tests, then this post is just for you.


Creating our docker-compose file

Firstly, we need to create a docker-compose file in the root of our .ddev directory. We’ll call it docker-compose.selenium.yaml.

version: '3.6'services:  selenium-hub:    image: selenium/hub:3.141.59-20200326    container_name: ddev-${DDEV_SITENAME}-selenium-hub    ports:      - "4444:4444"  chrome:    image: selenium/node-chrome:3.141.59-20200326    container_name: ddev-${DDEV_SITENAME}-chrome    volumes:      - /ddev/shm:/ddev/shm    depends_on:      - selenium-hub    environment:      - HUB_HOST=selenium-hub      - HUB_PORT=4444

Setting up Laravel’s environment

When we run ddev start, it will download and create our containers. Selenium is our testing driver and chrome is our headless browser. Next, we need to setup our environment and Dusk configuration. In your .env file, update APP_URL to http://web which is the name of our Docker web container.

Configuring Laravel Dusk

Next up is our Dusk configuration, head over to /tests/DuskTestCase.php and on lines 38 to 42, remove and replace with the following…

        return RemoteWebDriver::create(            'http://selenium-hub:4444/wd/hub', DesiredCapabilities::chrome()->setCapability(                ChromeOptions::CAPABILITY, $options            )        );

Here we’re telling Laravel Dusk to run on the url http://selenium-hub (the name of our container) at port 4444 (the default Selenium port). Because we will be using Chrome as our headless browser, we need to make sure our DesiredCapabilities is set to chrome() with ChromeOptions.

Run ddev ssh and then php artisan dusk. You should now see Dusk running your test suite.

Optional: DDEV Dusk command

I added a custom command to DDEV which makes running my tests slightly easier. If you would like to do the same, then head on over to /.ddev/commands/web and create a file called dusk. Go ahead and write the following…

#!/bin/bashphp artisan dusk

You can now run ddev dusk from your terminal and it will automatically ssh into your web container and run php artisan dusk.

Closing thoughts

This took me a lot of work to figure out, but I’m glad I got there in the end. If you enjoyed this post, please feel free to subscribe to my newsletter and if you have any issues, please be sure to comment below or @ me on Twitter

Leave a Reply