Using Laravel Dusk with Vagrant Homestead

If you are using Laravel Homestead and you want to use Laravel Dusk to test your application, you can find some problems.

For example, if you create a basic test

<?php

namespace Tests\Browser;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class ExampleTest extends DuskTestCase
{
    /**
     * A basic browser test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                ->assertPathIs('/login')
                ->assertSee('Forgot your password?');;
        });
    }
}

When you try to execute it, you can get an error

$ php artisan dusk
PHPUnit 5.7.13 by Sebastian Bergmann and contributors.

E 1 / 1 (100%)

Time: 1.23 seconds, Memory: 10.00MB

There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY"}}

Failed to connect to localhost port 9515: Connection refused

/home/vagrant/Code/my-test-project/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:287
/home/vagrant/Code/my-test-project/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:121
/home/vagrant/Code/my-test-project/tests/DuskTestCase.php:32
/home/vagrant/Code/my-test-project/vendor/laravel/dusk/src/TestCase.php:180
/home/vagrant/Code/my-test-project/vendor/laravel/framework/src/Illuminate/Support/helpers.php:639
/home/vagrant/Code/my-test-project/vendor/laravel/dusk/src/TestCase.php:181
/home/vagrant/Code/my-test-project/vendor/laravel/dusk/src/TestCase.php:111
/home/vagrant/Code/my-test-project/vendor/laravel/dusk/src/TestCase.php:85
/home/vagrant/Code/my-test-project/tests/Browser/ExampleTest.php:21

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

To resolve it, you have to install google-chrome in the Vagrant Homestead machine:

$ wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
$ sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
$ sudo apt-get update && sudo apt-get install -y google-chrome-stable

Then you have to install Xvfb

$ sudo apt-get install -y xvfb

Then you have to start the ChromeDriver. Your current directory must be the main directory of your project

$ ./vendor/laravel/dusk/bin/chromedriver-linux --port=8888

Starting ChromeDriver 2.25.426924 (649f9b868f6783ec9de71c123212b908bf3b232e) on port 8888
Only local connections are allowed.

Then add the project to the /etc/hosts in your Vagrant Homestead machine

127.0.0.1 your-project.app

Open another SSH connection to the Vagrant Homestead machine and execute

$ Xvfb :0 -screen 0 1280x960x24 &

to run the xvfb server.

Return to the first console and execute the tests

$ php artisan dusk
PHPUnit 5.7.13 by Sebastian Bergmann and contributors.

. 1 / 1 (100%)

Time: 4.79 seconds, Memory: 10.00MB

OK (1 test, 2 assertions)

Now you can see the tests in execution.

Info:

6 comments

  1. is 127.0.0.1 your-project.app including port? my application run on sandbox.dev:8080 (show home page)

  2. No, I include the “127.0.0.1 your-project.app” string in the “/etc/hosts” file in my Vagrant Homestead virtual machine without any port, because I use the default port on the nginx server.

  3. for some reason I cannot run ‘./vendor/laravel/dusk/bin/chromedriver-linux –port=8888’ i.e. permission denied

    so i need to run this chmod
    chmod 755 vendor/laravel/dusk/bin/*

    the rest are the same

    virtualbox 5.1.22
    vagrant 1.95
    laravel 5.4 (upgraded from 5.3)

  4. I have the same issue but my website is not running under a virtual machine elsewhere in my own.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.