From c6938b4c708c83d83a170919fc6a94d24cd2914d Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 20 Jul 2023 11:38:11 -0500 Subject: [PATCH] Fix ChromeDriverCommand to use Chrome 115 as new minimum --- phpunit.xml.dist | 2 - src/Console/ChromeDriverCommand.php | 104 ++++++++++++---------------- tests/ChromeProcessTest.php | 2 +- 3 files changed, 46 insertions(+), 62 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 59a79a707..38fc0dc9d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,8 +11,6 @@ stopOnError="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" - cacheDirectory=".phpunit.cache" - backupStaticProperties="false" > diff --git a/src/Console/ChromeDriverCommand.php b/src/Console/ChromeDriverCommand.php index ea909ca79..a8b3c3347 100644 --- a/src/Console/ChromeDriverCommand.php +++ b/src/Console/ChromeDriverCommand.php @@ -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; @@ -35,21 +37,14 @@ class ChromeDriverCommand extends Command * * @var string */ - protected $latestVersionUrl = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; + protected $latestVersionUrl = 'https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions-with-downloads.json'; /** - * URL to the latest release version for a major Chrome version. + * URL to the latest release versions for Chrome. * * @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'; + protected $versionsUrl = 'https://googlechromelabs.github.io/chrome-for-testing/latest-versions-per-milestone-with-downloads.json'; /** * Download slugs for the available operating systems. @@ -59,44 +54,10 @@ class ChromeDriverCommand extends Command protected $slugs = [ 'linux' => 'linux64', 'mac' => 'mac64', - 'mac-intel' => 'mac64', - 'mac-arm' => 'mac_arm64', + 'mac-intel' => 'mac-x64', + 'mac-arm' => 'mac-arm64', 'win' => 'win32', - ]; - - /** - * The legacy versions for the ChromeDriver. - * - * @var array - */ - protected $legacyVersions = [ - 43 => '2.20', - 44 => '2.20', - 45 => '2.20', - 46 => '2.21', - 47 => '2.21', - 48 => '2.21', - 49 => '2.22', - 50 => '2.22', - 51 => '2.23', - 52 => '2.24', - 53 => '2.26', - 54 => '2.27', - 55 => '2.28', - 56 => '2.29', - 57 => '2.29', - 58 => '2.31', - 59 => '2.32', - 60 => '2.33', - 61 => '2.34', - 62 => '2.35', - 63 => '2.36', - 64 => '2.37', - 65 => '2.38', - 66 => '2.40', - 67 => '2.41', - 68 => '2.42', - 69 => '2.44', + 'win64' => 'win64', ]; /** @@ -124,7 +85,10 @@ class ChromeDriverCommand extends Command 'mac-arm' => [ '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version', ], - 'win' => [ + 'win32' => [ + 'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version', + ], + 'win64' => [ 'reg query "HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon" /v version', ], ]; @@ -138,6 +102,12 @@ public function handle() { $version = $this->version(); + $milestone = (int) $version; + + if ($milestone < 115) { + throw new Exception('Dusk v7 requires Chrome 115 or above.'); + } + $all = $this->option('all'); $currentOS = OperatingSystem::id(); @@ -161,6 +131,8 @@ public function handle() * Get the desired ChromeDriver version. * * @return string + * + * @throws \Exception */ protected function version() { @@ -178,21 +150,20 @@ protected function version() return $version; } - $version = (int) $version; + $milestone = (int) $version; - if ($version < 70) { - return $this->legacyVersions[$version]; - } + $milestones = json_decode($this->getUrl($this->versionsUrl), true); - return trim($this->getUrl( - sprintf($this->versionUrl, $version) - )); + return $milestones['milestones'][$milestone]['version'] + ?? throw new Exception('Could not get the ChromeDriver version.'); } /** * Get the latest stable ChromeDriver version. * * @return string + * + * @throws \Exception */ protected function latestVersion() { @@ -211,7 +182,12 @@ protected function latestVersion() $streamOptions['http'] = ['proxy' => $this->option('proxy'), 'request_fulluri' => true]; } - return trim(file_get_contents($this->latestVersionUrl, false, stream_context_create($streamOptions))); + $versions = json_decode(file_get_contents( + $this->latestVersionUrl, false, stream_context_create($streamOptions) + ), true); + + return $versions['channels']['Stable']['version'] + ?? throw new Exception('Could not get the latest ChromeDriver version.'); } /** @@ -247,10 +223,20 @@ protected function detectChromeVersion($os) * @param string $version * @param string $slug * @return string + * + * @throws \Exception */ protected function download($version, $slug) { - $url = sprintf($this->downloadUrl, $version, $slug); + $milestone = (int) $version; + + $versions = json_decode($this->getUrl($this->versionsUrl), true); + + $chromedrivers = $versions['milestones'][$milestone]['downloads']['chromedriver'] + ?? throw new Exception('Could not get the ChromeDriver version.'); + + $url = collect($chromedrivers)->firstWhere('platform', $slug)['url'] + ?? throw new Exception('Could not get the ChromeDriver version.'); file_put_contents( $archive = $this->directory.'chromedriver.zip', @@ -274,7 +260,7 @@ protected function extract($archive) $zip->extractTo($this->directory); - $binary = $zip->getNameIndex(0); + $binary = $zip->getNameIndex(1); $zip->close(); @@ -292,7 +278,7 @@ protected function extract($archive) */ protected function rename($binary, $os) { - $newName = str_replace('chromedriver', 'chromedriver-'.$os, $binary); + $newName = Str::after(str_replace('chromedriver', 'chromedriver-'.$os, $binary), DIRECTORY_SEPARATOR); rename($this->directory.$binary, $this->directory.$newName); diff --git a/tests/ChromeProcessTest.php b/tests/ChromeProcessTest.php index 4e8b41aab..b6fa8ba60 100644 --- a/tests/ChromeProcessTest.php +++ b/tests/ChromeProcessTest.php @@ -24,7 +24,7 @@ public function test_build_process_for_windows() try { (new ChromeProcessWindows)->toProcess(); } catch (RuntimeException $exception) { - $this->assertStringContainsString('chromedriver-win.exe', $exception->getMessage()); + $this->assertStringContainsString('chromedriver-win32.exe', $exception->getMessage()); } }