70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use Facebook\WebDriver\Chrome\ChromeOptions;
|
|
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
|
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
|
use Illuminate\Support\Collection;
|
|
use Laravel\Dusk\TestCase as BaseTestCase;
|
|
use PHPUnit\Framework\Attributes\BeforeClass;
|
|
|
|
abstract class DuskTestCase extends BaseTestCase
|
|
{
|
|
/**
|
|
* Kiểm tra xem có nên chạy Chrome ở chế độ headless không.
|
|
*
|
|
* Ưu tiên:
|
|
* 1. Nếu DUSK_HEADLESS=true (set từ shell trước khi PHP chạy) → headless
|
|
* 2. Ngược lại fallback về DUSK_HEADLESS_DISABLED trong .env
|
|
*/
|
|
protected function hasHeadlessDisabled(): bool
|
|
{
|
|
if (isset($_SERVER['DUSK_HEADLESS']) || isset($_ENV['DUSK_HEADLESS'])) {
|
|
return false; // Buộc headless dù .env có DUSK_HEADLESS_DISABLED
|
|
}
|
|
|
|
return isset($_SERVER['DUSK_HEADLESS_DISABLED']) ||
|
|
isset($_ENV['DUSK_HEADLESS_DISABLED']);
|
|
}
|
|
|
|
/**
|
|
* Prepare for Dusk test execution.
|
|
*/
|
|
#[BeforeClass]
|
|
public static function prepare(): void
|
|
{
|
|
if (! static::runningInSail()) {
|
|
static::startChromeDriver(['--port=9515']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create the RemoteWebDriver instance.
|
|
*/
|
|
protected function driver(): RemoteWebDriver
|
|
{
|
|
$options = (new ChromeOptions)->addArguments(collect([
|
|
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
|
|
'--disable-search-engine-choice-screen',
|
|
'--disable-smooth-scrolling',
|
|
'--no-sandbox',
|
|
'--disable-dev-shm-usage',
|
|
])->unless($this->hasHeadlessDisabled(), function (Collection $items) {
|
|
return $items->merge([
|
|
'--disable-gpu',
|
|
'--headless=new',
|
|
]);
|
|
})->all());
|
|
|
|
$options->setBinary('/usr/bin/google-chrome');
|
|
|
|
return RemoteWebDriver::create(
|
|
$_ENV['DUSK_DRIVER_URL'] ?? env('DUSK_DRIVER_URL') ?? 'http://localhost:9515',
|
|
DesiredCapabilities::chrome()->setCapability(
|
|
ChromeOptions::CAPABILITY, $options
|
|
)
|
|
);
|
|
}
|
|
}
|