Laravel Dusk - can't make it run on GitLab CI

Viewed 923

Did anyone get to make Laravel Dusk to work on GitLab CI?

I'm using this docker image, which is amazing, but I simply can't execute ./vendor/laravel/dusk/bin/chromedriver-linux, it says:

/bin/bash: line 102: ./vendor/laravel/dusk/bin/chromedriver-linux: No such file or directory

When I enter at ./vendor/laravel/dusk/bin I can see that chromedriver-linux is there and it's executable, but can't make it run.

By now, the script lines that compose this phase are:

- mv .env.testing .env
- php artisan key:generate
- ./vendor/bin/phpunit --colors=never
- php artisan dusk:install
- chmod -R 0755 vendor/laravel/dusk/bin
- ./vendor/laravel/dusk/bin/chromedriver-linux
- php artisan dusk

I've also tried the approaches on documentation, even though they're not for GitLab CI, but none of them worked.

Update:

Running ls -l ./vendor/laravel/dusk/bin returns:

total 33560
-rwxr-xr-x    1 root     root      10885776 Jun 26 13:10 chromedriver-linux
-rwxr-xr-x    1 root     root      14994520 Jun 26 13:10 chromedriver-mac
-rwxr-xr-x    1 root     root       8393728 Jun 26 13:10 chromedriver-win.exe
-rwxr-xr-x    1 root     root         69309 Jun 25 22:59 jquery.js

I've tried to copy the file to the machine's current PATH but didn't work either:

Testing the $PATH variable

$ echo $PATH
./vendor/bin:/composer/vendor/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Copying the binary to /usr/local/bin

$ CHROME_DRIVER_PATH_BIN=./vendor/laravel/dusk/bin/chromedriver-linux
$ cp $CHROME_DRIVER_PATH_BIN /usr/local/bin

Testing if the file is really there

$ ls -lha /usr/local/bin
total 56564
drwxr-xr-x    1 root     root        4.0K Jun 26 17:33 .
drwxr-xr-x    1 root     root        4.0K Feb 21 23:08 ..
-rwxr-xr-x    1 root     root       10.4M Jun 26 17:33 chromedriver-linux
-rwxr-xr-x    1 root     root        1.8M Feb 22 06:40 composer
-rwxrwxr-x    1 root     root         118 Feb 21 23:02 docker-php-entrypoint
-rwxrwxr-x    1 root     root        1.4K Feb 21 23:02 docker-php-ext-configure
-rwxrwxr-x    1 root     root        2.4K Feb 21 23:02 docker-php-ext-enable
-rwxrwxr-x    1 root     root        2.3K Feb 21 23:02 docker-php-ext-install
-rwxrwxr-x    1 root     root         587 Feb  9 01:57 docker-php-source
lrwxrwxrwx    1 root     root          32 Jun 26 17:33 heroku -> /usr/local/lib/heroku/bin/heroku
-rwxr-xr-x    1 root     root         817 Feb 21 23:08 pear
-rwxr-xr-x    1 root     root         838 Feb 21 23:08 peardev
-rwxr-xr-x    1 root     root         751 Feb 21 23:08 pecl
lrwxrwxrwx    1 root     root           9 Feb 21 23:08 phar -> phar.phar
-rwxr-xr-x    1 root     root       14.5K Feb 21 23:08 phar.phar
-rwxr-xr-x    1 root     root       14.3M Feb 21 23:08 php
-rwxr-xr-x    1 root     root       14.2M Feb 21 23:08 php-cgi
-rwxr-xr-x    1 root     root        2.6K Feb 21 23:08 php-config
-rwxr-xr-x    1 root     root       14.4M Feb 21 23:08 phpdbg
-rwxr-xr-x    1 root     root        4.5K Feb 21 23:08 phpize

Either way, running chromedriver-linux does not work, I got the error nohup: can't execute 'chromedriver-linux': No such file or directory

I've tested the same concept on my macOS: created an executable, put it into a folder which is on my PATH variable using a symlink and it works perfectly.

2 Answers

I could solve that installing chromium and chromium-chromedriver packages since they are available on Alpine Linux.

Below is the relevant part of my .gitlab-ci.yml file:

image: lorisleiva/laravel-docker

before_script:
  - apk add chromium-chromedriver -y
  - apk add chromium -y

phptest:
  stage: test
  script:
    - mv .env.testing .env
    - php artisan key:generate
    - ./vendor/bin/phpunit --colors=never
    - chromedriver 2>&1 &
    - sleep 5
    - nohup php artisan serve &
    - php artisan dusk

Also, a really important part is to add --no-sandbox argument when creating the RemoteWebDriver. This can be done changing the function driver on tests/DuskTestCase.php file:

<?php
    // tests/DuskTestCase.php
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--no-sandbox', // # Add this # //
            '--window-size=1920,1080',
        ]);

        return RemoteWebDriver::create(
            'http://localhost:9515',
            DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY,
                $options
            )
        );
    }

Just this solved my problem and now I can test with Dusk on GitLab CI using lorisleiva/laravel-docker Docker image.

The Dockerfile for that lorisleiva/laravel-docker image does:

ENV PATH ./vendor/bin:/composer/vendor/bin:$PATH
...
WORKDIR /var/www

So when your script does execute, the '.' in './vendor' might not reference the current working directory you expect.

Since PATH is set, it would be best if chromedriver-linux was in ./vendor/bin instead.
That way, your script can simply call chromedriver-linux (no leading path)

Related