Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Created February 2, 2023 17:04
Show Gist options
  • Save kmuenkel/926d778de858948afd89b7a287d7d8da to your computer and use it in GitHub Desktop.
Save kmuenkel/926d778de858948afd89b7a287d7d8da to your computer and use it in GitHub Desktop.
Control which browser plugin will be used by Dusk by .env values
<?php
namespace Tests;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
const CHROME_DRIVER = 'chrome';
const FIREFOX_DRIVER = 'firefox';
protected static string $driver = self::CHROME_DRIVER;
/**
* Prepare for Dusk test execution.
*
* @beforeClass
*/
public static function prepare(): void
{
static::setDriver(env('SELENIUM_DRIVER', self::CHROME_DRIVER));
static::runningInSail() || static::startChromeDriver();
}
public static function setDriver(string $driver): void
{
self::$driver = $driver;
}
/**
* Create the RemoteWebDriver instance.
*/
protected function driver(): RemoteWebDriver
{
$args = [
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
'--ignore-certificate-errors',
'--ignore-ssl-errors'
];
$headlessArgs = fn (Collection $items) => $items->merge([
'--disable-gpu',
'--headless',
'--no-sandbox',
'--disable-dev-shm-usage',
'--disable-software-rasterizer'
]);
$args = collect($args)->unless($this->hasHeadlessDisabled(), $headlessArgs)->all();
return match (self::$driver) {
self::CHROME_DRIVER => $this->chromeDriver($args),
self::FIREFOX_DRIVER => $this->firefoxDriver($args),
default => throw new InvalidArgumentException('Unsupported driver: \'' . self::$driver . '\'')
};
}
protected function chromeDriver(array $args = []): RemoteWebDriver
{
$options = (new ChromeOptions)->addArguments($args);
$capabilities = (DesiredCapabilities::chrome())
->setCapability('acceptInsecureCerts', true)
->setCapability($options::CAPABILITY, $options);
return RemoteWebDriver::create(env('DUSK_CHROME_DRIVER_URL', 'http://localhost:9515'), $capabilities);
}
protected function firefoxDriver(array $args = []): RemoteWebDriver
{
$options = (new FirefoxOptions)->addArguments($args);
$capabilities = (DesiredCapabilities::firefox())
->setCapability('acceptInsecureCerts', true)
->setCapability($options::CAPABILITY, $options);
return RemoteWebDriver::create(env('DUSK_FIREFOX_DRIVER_URL', 'http://localhost:9515'), $capabilities);
}
/**
* Determine whether the Dusk command has disabled headless mode.
*/
protected function hasHeadlessDisabled(): bool
{
return isset($_SERVER['DUSK_HEADLESS_DISABLED']) || env('DUSK_HEADLESS_DISABLED');
}
/**
* Determine if the browser window should start maximized.
*/
protected function shouldStartMaximized(): bool
{
return isset($_SERVER['DUSK_START_MAXIMIZED']) || env('DUSK_START_MAXIMIZED');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment