From cce3833386abc5e9b81786fb735789ef19fc8ecb Mon Sep 17 00:00:00 2001 From: Kevin van Sonsbeek Date: Fri, 16 Feb 2024 20:57:06 +0100 Subject: [PATCH 1/4] bugfix: Update const to clearly refer to legacy endpoint --- src/Driver/ChromeDriver/VersionResolver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Driver/ChromeDriver/VersionResolver.php b/src/Driver/ChromeDriver/VersionResolver.php index 92f0461..2f6e65a 100644 --- a/src/Driver/ChromeDriver/VersionResolver.php +++ b/src/Driver/ChromeDriver/VersionResolver.php @@ -23,7 +23,7 @@ final class VersionResolver implements VersionResolverInterface { public const MAJOR_VERSION_ENDPOINT_BREAKPOINT = 115; - private const VERSION_ENDPOINT = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; + private const LEGACY_ENDPOINT = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; private const VERSION_ENDPOINT_JSON = 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json'; private HttpClientInterface $httpClient; @@ -68,7 +68,7 @@ public function fromBrowser(Browser $browser): Version public function latest(): Version { - $response = $this->httpClient->request('GET', self::VERSION_ENDPOINT); + $response = $this->httpClient->request('GET', self::LEGACY_ENDPOINT); $versionString = $response->getContent(); return Version::fromString($versionString); @@ -109,13 +109,13 @@ private function getVersionString(Browser $browser): string */ private function getBrowserVersionEndpoint(Browser $browser): string { - $versionEndpoint = sprintf('%s_%s', self::VERSION_ENDPOINT, $browser->version()->toString()); + $versionEndpoint = sprintf('%s_%s', self::LEGACY_ENDPOINT, $browser->version()->toString()); $stableVersion = $this->latest(); $betaVersionMajor = (int) $stableVersion->major() + 1; if ((int) $browser->version()->major() > $betaVersionMajor) { - $versionEndpoint = sprintf('%s_%s', self::VERSION_ENDPOINT, (string) $betaVersionMajor); + $versionEndpoint = sprintf('%s_%s', self::LEGACY_ENDPOINT, (string) $betaVersionMajor); } return $versionEndpoint; From 2cb0c10841181db038d62e8ab8c634b9c84e6555 Mon Sep 17 00:00:00 2001 From: Kevin van Sonsbeek Date: Fri, 16 Feb 2024 21:00:02 +0100 Subject: [PATCH 2/4] bugfix: Base the latest version on the new endpoint --- src/Driver/ChromeDriver/VersionResolver.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Driver/ChromeDriver/VersionResolver.php b/src/Driver/ChromeDriver/VersionResolver.php index 2f6e65a..e2fa6ca 100644 --- a/src/Driver/ChromeDriver/VersionResolver.php +++ b/src/Driver/ChromeDriver/VersionResolver.php @@ -23,7 +23,8 @@ final class VersionResolver implements VersionResolverInterface { public const MAJOR_VERSION_ENDPOINT_BREAKPOINT = 115; - private const LEGACY_ENDPOINT = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; + private const LEGACY_ENDPOINT = 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE'; + private const LATEST_VERSION_ENDPOINT_JSON = 'https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json'; private const VERSION_ENDPOINT_JSON = 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json'; private HttpClientInterface $httpClient; @@ -68,10 +69,13 @@ public function fromBrowser(Browser $browser): Version public function latest(): Version { - $response = $this->httpClient->request('GET', self::LEGACY_ENDPOINT); - $versionString = $response->getContent(); + $response = $this->httpClient->request('GET', self::LATEST_VERSION_ENDPOINT_JSON); + $versions = $response->toArray(); + if (! isset($versions['channels']['Stable']['version'])) { + throw new UnexpectedValueException('Could not resolve the latest stable version.'); + } - return Version::fromString($versionString); + return Version::fromString((string) $versions['channels']['Stable']['version']); } public function supports(Browser $browser): bool From 6bbaf463bc87a0d22574ae7ff9212ab417971a66 Mon Sep 17 00:00:00 2001 From: Kevin van Sonsbeek Date: Fri, 16 Feb 2024 21:10:30 +0100 Subject: [PATCH 3/4] bugfix: Update test --- tests/Driver/ChromeDriver/VersionResolverTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Driver/ChromeDriver/VersionResolverTest.php b/tests/Driver/ChromeDriver/VersionResolverTest.php index 6aa3d2e..4d25bbf 100644 --- a/tests/Driver/ChromeDriver/VersionResolverTest.php +++ b/tests/Driver/ChromeDriver/VersionResolverTest.php @@ -78,7 +78,7 @@ public function testFromGetBetaVersionForDevChrome(): void public function testLatest(): void { - self::assertEquals(Version::fromString('86.0.4240.22'), $this->versionResolver->latest()); + self::assertEquals(Version::fromString('121.0.6167.184'), $this->versionResolver->latest()); } protected function setUp(): void @@ -99,6 +99,12 @@ static function (string $method, string $url): MockResponse { return new MockResponse('87.0.4280.20'); } + if ($url === 'https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json') { + return new MockResponse( + json_encode(['channels' => ['Stable' => ['version' => '121.0.6167.184']]]) + ); + } + if ($url === 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json') { return new MockResponse( json_encode(['builds' => ['115.0.5751' => ['version' => '115.0.5751.20']]]) From a2ed451e6bf9bc65e9f150e4b732851df87cb5d9 Mon Sep 17 00:00:00 2001 From: Kevin van Sonsbeek Date: Fri, 16 Feb 2024 22:21:17 +0100 Subject: [PATCH 4/4] bugfix: Add fetching of the latest beta version, as the functionality to downscale to the last beta when using Dev or Canary was lost during the transition to the json endpoints --- src/Driver/ChromeDriver/VersionResolver.php | 25 ++++++++++++++++--- .../ChromeDriver/VersionResolverTest.php | 18 ++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/Driver/ChromeDriver/VersionResolver.php b/src/Driver/ChromeDriver/VersionResolver.php index e2fa6ca..ae974d5 100644 --- a/src/Driver/ChromeDriver/VersionResolver.php +++ b/src/Driver/ChromeDriver/VersionResolver.php @@ -85,6 +85,17 @@ public function supports(Browser $browser): bool return $browserName->equals(BrowserName::GOOGLE_CHROME()) || $browserName->equals(BrowserName::CHROMIUM()); } + private function latestBetaVersion(): Version + { + $response = $this->httpClient->request('GET', self::LATEST_VERSION_ENDPOINT_JSON); + $versions = $response->toArray(); + if (! isset($versions['channels']['Beta']['version'])) { + throw new UnexpectedValueException('Could not resolve the latest beta version.'); + } + + return Version::fromString((string) $versions['channels']['Beta']['version']); + } + /** * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface @@ -99,13 +110,21 @@ private function getVersionString(Browser $browser): string $response = $this->httpClient->request('GET', self::VERSION_ENDPOINT_JSON); $versions = $response->toArray(); - if (! array_key_exists($browser->version()->toString(), $versions['builds'])) { + + $latestBeta = $this->latestBetaVersion(); + $versionToFetch = $browser->version(); + if ((int) $versionToFetch->major() > (int) $latestBeta->major()) { + // In this case we're dealing with a Dev or Canary version, so we will take the last Beta version. + $versionToFetch = $latestBeta; + } + + if (! array_key_exists($versionToFetch->toString(), $versions['builds'])) { throw new UnexpectedValueException( - sprintf('There is no build for version : %s', $browser->version()->toString()) + sprintf('There is no build for version : %s', $versionToFetch->toString()) ); } - return $versions['builds'][$browser->version()->toString()]['version']; + return $versions['builds'][$versionToFetch->toString()]['version']; } /** diff --git a/tests/Driver/ChromeDriver/VersionResolverTest.php b/tests/Driver/ChromeDriver/VersionResolverTest.php index 4d25bbf..6e68fed 100644 --- a/tests/Driver/ChromeDriver/VersionResolverTest.php +++ b/tests/Driver/ChromeDriver/VersionResolverTest.php @@ -71,9 +71,9 @@ public function testFromExceptionIfCanNotParseVersionReceived(): void public function testFromGetBetaVersionForDevChrome(): void { - $devChrome = new Browser(BrowserName::GOOGLE_CHROME(), Version::fromString('88.0.4302.0'), OperatingSystem::MACOS()); + $devChrome = new Browser(BrowserName::GOOGLE_CHROME(), Version::fromString('123.0.6300.3'), OperatingSystem::MACOS()); - self::assertEquals(Version::fromString('87.0.4280.20'), $this->versionResolver->fromBrowser($devChrome)); + self::assertEquals(Version::fromString('122.0.6261.39'), $this->versionResolver->fromBrowser($devChrome)); } public function testLatest(): void @@ -101,13 +101,23 @@ static function (string $method, string $url): MockResponse { if ($url === 'https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json') { return new MockResponse( - json_encode(['channels' => ['Stable' => ['version' => '121.0.6167.184']]]) + json_encode([ + 'channels' => [ + 'Stable' => ['version' => '121.0.6167.184'], + 'Beta' => ['version' => '122.0.6261.39'], + ], + ]) ); } if ($url === 'https://googlechromelabs.github.io/chrome-for-testing/latest-patch-versions-per-build.json') { return new MockResponse( - json_encode(['builds' => ['115.0.5751' => ['version' => '115.0.5751.20']]]) + json_encode([ + 'builds' => [ + '115.0.5751' => ['version' => '115.0.5751.20'], + '122.0.6261' => ['version' => '122.0.6261.39'], + ], + ]) ); } }