3

I am using Laravel Dusk to automate procedures with my browser Laravel Dusk uses ChromeDriver to automate the issue I am having that I have some extensions installed on my regular Google Chrome

However when I load the ChromeDriver via Laravel Dusk none of the extensions load with it.

Is there way to make them load?

starlev
  • 57
  • 1
  • 6

1 Answers1

1

Wondering myself how to do that, here is a solution using MetaMask extension as example:

First you need to edit the driver function of the DuskTestCase.php file:

/**
 * Create the RemoteWebDriver instance.
 *
 * Get "user-data-dir" from Google Chrome Profile Path parameters: chrome://version/
 *
 * @return \Facebook\WebDriver\Remote\RemoteWebDriver
 */
protected function driver()
{
    // --user-data-dir & --profile-directory are optional
    $options = (new ChromeOptions)->addArguments(collect([
        '--window-size=1920,1080',
        //'--user-data-dir='.$_ENV["GOOGLE_CHROME_USER_DATA_DIR"],
        // '--profile-directory='.$_ENV["GOOGLE_CHROME_USER_PROFILE"],
    ])->unless($this->hasHeadlessDisabled(), function ($items) {
        return $items->merge([
           //'--disable-gpu',
           //'--headless',
        ]);
    })->all());

    // Here you add an array with the paths of the extensions you want to add, must be files in .CRX format
    try {
        $options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);
    } catch (\Throwable $e) {
        //
    }

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

The code '$options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);' specify an array of paths of extensions files in CRX format.

So you need to have your extensions in .crx in a directory of your choice.

To convert a Google Chrome Extension in the CRX format, here are the steps: (source: https://dev.to/ltmenezes/automated-dapps-scrapping-with-selenium-and-metamask-2ae9)

  • Install Metamask (or the extension of your choice) on your regular Chrome
  • Navigate to chrome://extensions/
  • Click 'Pack extension' and enter the local path to the Metamask extension. Somewhere like in there in Mac Os X PATH_TO/Library/Application Support/Google/Chrome/Default/Extensions/EXTENSION_IDENTIFIER/FOLDER/

See: Where does Chrome store extensions?

This will generate a .crx file that you can use to load as an extension on Chromium.

In case the extension needs a lot of configurations the first time, it's better to install it on a fresh Google Chrome Profile, configure it, then provide the --user-data-dir and --profile-directory as ChromeOptions to load it. In that specific case, the extension will already be installed, so the part with '$options->addExtensions([$_ENV['METAMASK_EXTENSION_PATH']]);' can be commented.

# Google CRX3 from extension to download a Chrome extension in .crx format
METAMASK_EXTENSION_PATH=/CHANGE_ME/metamask-chrome-10.8.1.crx

# See Profile Path in Google Chrome with chrome://version/
# The last part of the full Profile Path is the User Profile name to set for GOOGLE_CHROME_USER_PROFILE,
# remove that last part to set GOOGLE_CHROME_USER_DATA_DIR
GOOGLE_CHROME_USER_DATA_DIR="/CHANGE_ME/Library/Application Support/Google/Chrome/"
GOOGLE_CHROME_USER_PROFILE=Default
Marius
  • 2,946
  • 1
  • 18
  • 18