Categories
Laravel Tutorial

How to Configure Laravel Dusk ChromeDriver

Why?

When I set out to build a simple website crawler with Laravel I started from scratch using Guzzle. I needed to input a website URL and generate a sitemap with every link that belonged to the same URL. I accomplished this, but it was finicky because of Guzzle’s inherent limitations as an HTTP client.

I needed to simulate a real browser so that I could crawl JS only website (think React, Vue, etc.). Laravel Dusk to the rescue. I had my crawler up and running with Dusk in 10 minutes thanks to this guide by Tushar Gugnani.

Problems arose when my crawler encountered links which lead to PDFs. The PDFs would be downloaded to the root directory of my Laravel Application. Configuring a download directory for Dusk was impossible to find help with.

So here we are.

Where to configure Chromedriver within Laravel Dusk?

After you install Laravel Dusk, a file called DuskTestCase.php will exist within the “tests/” directory of your Laravel App. It is within DuskTestCase.php where you can configure the virtual Chrome browser that Dusk will use.

You will configure ChromeDriver within the driver() method of this file:

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

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

What you can configure

I will explain two ways to configure ChromeDriver; Preferences and Switches.

Preferences

Chrome preferences consist of the settings you can find normally in Chrome within Settings. Preferences allow you to control familiar parameters such as where downloads should be stored.

You can refer to this list of Chrome Preferences at your disposal.

Switches

Chrome switches are much like Chrome preferences but more advanced. Switches are simply parameters that control how Chrome behaves but they can’t be manipulated by the average user. By leveraging switches you can control things such as disabling extensions to setting frame rate.

You can refer to this list of Chrome Switches at your disposal.

How to set your configurations

Within DuskTestCase.php let’s set up some switches. Notice that we’re setting the $options variable, adding arguments to it, then adding “experimental options” to it. In this example I am adding two preferences:

  1. ‘download.prompt_for_download’ as false
  2. ‘download.default_directory’ as ‘downloads’

Modified driver() function in Laravel Dusk DuskTestCase.php:

protected function driver()
    {

        $options = (new ChromeOptions);

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

        $options->setExperimentalOption('prefs', [
            'download.prompt_for_download' => false,
            'download.default_directory' => 'downloads'
        ]);

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

Questions? Improvements?

If you have questions or found errors in my explanation, please post a comment so that I may improve this article.

Leave a Reply

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