From f7093bb5a58414bc85554a93fbb831c6ae6d9030 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Tue, 19 Nov 2024 21:28:46 +0100 Subject: [PATCH 1/4] Fix and streamline capability handling --- src/WebdriverClassicDriver.php | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/WebdriverClassicDriver.php b/src/WebdriverClassicDriver.php index 438abd5..38f6bb1 100644 --- a/src/WebdriverClassicDriver.php +++ b/src/WebdriverClassicDriver.php @@ -789,29 +789,22 @@ private function getNormalisedBrowserName(): string */ private function initCapabilities(array $desiredCapabilities): DesiredCapabilities { - // Build base capabilities - $caps = $this->getBrowserSpecificCapabilities() ?? new DesiredCapabilities(); - - // Set defaults - $defaults = array_merge( - self::DEFAULT_CAPABILITIES['default'], - self::DEFAULT_CAPABILITIES[$this->getNormalisedBrowserName()] ?? [] - ); - foreach ($defaults as $key => $value) { - if ($caps->getCapability($key) === null) { - $caps->setCapability($key, $value); - } - } - - // Merge in other requested types - foreach ($desiredCapabilities as $key => $value) { - $caps->setCapability($key, $value); + $capabilities = $this->createBrowserSpecificCapabilities(); + + foreach ( + array_merge( + self::DEFAULT_CAPABILITIES['default'], + self::DEFAULT_CAPABILITIES[$this->getNormalisedBrowserName()] ?? [], + $desiredCapabilities, + ) as $capabilityKey => $capabilityValue + ) { + $capabilities->setCapability($capabilityKey, $capabilityValue); } - return $caps; + return $capabilities; } - private function getBrowserSpecificCapabilities(): ?DesiredCapabilities + private function createBrowserSpecificCapabilities(): DesiredCapabilities { switch ($this->getNormalisedBrowserName()) { case WebDriverBrowserType::FIREFOX: @@ -855,7 +848,7 @@ private function getBrowserSpecificCapabilities(): ?DesiredCapabilities case WebDriverBrowserType::MOCK: case WebDriverBrowserType::IE_HTA: default: - return null; + return new DesiredCapabilities(); } } From a5f49308d3e21fc8c9f65a0d2c6c25a511a9c80c Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Thu, 21 Nov 2024 22:25:55 +0100 Subject: [PATCH 2/4] Allow accessing capabilities; add capabilities tests --- src/WebdriverClassicDriver.php | 7 ++- tests/Custom/CapabilityTest.php | 100 ++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/Custom/CapabilityTest.php diff --git a/src/WebdriverClassicDriver.php b/src/WebdriverClassicDriver.php index 38f6bb1..5c8e221 100644 --- a/src/WebdriverClassicDriver.php +++ b/src/WebdriverClassicDriver.php @@ -762,7 +762,7 @@ protected function createWebDriver(): void throw new DriverException('Base driver has already been created'); } - $this->webDriver = RemoteWebDriver::create($this->webDriverHost, $this->desiredCapabilities); + $this->webDriver = RemoteWebDriver::create($this->webDriverHost, $this->getDesiredCapabilities()); } /** @@ -777,6 +777,11 @@ protected function getWebDriver(): RemoteWebDriver throw new DriverException('Base driver has not been created'); } + protected function getDesiredCapabilities(): array + { + return $this->desiredCapabilities->toArray(); + } + private function getNormalisedBrowserName(): string { return self::BROWSER_NAME_ALIAS_MAP[$this->browserName] ?? $this->browserName; diff --git a/tests/Custom/CapabilityTest.php b/tests/Custom/CapabilityTest.php new file mode 100644 index 0000000..6fb2a0a --- /dev/null +++ b/tests/Custom/CapabilityTest.php @@ -0,0 +1,100 @@ + $desiredCapabilities + * @param array $expectedCapabilities + * + * @dataProvider capabilitiesDataProvider + */ + public function testThatCapabilitiesAreAsExpected(string $browserName, array $desiredCapabilities, array $expectedCapabilities): void + { + $driver = $this->createDriverExposingCapabilities($browserName, $desiredCapabilities); + + $this->assertSame($expectedCapabilities, $driver->capabilities); + } + + public static function capabilitiesDataProvider(): iterable + { + yield 'unknown browser starts with default driver capabilities' => [ + 'browserName' => 'fake browser', + 'desiredCapabilities' => [], + 'expectedCapabilities' => [ + 'platform' => 'ANY', + 'name' => 'Behat Test', + 'deviceOrientation' => 'landscape', + 'deviceType' => 'desktop', + ], + ]; + + yield 'default capabilities can be customised' => [ + 'browserName' => 'fake browser', + 'desiredCapabilities' => [ + 'something' => 'custom', + 'name' => 'Custom Test', + ], + 'expectedCapabilities' => [ + 'platform' => 'ANY', + 'name' => 'Custom Test', + 'deviceOrientation' => 'landscape', + 'deviceType' => 'desktop', + 'something' => 'custom', + ], + ]; + + yield 'browser-specific default capabilities are added' => [ + 'browserName' => 'chrome', + 'desiredCapabilities' => [], + 'expectedCapabilities' => [ + 'browserName' => 'chrome', + 'platform' => 'ANY', + 'name' => 'Behat Test', + 'deviceOrientation' => 'landscape', + 'deviceType' => 'desktop', + 'goog:chromeOptions' => [ + 'excludeSwitches' => ['enable-automation'], + ], + ], + ]; + + yield 'browser-specific default capabilities can be customised' => [ + 'browserName' => 'chrome', + 'desiredCapabilities' => [ + 'name' => 'Custom Test', + 'goog:chromeOptions' => ['args' => ['a', 'b', 'c']], + ], + 'expectedCapabilities' => [ + 'browserName' => 'chrome', + 'platform' => 'ANY', + 'name' => 'Custom Test', + 'deviceOrientation' => 'landscape', + 'deviceType' => 'desktop', + 'goog:chromeOptions' => ['args' => ['a', 'b', 'c']], + ], + ]; + } + + /** + * @param string $browserName + * @param array $desiredCapabilities + * @return WebdriverClassicDriver&object{capabilities: array} + */ + private function createDriverExposingCapabilities(string $browserName, array $desiredCapabilities = []): WebdriverClassicDriver + { + return new class($browserName, $desiredCapabilities) extends WebdriverClassicDriver { + public ?array $capabilities = null; + + public function __construct(string $browserName, array $desiredCapabilities) + { + parent::__construct($browserName, $desiredCapabilities); + + $this->capabilities = $this->getDesiredCapabilities(); + } + }; + } +} From 024c979b8e593f5a11cc0bbbd94f003117f69fb5 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Thu, 21 Nov 2024 22:30:13 +0100 Subject: [PATCH 3/4] SA fix --- tests/Custom/CapabilityTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Custom/CapabilityTest.php b/tests/Custom/CapabilityTest.php index 6fb2a0a..e4c88ee 100644 --- a/tests/Custom/CapabilityTest.php +++ b/tests/Custom/CapabilityTest.php @@ -87,6 +87,9 @@ public static function capabilitiesDataProvider(): iterable private function createDriverExposingCapabilities(string $browserName, array $desiredCapabilities = []): WebdriverClassicDriver { return new class($browserName, $desiredCapabilities) extends WebdriverClassicDriver { + /** + * @var null|array + */ public ?array $capabilities = null; public function __construct(string $browserName, array $desiredCapabilities) From 25d1859f4a9db5afc4e303975efcf298c4942311 Mon Sep 17 00:00:00 2001 From: Christian Sciberras Date: Thu, 21 Nov 2024 22:31:39 +0100 Subject: [PATCH 4/4] Another SA fix --- tests/Custom/CapabilityTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Custom/CapabilityTest.php b/tests/Custom/CapabilityTest.php index e4c88ee..4d5f096 100644 --- a/tests/Custom/CapabilityTest.php +++ b/tests/Custom/CapabilityTest.php @@ -82,7 +82,7 @@ public static function capabilitiesDataProvider(): iterable /** * @param string $browserName * @param array $desiredCapabilities - * @return WebdriverClassicDriver&object{capabilities: array} + * @return WebdriverClassicDriver&object{capabilities: null|array} */ private function createDriverExposingCapabilities(string $browserName, array $desiredCapabilities = []): WebdriverClassicDriver {