Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File download in headless chrome using Laravel/Dusk

I'm trying to automate a file download in headless chrome using Laravel/Dusk.In GUI mode,the file downloads just fine in my download folder.But in headless mode,the download does not take place at all.Is there any way to solve this issue?

like image 297
Madhav Kurup Avatar asked Dec 10 '25 06:12

Madhav Kurup


1 Answers

For those who come across this, I found a simple solution with the current version of Laravel at the time of writing this.

I suggest first creating a directory in your storage path called temp (probably want to gitignore this too), and then navigate to the DuskTestCase.php file setup with the Dusk installation.

Under the driver method, add the following under the section that initializes the ChromeOptions variable.

$options->setExperimentalOption('prefs', [
    'download.default_directory' => storage_path('temp')
]);

The driver function should now look like this:

$options = (new ChromeOptions())->addArguments([
    '--disable-gpu',
    '--headless',
    '--window-size=1920,1080'
]);

$options->setExperimentalOption('prefs', [
    'download.default_directory' => storage_path('temp')
]);

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

As a side note, this worked for me with a PDF file created via JS, so I can't definitively say how this works with a file downloaded from the back-end.

like image 114
Garrett Avatar answered Dec 12 '25 01:12

Garrett