chilio / laravel-dusk-ci

Docker Test suite for Laravel Dusk in gitlab CI
MIT License
160 stars 51 forks source link
browser-testing browser-tests chromedriver chromedriver-versions ci-test ci-tests docker docker-image dusk dusk-tests gitlab-ci laravel-dusk laravel-dusk-ci laravel-gitlab-ci laravel-test laravel-test-driven-development laravel-testing php phpunit vue

Browser Tests with Laravel Dusk in Gitlab CI/CD Pipelines.

Docker pulls GitHub issues GitHub stars GitHub license

This is a complete test suite for running tests (Unit, Feature, Browser) on your Laravel installation.

It uses Laravel Dusk for browser tests via a Docker executor runner in GitLab.

With this package, you don't need to worry, about Chrome or chromedriver compatibility. This is done automatically, to make your Browser Testing with Laravel Dusk, as easy, as possible.

Versions:

Deprecated versions:

Laravel dusk? Find more in Laravel Docs Laravel 10.x | Laravel 9.x | Laravel 8.x | Laravel 7.x | Laravel 6.x | Laravel 5.8

Gitlab? Find more on gitlab

Gitlab Continous Integration CI ? Find more on gitlab-runner

You may be wondering why deprecated versions are still available. The main reason is to allow for browser testing of your older Laravel applications while you're transitioning to newer versions.

This plug-and-play package is designed for your CI testing environment in GitLab.

All you need to do is select the appropriate version of your PHP interpreter (based on your Laravel version), and you should be ready to go.

Colours indicate the current state of PHP's life cycle and supported versions. Please note that all published images are fully functional and entirely usable for testing purposes.

Compatibility

LARAVEL VERSION COMPATIBLE PHP VERSIONS
10.x YES
9.x YES
8.x YES
7.x YES
6.x YES
5.8 YES
5.7 YES
5.6 YES
5.5 YES
5.4 Reported working
5.3 and below Not tested

With no modifications needed, you should be up and running in seconds. However, please carefully read the notes that follow.

For now DuskTestCase.php needs to be modified in all cases. THIS package is not going to work without -no-sandbox directive in this file.... Please read more about it, under Usage...

In case of any issues, please read the current documentation thoroughly, check existing issues (including closed ones) on GitHub, and finally, report a new issue on GitHub.

If this package proves to be useful to you, please star it on Docker Hub or GitHub to increase its visibility to other users.

Changelog

Documentation

What's included? (versions depend on image/tag)

Libs included
PHP
Xdebug
NGINX
Chromedriver
Chrome
NODEJS
NPM
YARN
BOWER
PHPUNIT
NODE-SASS
GULP

To see exact versions of installed packages, please run versions command in your script declaration in .gitlab-ci.yml, as in examples.

Available additional commands:

configure-laravel - sets up file permissions, generates laravel key, migrates and seeds db, checks and enforces chromedriver compatibility

start-nginx-ci-project - configures and starts nginx with php-fpm

versions - shows versions of included packages and enabled php modules.

Databases:

This build is tested with mysql, but also works with other docker db engines

To successfully run mysql add to your test routine:

services:

mysql:5.7 #or specify any downwards version of mysql. mysql:latest will not work out of the box, therefore please check changelog

And in your .env or your .env candidate file to use, mark mysql as the corresponding resource (DB_HOST=mysql)

If you receive errors like

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

probably you have other mysql instance running in your host system with that name. In this case you may need to provide db-alias for your test. More info here

Usage:

Make sure your DuskTestCase class in /tests/DuskTestCase.php matches all attributes, like drive options, host url, and port, like in this example DuskTestCase.php # there are some modifications which you need to apply (especially --no-sandbox in $options for driver)!!!, these changes should not affect your local dev environment, otherwise there might be something else wrong with your project.

In your .gitlab-ci.yml use this image like:

image: chilio/laravel-dusk-ci:latest# you can experiment with other images, which might address your needs.

add script lines:

- cp .env.example .env # remember to have APP_URL=http://localhost and DB_HOST=mysql

- configure-laravel # preparations to run project

- start-nginx-ci-project # Here we start webserver, so this is important before running dusk

Finally you can run all your tests served by nginx | php-fpm via:

- php artisan dusk

Note on using chromedriver versions:

Laravel Dusk ships with included chromedriver for linux, mac and windows. The examples here allow you to run dusk tests with these included chromedrivers (for linux in this case).

As of 2018-05-08 this package automatically fixes eventual problems with chrome or chromedriver. Therefore following changes to your app should not be necessary.

However, if you encounter problems, especially errors, with incorrect chromedriver version on your local machine, or this docker image, you can use, this package inbuilt own chromedriver. This option brings, a little bit more compatibility to your project, since Chrome is updated much more often, then chromedriver. In order to do that, you need to make only 2 script modifications:

  1. In DuskTestCase.php comment out starting chromedriver like// static::startChromeDriver();. In that case, to make your local development working, before issuing php artisan dusk command, you need to install proper chromedriver version manually, and make sure your chromedriver version matches Chrome, running on your local machine. You need to make sure chromedriver is started/running also. Due to different systems and configurations, that's beyond the scope of this documentation. Just to make clear here, in this case you are responsible for updating your own chromedriver with your current installation of Chrome. Since Chrome updates are pretty often, that's the suggested way to go, to keep your local dev running, while other packages are a little behind...
  2. In .gitlab-ci.yml add - chromedriver & before running - php artisan dusk. This will start system inbuilt chromedriver and not the one, that is shipped with laravel dusk, cause it might be outdated and causing problems with your local development.

Further example script commands (if needed in your case):

- cp phpunit.xml.ci phpunit.xml # In case ,you don't want to interfere, with your local test environment, you can apply this approach for ci tests. Make sure, to create phpunit.xml.ci, in your project and phpunit.dusk.xml. You can specify, 2 separate files for your tests, one for phpunit and one for dusk, you can use examples, from this repo or modify them to suite your needs...

- composer install --prefer-dist --no-ansi --no-interaction --no-progress --no-scripts

- bower install --quiet

- npm run dev or if you are on yarn registry - yarn run dev

- ./vendor/phpunit/phpunit/phpunit -v --coverage-text --colors --stderr# to run phpunit with version specified in your project

Examples:

DuskTestCase.php # with all modifications to successfully run php artisan dusk tests

phpunit.dusk.xml # If exists in project root, this file will be automatically injected when dusk is run, in case, you want to define any custom variables for your tests like DBs, Caches etc.

phpunit.xml.ci # in case, you want to customize phpunit tests in CI , remember to copy this to phpunit.xml by adding - cp phpunit.xml.ci phpunit.xml to your gitlab-ci.yml.

gitlab-ci.yml # with stages, cache, and artifacts, assuming you are using scripts like "dev" in package.json.

Troubleshooting:

Disclaimer

Images mentioned above are intended for testing purposes in CI environments only. Production usage is not recommended.