Skip to content

Commit

Permalink
Implemenent driver initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 4, 2025
1 parent 5206bb4 commit d1f395c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
24 changes: 22 additions & 2 deletions src/WebdriverClassicDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
use Facebook\WebDriver\WebDriverSelect;
use JetBrains\PhpStorm\Language;

/**
* @phpstan-type TWebDriverInstantiator callable(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver
*/
class WebdriverClassicDriver extends CoreDriver
{
public const DEFAULT_BROWSER = WebDriverBrowserType::CHROME;
Expand Down Expand Up @@ -81,19 +84,27 @@ class WebdriverClassicDriver extends CoreDriver

private string $webDriverHost;

/**
* @var TWebDriverInstantiator
*/
private $webDriverInstantiator;

private ?string $initialWindowHandle = null;

/**
* @param string $browserName One of 'edge', 'firefox', 'chrome' or any one of {@see WebDriverBrowserType} constants.
* @param TWebDriverInstantiator|null $webDriverInstantiator
*/
public function __construct(
string $browserName = self::DEFAULT_BROWSER,
array $desiredCapabilities = [],
string $webDriverHost = 'http://localhost:4444/wd/hub'
string $webDriverHost = 'http://localhost:4444/wd/hub',
?callable $webDriverInstantiator = null
) {
$this->browserName = $browserName;
$this->desiredCapabilities = $this->initCapabilities($desiredCapabilities);
$this->webDriverHost = $webDriverHost;
$this->webDriverInstantiator = $webDriverInstantiator ?? [self::class, 'instantiateWebDriver'];
}

// <editor-fold desc="Implementation">
Expand Down Expand Up @@ -762,7 +773,7 @@ protected function createWebDriver(): void
throw new DriverException('Base driver has already been created');
}

$this->webDriver = RemoteWebDriver::create($this->webDriverHost, $this->getDesiredCapabilities());
$this->webDriver = ($this->webDriverInstantiator)($this->webDriverHost, $this->desiredCapabilities);
}

/**
Expand All @@ -782,6 +793,15 @@ protected function getDesiredCapabilities(): array
return $this->desiredCapabilities->toArray();
}

// </editor-fold>

// <editor-fold desc="Private Utilities">

private static function instantiateWebDriver(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver
{
return RemoteWebDriver::create($driverHost, $capabilities);
}

private function getNormalisedBrowserName(): string
{
return self::BROWSER_NAME_ALIAS_MAP[$this->browserName] ?? $this->browserName;
Expand Down
46 changes: 22 additions & 24 deletions tests/Custom/CapabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Mink\WebdriverClassicDriver\Tests\Custom;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverOptions;
use Facebook\WebDriver\WebDriverTimeouts;
use Mink\WebdriverClassicDriver\WebdriverClassicDriver;

class CapabilityTest extends \PHPUnit\Framework\TestCase
Expand All @@ -14,9 +17,26 @@ class CapabilityTest extends \PHPUnit\Framework\TestCase
*/
public function testThatCapabilitiesAreAsExpected(string $browserName, array $desiredCapabilities, array $expectedCapabilities): void
{
$driver = $this->createDriverExposingCapabilities($browserName, $desiredCapabilities);
$mockWebDriver = $this->createMock(RemoteWebDriver::class);
$mockWebDriverOptions = $this->createMock(WebDriverOptions::class);
$mockWebDriverTimeouts = $this->createMock(WebDriverTimeouts::class);
$mockWebDriver->method('manage')->willReturn($mockWebDriverOptions);
$mockWebDriverOptions->method('timeouts')->willReturn($mockWebDriverTimeouts);

$this->assertSame($expectedCapabilities, $driver->capabilities);
$actualCapabilities = null;
$driver = new WebdriverClassicDriver(
$browserName,
$desiredCapabilities,
'example.com',
function ($host, $capabilities) use (&$actualCapabilities, $mockWebDriver) {
$actualCapabilities = $capabilities->toArray();
return $mockWebDriver;
}
);

$driver->start();

$this->assertSame($expectedCapabilities, $actualCapabilities);
}

public static function capabilitiesDataProvider(): iterable
Expand Down Expand Up @@ -78,26 +98,4 @@ public static function capabilitiesDataProvider(): iterable
],
];
}

/**
* @param string $browserName
* @param array<string, mixed> $desiredCapabilities
* @return WebdriverClassicDriver&object{capabilities: array<string, mixed>}
*/
private function createDriverExposingCapabilities(string $browserName, array $desiredCapabilities = []): WebdriverClassicDriver
{
return new class($browserName, $desiredCapabilities) extends WebdriverClassicDriver {
/**
* @var array<string, mixed>
*/
public array $capabilities;

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

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

0 comments on commit d1f395c

Please sign in to comment.