Skip to content
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

Fix and streamline capability handling #51

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -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;
Expand All @@ -789,29 +794,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:
Expand Down Expand Up @@ -855,7 +853,7 @@ private function getBrowserSpecificCapabilities(): ?DesiredCapabilities
case WebDriverBrowserType::MOCK:
case WebDriverBrowserType::IE_HTA:
default:
return null;
return new DesiredCapabilities();
}
}

Expand Down
103 changes: 103 additions & 0 deletions tests/Custom/CapabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace Mink\WebdriverClassicDriver\Tests\Custom;

use Mink\WebdriverClassicDriver\WebdriverClassicDriver;

class CapabilityTest extends \PHPUnit\Framework\TestCase
{
/**
* @param array<string, mixed> $desiredCapabilities
* @param array<string, mixed> $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<string, mixed> $desiredCapabilities
* @return WebdriverClassicDriver&object{capabilities: null|array<string, mixed>}
*/
private function createDriverExposingCapabilities(string $browserName, array $desiredCapabilities = []): WebdriverClassicDriver
{
return new class($browserName, $desiredCapabilities) extends WebdriverClassicDriver {
/**
* @var null|array<string, mixed>
*/
public ?array $capabilities = null;

public function __construct(string $browserName, array $desiredCapabilities)
{
parent::__construct($browserName, $desiredCapabilities);

$this->capabilities = $this->getDesiredCapabilities();
}
};
}
}