-
Notifications
You must be signed in to change notification settings - Fork 327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[7.x] Fix ChromeDriverCommand for ChromeDriver 115+ #1043
Changes from 1 commit
406e77d
66ad852
0b28c2f
049fef8
ca6de8b
47cb880
2b44d8c
806b1fc
aaa2374
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
namespace Laravel\Dusk\Console; | ||
|
||
use Exception; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Str; | ||
use Laravel\Dusk\OperatingSystem; | ||
use Symfony\Component\Process\Process; | ||
use ZipArchive; | ||
|
@@ -30,40 +32,6 @@ class ChromeDriverCommand extends Command | |
*/ | ||
protected $description = 'Install the ChromeDriver binary'; | ||
|
||
/** | ||
* URL to the latest stable release version. | ||
* | ||
* @var string | ||
*/ | ||
protected $latestVersionUrl = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; | ||
|
||
/** | ||
* URL to the latest release version for a major Chrome version. | ||
* | ||
* @var string | ||
*/ | ||
protected $versionUrl = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE_%d'; | ||
|
||
/** | ||
* URL to the ChromeDriver download. | ||
* | ||
* @var string | ||
*/ | ||
protected $downloadUrl = 'https://chromedriver.storage.googleapis.com/%s/chromedriver_%s.zip'; | ||
|
||
/** | ||
* Download slugs for the available operating systems. | ||
* | ||
* @var array | ||
*/ | ||
protected $slugs = [ | ||
'linux' => 'linux64', | ||
'mac' => 'mac64', | ||
'mac-intel' => 'mac64', | ||
'mac-arm' => 'mac_arm64', | ||
'win' => 'win32', | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
|
||
/** | ||
* The legacy versions for the ChromeDriver. | ||
* | ||
|
@@ -106,29 +74,6 @@ class ChromeDriverCommand extends Command | |
*/ | ||
protected $directory = __DIR__.'/../../bin/'; | ||
|
||
/** | ||
* The default commands to detect the installed Chrome / Chromium version. | ||
* | ||
* @var array | ||
*/ | ||
protected $chromeVersionCommands = [ | ||
'linux' => [ | ||
'/usr/bin/google-chrome --version', | ||
'/usr/bin/chromium-browser --version', | ||
'/usr/bin/chromium --version', | ||
'/usr/bin/google-chrome-stable --version', | ||
], | ||
'mac-intel' => [ | ||
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', | ||
], | ||
'mac-arm' => [ | ||
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', | ||
], | ||
'win' => [ | ||
'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version', | ||
], | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to |
||
|
||
/** | ||
* Execute the console command. | ||
* | ||
|
@@ -142,11 +87,11 @@ public function handle() | |
|
||
$currentOS = OperatingSystem::id(); | ||
|
||
foreach ($this->slugs as $os => $slug) { | ||
foreach (OperatingSystem::all() as $os) { | ||
if ($all || ($os === $currentOS)) { | ||
$archive = $this->download($version, $slug); | ||
$archive = $this->download($version, $os); | ||
|
||
$binary = $this->extract($archive); | ||
$binary = $this->extract($version, $archive); | ||
|
||
$this->rename($binary, $os); | ||
} | ||
|
@@ -182,11 +127,14 @@ protected function version() | |
|
||
if ($version < 70) { | ||
return $this->legacyVersions[$version]; | ||
} elseif ($version < 115) { | ||
return $this->fetchChromeVersionFromUrl($version); | ||
} | ||
|
||
return trim($this->getUrl( | ||
sprintf($this->versionUrl, $version) | ||
)); | ||
$milestones = $this->resolveChromeVersionsPerMilestone(); | ||
|
||
return $milestones['milestones'][$version]['version'] | ||
?? throw new Exception('Could not get the ChromeDriver version.'); | ||
} | ||
|
||
/** | ||
|
@@ -196,22 +144,10 @@ protected function version() | |
*/ | ||
protected function latestVersion() | ||
{ | ||
$streamOptions = []; | ||
|
||
if ($this->option('ssl-no-verify')) { | ||
$streamOptions = [ | ||
'ssl' => [ | ||
'verify_peer' => false, | ||
'verify_peer_name' => false, | ||
], | ||
]; | ||
} | ||
|
||
if ($this->option('proxy')) { | ||
$streamOptions['http'] = ['proxy' => $this->option('proxy'), 'request_fulluri' => true]; | ||
} | ||
$versions = json_decode($this->getUrl('https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json'), true); | ||
|
||
return trim(file_get_contents($this->latestVersionUrl, false, stream_context_create($streamOptions))); | ||
return $versions['channels']['Stable']['version'] | ||
?? throw new Exception('Could not get the latest ChromeDriver version.'); | ||
} | ||
|
||
/** | ||
|
@@ -222,7 +158,7 @@ protected function latestVersion() | |
*/ | ||
protected function detectChromeVersion($os) | ||
{ | ||
foreach ($this->chromeVersionCommands[$os] as $command) { | ||
foreach (OperatingSystem::chromeVersionCommands($os) as $command) { | ||
$process = Process::fromShellCommandline($command); | ||
|
||
$process->run(); | ||
|
@@ -245,12 +181,12 @@ protected function detectChromeVersion($os) | |
* Download the ChromeDriver archive. | ||
* | ||
* @param string $version | ||
* @param string $slug | ||
* @param string $os | ||
* @return string | ||
*/ | ||
protected function download($version, $slug) | ||
protected function download($version, $os) | ||
{ | ||
$url = sprintf($this->downloadUrl, $version, $slug); | ||
$url = $this->resolveChromeDriverDownloadUrl($version, $os); | ||
|
||
file_put_contents( | ||
$archive = $this->directory.'chromedriver.zip', | ||
|
@@ -263,18 +199,19 @@ protected function download($version, $slug) | |
/** | ||
* Extract the ChromeDriver binary from the archive and delete the archive. | ||
* | ||
* @param string $version | ||
* @param string $archive | ||
* @return string | ||
*/ | ||
protected function extract($archive) | ||
protected function extract($version, $archive) | ||
{ | ||
$zip = new ZipArchive; | ||
|
||
$zip->open($archive); | ||
|
||
$zip->extractTo($this->directory); | ||
|
||
$binary = $zip->getNameIndex(0); | ||
$binary = $zip->getNameIndex(version_compare($version, '115.0', '<') ? 0 : 1); | ||
|
||
$zip->close(); | ||
|
||
|
@@ -292,7 +229,9 @@ protected function extract($archive) | |
*/ | ||
protected function rename($binary, $os) | ||
{ | ||
$newName = str_replace('chromedriver', 'chromedriver-'.$os, $binary); | ||
$newName = Str::contains($binary, DIRECTORY_SEPARATOR) | ||
? Str::after(str_replace('chromedriver', 'chromedriver-'.$os, $binary), DIRECTORY_SEPARATOR) | ||
: str_replace('chromedriver', 'chromedriver-'.$os, $binary); | ||
|
||
rename($this->directory.$binary, $this->directory.$newName); | ||
|
||
|
@@ -302,7 +241,6 @@ protected function rename($binary, $os) | |
/** | ||
* Get the contents of a URL using the 'proxy' and 'ssl-no-verify' command options. | ||
* | ||
* @param string $url | ||
* @return string|bool | ||
*/ | ||
protected function getUrl(string $url) | ||
|
@@ -321,4 +259,55 @@ protected function getUrl(string $url) | |
|
||
return file_get_contents($url, false, $streamContext); | ||
} | ||
|
||
/** | ||
* Get the chrome version from URL. | ||
* | ||
* @return string | ||
*/ | ||
protected function fetchChromeVersionFromUrl(int $version) | ||
{ | ||
return trim((string) $this->getUrl( | ||
sprintf('https://chromedriver.storage.googleapis.com/LATEST_RELEASE_%d', $version) | ||
)); | ||
} | ||
|
||
/** | ||
* Get the chrome versions per milestone. | ||
* | ||
* @return array | ||
*/ | ||
protected function resolveChromeVersionsPerMilestone() | ||
{ | ||
return json_decode( | ||
$this->getUrl('https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.json'), true | ||
); | ||
} | ||
|
||
/** | ||
* Resolve the download url. | ||
* | ||
* @return string | ||
* | ||
* @throws \Exception | ||
*/ | ||
protected function resolveChromeDriverDownloadUrl(string $version, string $os) | ||
{ | ||
$slug = OperatingSystem::chromeDriverSlug($os, $version); | ||
|
||
if (version_compare($version, '115.0', '<')) { | ||
return sprintf('https://chromedriver.storage.googleapis.com/%s/chromedriver_%s.zip', $version, $slug); | ||
} | ||
|
||
$milestone = (int) $version; | ||
|
||
$versions = $this->resolveChromeVersionsPerMilestone(); | ||
|
||
/** @var array<string, mixed> $chromedrivers */ | ||
$chromedrivers = $versions['milestones'][$milestone]['downloads']['chromedriver'] | ||
?? throw new Exception('Could not get the ChromeDriver version.'); | ||
|
||
return collect($chromedrivers)->firstWhere('platform', $slug)['url'] | ||
?? throw new Exception('Could not get the ChromeDriver version.'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,107 @@ | |
namespace Laravel\Dusk; | ||
|
||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
|
||
class OperatingSystem | ||
{ | ||
/** | ||
* List of available Operating System platforms. | ||
* | ||
* @var array<string, array{slug: string, binary: string, commands: array<int, string>}> | ||
*/ | ||
protected static $platforms = [ | ||
'linux' => [ | ||
'slug' => 'linux64', | ||
'commands' => [ | ||
'/usr/bin/google-chrome --version', | ||
'/usr/bin/chromium-browser --version', | ||
'/usr/bin/chromium --version', | ||
'/usr/bin/google-chrome-stable --version', | ||
], | ||
], | ||
'mac' => [ | ||
'slug' => 'mac-x64', | ||
'commands' => [ | ||
'/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', | ||
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', | ||
], | ||
], | ||
'mac-intel' => [ | ||
'slug' => 'mac-x64', | ||
'commands' => [ | ||
'/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', | ||
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', | ||
], | ||
], | ||
'mac-arm' => [ | ||
'slug' => 'mac-arm64', | ||
'commands' => [ | ||
'/Applications/Google\ Chrome\ for\ Testing.app/Contents/MacOS/Google\ Chrome\ for\ Testing --version', | ||
'/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add support for |
||
], | ||
], | ||
'win' => [ | ||
'slug' => 'win32', | ||
'commands' => [ | ||
'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version', | ||
], | ||
], | ||
]; | ||
|
||
/** | ||
* Resolve Chrome version commands. | ||
* | ||
* @param string $operatingSystem | ||
* @return array<int, string> | ||
*/ | ||
public static function chromeVersionCommands($operatingSystem) | ||
{ | ||
$commands = static::$platforms[$operatingSystem]['commands'] ?? null; | ||
|
||
if (\is_null($commands)) { | ||
throw new InvalidArgumentException("Unable to find commands for Operating System [{$operatingSystem}]"); | ||
} | ||
|
||
return $commands; | ||
} | ||
|
||
/** | ||
* Resolve ChromeDriver slug. | ||
* | ||
* @param string $operatingSystem | ||
* @param string|null $version | ||
* @return string | ||
*/ | ||
public static function chromeDriverSlug($operatingSystem, $version = null) | ||
{ | ||
$slug = static::$platforms[$operatingSystem]['slug'] ?? null; | ||
|
||
if (\is_null($slug)) { | ||
throw new InvalidArgumentException("Unable to find ChromeDriver slug for Operating System [{$operatingSystem}]"); | ||
} | ||
|
||
if (! \is_null($version) && version_compare($version, '115.0', '<')) { | ||
if ($slug === 'mac-arm64') { | ||
return version_compare($version, '106.0.5249', '<') ? 'mac64_m1' : 'mac_arm64'; | ||
} elseif ($slug === 'mac-x64') { | ||
return 'mac64'; | ||
} | ||
} | ||
|
||
return $slug; | ||
} | ||
|
||
/** | ||
* Returns all possible OS. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public static function all() | ||
{ | ||
return array_keys(static::$platforms); | ||
} | ||
|
||
/** | ||
* Returns the current OS identifier. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
URLs will be hardcoded based on usage to support different version.