Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dusk under Laravel Sail fails when there is a .env.dusk.local file #851

Closed
otepas opened this issue Dec 31, 2020 · 6 comments
Closed

Dusk under Laravel Sail fails when there is a .env.dusk.local file #851

otepas opened this issue Dec 31, 2020 · 6 comments

Comments

@otepas
Copy link

otepas commented Dec 31, 2020

Environment

  • Dusk Version: 6.11.0
  • Laravel Version: 8.20.1
  • PHP Version: 8.0.0 / 7.4.13 (same result with both)
  • Database Driver & Version: not used

Development platform:

  • Windows 10 with WSL2 (Ubuntu 20.04)
  • Laravel sail: 0.0.5 / 1.0.1 (same result with both)

Description:

Laravel dusk fails when there is a .env.dusk.local file.
The contents of the .env.dusk.local file do not appear to affect the result. Copying a working .env file to .env.dusk.local is enough to make Dusk report a problem.
This behaviour was detected on an existing application, but it can be reproduced with a fresh Laravel install.

Steps To Reproduce:

Fresh Laravel install with Sail:
composer create-project laravel/laravel test
composer install laravel/dusk --dev
edit docker-compose.yml to enable selenium by uncommenting the following lines:

    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
            - '/dev/shm:/dev/shm'
        networks:
            - sail

And the dependency of laravel.test on selenium:

        depends_on:
            - mysql
            - redis
            - selenium

sail up -d
sail artisan dusk:install
sail dusk

Default test runs OK

cp .env .env.dusk.local
sail dusk

Default test results in Error:

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: net::ERR_CONNECTION_REFUSED
  (Session info: headless chrome=87.0.4280.66)

/var/www/html/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:139
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php:371
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php:612
/var/www/html/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php:27
/var/www/html/vendor/php-webdriver/webdriver/lib/WebDriverNavigation.php:41
/var/www/html/vendor/laravel/dusk/src/Browser.php:153
/var/www/html/tests/Browser/ExampleTest.php:19
/var/www/html/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:68
/var/www/html/tests/Browser/ExampleTest.php:21
@driesvints
Copy link
Member

I got the same issue. The problem, I suspect, is that if you try to run the dusk tests within the Docker container with your APP_URL set to http://localhost (which is the default for Laravel), it doesn't have that context of a localhost within a Docker container. You'll need to target the url set by Sail itself which is http://laravel.test. So change APP_URL to APP_URL=http://laravel.test in your .env.dusk.local file and that should work (it did for me).

@otepas
Copy link
Author

otepas commented Jan 5, 2021

Thanks! I just tried that in my app and it does work.

I don't quite understand why though... When I have APP_URL to http://localhost in .env and there is no .env.dusk.local the tests run correctly. I would have expected that in this situation dusk would use http://localhost and fail the same way it does when there is a .env.dusk.local file.

Anyways, problem solved, so thanks for your help.

@matthewlilly
Copy link

matthewlilly commented Jan 26, 2021

Leaving this here, just incase anyone has a similar issue that isn't resolved by using http://laravel.test for APP_URL

I had more luck when updating the docker-compose.yml and phpunit.dusk.xml files within the root folder.

Update docker-compose.yml to use two databases, allowing the tests to be separate;

mysql:
      image: 'mysql:8.0'
      ports:
          - '${FORWARD_DB_PORT:-3306}:3306'
      environment:
          MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
          MYSQL_DATABASE: '${DB_DATABASE}'
          MYSQL_USER: '${DB_USERNAME}'
          MYSQL_PASSWORD: '${DB_PASSWORD}'
          MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      volumes:
          - 'sailmysql:/var/lib/mysql'
      networks:
          - sail
         
mysql.test:
      image: 'mysql:8.0'
      environment:
          MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
          MYSQL_DATABASE: '${DB_DATABASE_TEST}'
          MYSQL_USER: '${DB_USERNAME}'
          MYSQL_PASSWORD: '${DB_PASSWORD}'
          MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
      tmpfs: '/var/lib/mysql'
      networks:
          - sail

Then update phpunit.dusk.xml to use the test database;

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Browser Test Suite">
            <directory suffix="Test.php">./tests/Browser</directory>
        </testsuite>
    </testsuites>
    <php>
        <server name="DB_HOST" value="mysql.test"/>
        <server name="DB_DATABASE" value="laravel_test"/>
    </php>
</phpunit>

There is no need for the .env.dusk.local file this way.

@bjuppa
Copy link

bjuppa commented Oct 13, 2021

I just found that removing APP_URL entirely from the .env.dusk.local file seems to make it use the "internal" Sail url. That's easier than setting it to the specific url used by Sail!

@driesvints Would it be useful to update the docs with instruction to leave out APP_URL from .env.dusk.local if using Sail?

I guess that would be here: https://laravel.com/docs/8.x/sail#laravel-dusk
...and not here https://laravel.com/docs/8.x/dusk#environment-handling

@7emansell
Copy link

@matthewlilly did you still enable selenium and the laravel.test selenium dependency?

@matthewlilly
Copy link

@matthewlilly did you still enable selenium and the laravel.test selenium dependency?

Yes, I used selenium in testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants