laravel / dusk

Laravel Dusk provides simple end-to-end testing and browser automation.
https://laravel.com/docs/dusk
MIT License
1.88k stars 322 forks source link

Cannot run test on Homestead/Docker #71

Closed mnabialek closed 7 years ago

mnabialek commented 7 years ago

There are 2 problems with this (I personally test it in Docker):

1) Tests won't run without running extra commands at all (missing libs etc. so before running tests you need to run those magic commands) or you get :

Failed to connect to localhost port 9515: Connection refused

2) Even if you run multiple commands it still might not work. What I'm finally getting is:

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

Operation timed out after 30000 milliseconds with 0 bytes received

/usr/share/nginx/html/vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:287 /usr/share/nginx/html/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:121 /usr/share/nginx/html/tests/DuskTestCase.php:32 /usr/share/nginx/html/vendor/laravel/dusk/src/TestCase.php:180 /usr/share/nginx/html/vendor/laravel/framework/src/Illuminate/Support/helpers.php:639 /usr/share/nginx/html/vendor/laravel/dusk/src/TestCase.php:181 /usr/share/nginx/html/vendor/laravel/dusk/src/TestCase.php:111 /usr/share/nginx/html/vendor/laravel/dusk/src/TestCase.php:85 /usr/share/nginx/html/tests/Browser/ExampleTest.php:20

The commands I've run to get to error in step 2 were:

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

Just to update, I've run Dusk tests for the same project without any problem on host machine (Windows). But when running tests on Windows, browser windows opens, so maybe that's the problem why tests on VM are failing?

ockle commented 7 years ago

@mnabialek I was running into similar problems and after playing around with it for ages, I got it to work, although it requires a change to SupportsChrome.php. Change:

->setPrefix(realpath(__DIR__.'/../bin/chromedriver-'.static::driverSuffix()))

to:

->setPrefix('xvfb-run')
->setArguments([realpath(__DIR__.'/../bin/chromedriver-'.static::driverSuffix())])

To anyone else reading this, note the prerequisite parts:

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg –i google-chrome-stable_current_amd64.deb
apt-get install -y xvfb
mattkoch614 commented 7 years ago

@ockle This was the only thing that worked for me, and I've been trying to get Dusk running all morning in Laravel Homestead. I should mention that this was in addition to following install instructions here:

https://christopher.su/2015/selenium-chromedriver-ubuntu/

and then finally running:

chmod 755 vendor/laravel/dusk/bin
harryatoms commented 7 years ago

Hi everyone, I'm having the same issue in a Docker container based off of php:7-apache which is debian jessie.

I tried installing chrome/xvfb and modifying the SupportsChrome trait in vendor/laravel/dusk/src without success.

I'm still stuck on Operation timed out after 30001 milliseconds with 0 bytes received.

Pretty frustrating lol, many people run tests as a part of CI inside a built docker image and this is a popular image to extend from.

There has been a lot of reports of this problem on here and laracasts but still no solid fix or documentation updates for Dusk.

Thanks!

SebastianS90 commented 7 years ago

@harryatoms I had some trouble because GitLab CI doesn't assign a tty to the docker container. But that could be solved by starting dusk using script -q -e -c 'php artisan dusk'

And for the xvfb part I just use my own DuskCommand class which overrrides the original one as follows:

    protected $xDisplay = 17;
    protected $xDisplayResolution = '1280x720x24';

    protected function withDuskEnvironment($callback)
    {
        return parent::withDuskEnvironment(function () use ($callback) {
            return $this->withChromeDriver($callback);
        });
    }

    protected function withChromeDriver($callback)
    {
        // Start a headless X server
        $xvfb = (new ProcessBuilder())
            ->setTimeout(null)
            // ->add('exec') remove this line for current Symfony\Component\Process version 
            ->add('/usr/bin/Xvfb')
            ->add(':' . $this->xDisplay)
            ->add('-screen')->add('0')->add($this->xDisplayResolution)
            ->getProcess();
        $xvfb->start();

        // Start the chromedriver
        $chrome = (new ProcessBuilder())
            ->setTimeout(null)
            // ->add('exec') remove this line for current Symfony\Component\Process version 
            ->add(base_path('vendor/laravel/dusk/bin/chromedriver-linux'))
            ->getProcess()
            ->setEnv(['DISPLAY' => ':' . $this->xDisplay]);
        $chrome->start();

        // Terminate both processes once we are done
        return tap($callback(), function () use ($chrome, $xvfb) {
            $chrome->stop();
            $xvfb->stop();
        });
    }

and then comment out the contents of the prepare function in DuskTestCase

Edit [2017-06-05]: as noted below the code stopped working after a symfony update. Remove the two ->add('exec') lines and it should work again.

harryatoms commented 7 years ago

@SebastianS90 very handy, thanks! I'll give this a try. I'm unable to run dusk tests even after starting Xvfb, so I'm still trying to get that to work.

For now, I switched to phantomjs and it's working, but that was more for a POC. I'll still be trying to use chrome through the methods mentioned above until I can put together a good configuration.

mydnic commented 7 years ago

@ockle solution worked for me. I'm using Homestead on a windows machine. This fix should be implemented asap in the package.

vesper8 commented 7 years ago

Lots of solutions floating around but this here is the official solution that will be included in the next vagrant homestead box. I've tried it and it works good

https://github.com/laravel/homestead/pull/528/files#diff-6c17dd8d21b8b745850a87b9d0de77c7

basically run

sudo apt-get update
sudo apt-get -y install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4
sudo apt-get -y install chromium-browser
sudo apt-get -y install xvfb gtk2-engines-pixbuf
sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
sudo apt-get -y install imagemagick x11-apps

and then update your aliases with the latest ones which includes this new function

function dusk() {
     pids=$(pidof /usr/bin/Xvfb)

     if [ ! -n "$pids" ]; then
         Xvfb :0 -screen 0 1280x960x24 &
     fi

     php artisan dusk "$@"
 }

You can also alternatively use this release canditate homestead box which has the fix in it https://atlas.hashicorp.com/Svpernova09/boxes/nothomestead

alejandrorosas commented 7 years ago

@SebastianS90 it seems that your code doesn't works with the latest symfony process :(

stephangroen commented 7 years ago

On Ubuntu Linux 16.04, I got this to work:

Install Chromium & dependencies for headless testing sudo apt-get -y install chromium-browser xvfb gtk2-engines-pixbuf xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable imagemagick x11-apps

Create a customDuskCommand Which extends the original, with this handle method:

    public function handle()
    {
        $xvfb = (new ProcessBuilder())
            ->setTimeout(null)
            ->setPrefix('/usr/bin/Xvfb')
            ->setArguments(['-ac',  ':0', '-screen', '0', '1280x1024x16'])
            ->getProcess();

        $xvfb->start();

        try {
            parent::handle();
        } finally {
            $xvfb->stop();
        }

        return;
    }
SebastianS90 commented 7 years ago

@alejandrorosas Yes, there has been some change to symfony process. Remove the two lines ->add('exec') and it should work again.

JackWH commented 7 years ago

@vesper8 Thanks for your comment about running dusk directly within Homestead. This gets Dusk to work headlessly for me, but, the error screenshots being saved are 800x600px in size, despite having set the Xvfb arguments to 1280x960x24.

Is this expected behaviour, or am I doing something wrong? Thanks in advance.

JackWH commented 7 years ago

@vesper8 Sorry, ignore that last message - figured it out - just needed to re-provision Homestead again for changes to the aliases file to take effect. Thanks!