Skip to content

Instantly share code, notes, and snippets.

@aknackd
Last active May 18, 2022 09:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aknackd/e8383a8389eb96c8a425d2a0dc738ff2 to your computer and use it in GitHub Desktop.
Save aknackd/e8383a8389eb96c8a425d2a0dc738ff2 to your computer and use it in GitHub Desktop.
Laravel Dusk for Selenium using Docker

Configuring Laravel Dusk for Selenium using Docker

Start Docker containers

First, we need to run a Selenium Hub and a Firefox Selenium node:

$ docker run -d --name hub -p 4444:4444 selenium/hub
$ docker run -d --name fx --link hub:hub selenium/node-firefox

The Selenium Hub receives the request from Laravel Dusk and passes the request to the Firefox container.

NOTE: We're using Firefox instead of Chrome because, at least in my testing, Dusk will generate a proper screenshot of the app when a test fails, whereas Chrome kept generating an all white image. This may not be the case for you, to test using Chrome instead of Firefox use the selenium/node-chrome image instead of selenium/node-firefox when executing the second docker run command.

Create a new Laravel application

$ laravel new my-app
$ cd my-app

Install and configure Laravel Dusk

Follow the official documentation - stop when you reach the Using Other Browsers section.

Modify tests/Browser/DuskTestCase.php as follows:

Comment the contents of the prepare() method to disable the Chrome driver:

public static function prepare()
{
    // static::startChromeDriver();
}

Modify the driver() method to point to the Selenium hub:

protected function driver()
{
    return RemoteWebDriver::create(
        'http://localhost:4444/wd/hub', DesiredCapabilities::firefox()
    );
}

NOTE: If you're using Docker Machine, use the IP address of the Docker machine (usually 192.168.99.100 by default) instead of localhost.

Start NGINX and PHP-FPM containers

To test, we need a web server so we'll setup to two more Docker containers: one for PHP-FPM that will execute the Laravel app and the other that provides NGINX for web access.

First we need an NGINX configuration for serving the site in the web container:

nginx.conf:

server {
    listen 80;

    index index.php index.html;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Now lets start our containers:

$ docker run -d --name php -v ./:/var/www/html php:7.1-fpm-alpine
$ docker run -d --name web --links php:php -p 8080:80 -v ./:/var/www/html -v ./nginx.conf:/etc/nginx/conf.d/default.conf nginx:alpine

To test we can simply go to http://localhost:8080 and see our Laravel application.

Running Dusk

The final step we have to perform before we can finally test is to set APP_URL in .env to the Laravel app hosted in the web container that was started in the previous section:

APP_URL=http://localhost:8080

NOTE: Again, if you're using Docker Machine substitute localhost with the machine's IP address.

Finally we can run our browser tests:

$ php artisan dusk

Test output will be sent to the console and browser screenshots will be stored in tests/Browser/screenshots in the event a test fails.

NOTE: Alternatively you can set APP_URL as an environment variable when executing php artisan dusk if you don't want to touch the filesystem or if you're setting up a CI build process.

@ngohuytrieu
Copy link

ngohuytrieu commented Apr 24, 2022

@aknackd Didn't work, runed test case failed 😞
I dumped it out, it said "ERR_CONNECTION_REFUSE".

Any idea?

@greatmaxix
Copy link

@ngohuytrieu I think selenium allows only localhost connections by default. You should probably pass CHROMEDRIVER_WHITELISTED_IPS='' to the selenium container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment