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.
$ laravel new my-app
$ cd my-app
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
.
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.
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.
@aknackd Didn't work, runed test case failed 😞
I dumped it out, it said "ERR_CONNECTION_REFUSE".
Any idea?