Summer Sale! All accounts are 50% off this week.

acacha's avatar

How to disable headless mode in Laravel 5.5 (Dusk 2.0)?

Now Laravel 5.5 uses Dusk 2.0 with headless mode active by default. Anybody know how to change this to execute Laravel Dusk tests like before (seeing the browser opening and executing tests)?

0 likes
8 replies
acacha's avatar

I overwrite driver method with:

/**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu'
        ]);

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

But I only want to disable headless for debugging methods so I wonder if there is any other option like:

php artisan dusk --no-headless or something similar.

ahashim's avatar

Any luck with this? I too am looking for this very thing.

browner12's avatar

I'm not sure how we would do it as a command argument, but it would be easy enough with an environment/config variable.

yoman's avatar

just remove '--headless' in drive method @ DuskTestCase.php

3 likes
kylerdmoore's avatar

@BROWNER12 - Anyone who's curious how this is done, this is how I did it.

.env.dusk.local

DUSK_HEADLESS=false

tests/DuskTestCase.php

$options = (new ChromeOptions)->addArguments([
    '--disable-gpu',
]);

if (env('DUSK_HEADLESS', false)) {
    $options->addArguments([
        '--headless',
    ]);
}

Just change DUSK_HEADLESS in the .env.dusk file to true if you want to see chrome or false if you don't.

browner12's avatar

One small thing to remember, don't use env() calls in your code. env() should only be called from your config files, and then use the config() call in your code. Otherwise you will run into issues if you try and cache your config values.

for example, in your services.php

'dusk_headless' => env('DUSK_HEADLESS'),

and then in DuskTestCase.php

$options = (new ChromeOptions)->addArguments([
    '--disable-gpu',
    config('dusk_headless') ? '--headless' : '',
]);
1 like
sardar1592's avatar

For anyone landing here in 2023, using Laravel 10, just add an env variable :

DUSK_HEADLESS_DISABLED=true

Then change your DuskTestCase file's hasHeadlessDisabled function as follows:

protected function hasHeadlessDisabled() : bool
    {
        return env('DUSK_HEADLESS_DISABLED', false);
    }

You will see the browser again. This also helps you not change the DuskTestCase file which you need to change again before committing if you are running your tests in CI.

Make sure to set the value to false in your .env.ci or your GitHub actions secrets.

Please or to participate in this conversation.