Skip to content

Commit

Permalink
Fix ChromeDriverCommand to use Chrome 115 as new minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Jul 20, 2023
1 parent 35f4242 commit c6938b4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
stopOnError="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Laravel Dusk Test Suite">
Expand Down
104 changes: 45 additions & 59 deletions src/Console/ChromeDriverCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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',
];

/**
Expand Down Expand Up @@ -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',
],
];
Expand All @@ -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();
Expand All @@ -161,6 +131,8 @@ public function handle()
* Get the desired ChromeDriver version.
*
* @return string
*
* @throws \Exception
*/
protected function version()
{
Expand All @@ -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()
{
Expand All @@ -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.');
}

/**
Expand Down Expand Up @@ -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',
Expand All @@ -274,7 +260,7 @@ protected function extract($archive)

$zip->extractTo($this->directory);

$binary = $zip->getNameIndex(0);
$binary = $zip->getNameIndex(1);

$zip->close();

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion tests/ChromeProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down

0 comments on commit c6938b4

Please sign in to comment.