3131use Facebook \WebDriver \WebDriverSelect ;
3232use JetBrains \PhpStorm \Language ;
3333
34+ /**
35+ * @phpstan-type TWebDriverInstantiator callable(string $driverHost, DesiredCapabilities $capabilities): RemoteWebDriver
36+ */
3437class WebdriverClassicDriver extends CoreDriver
3538{
3639 public const DEFAULT_BROWSER = WebDriverBrowserType::CHROME ;
@@ -81,19 +84,27 @@ class WebdriverClassicDriver extends CoreDriver
8184
8285 private string $ webDriverHost ;
8386
87+ /**
88+ * @var TWebDriverInstantiator
89+ */
90+ private $ webDriverInstantiator ;
91+
8492 private ?string $ initialWindowHandle = null ;
8593
8694 /**
8795 * @param string $browserName One of 'edge', 'firefox', 'chrome' or any one of {@see WebDriverBrowserType} constants.
96+ * @param TWebDriverInstantiator|null $webDriverInstantiator
8897 */
8998 public function __construct (
9099 string $ browserName = self ::DEFAULT_BROWSER ,
91100 array $ desiredCapabilities = [],
92- string $ webDriverHost = 'http://localhost:4444/wd/hub '
101+ string $ webDriverHost = 'http://localhost:4444/wd/hub ' ,
102+ ?callable $ webDriverInstantiator = null
93103 ) {
94104 $ this ->browserName = $ browserName ;
95105 $ this ->desiredCapabilities = $ this ->initCapabilities ($ desiredCapabilities );
96106 $ this ->webDriverHost = $ webDriverHost ;
107+ $ this ->webDriverInstantiator = $ webDriverInstantiator ?? [self ::class, 'instantiateWebDriver ' ];
97108 }
98109
99110 // <editor-fold desc="Implementation">
@@ -751,7 +762,7 @@ public function setTimeouts(array $timeouts): void
751762
752763 // </editor-fold>
753764
754- // <editor-fold desc="Private Utilities ">
765+ // <editor-fold desc="Extension Points ">
755766
756767 /**
757768 * @throws DriverException
@@ -762,7 +773,7 @@ protected function createWebDriver(): void
762773 throw new DriverException ('Base driver has already been created ' );
763774 }
764775
765- $ this ->webDriver = RemoteWebDriver:: create ($ this ->webDriverHost , $ this ->getDesiredCapabilities () );
776+ $ this ->webDriver = ($ this ->webDriverInstantiator )( $ this -> webDriverHost , $ this ->desiredCapabilities );
766777 }
767778
768779 /**
@@ -777,9 +788,13 @@ protected function getWebDriver(): RemoteWebDriver
777788 throw new DriverException ('Base driver has not been created ' );
778789 }
779790
780- protected function getDesiredCapabilities (): array
791+ // </editor-fold>
792+
793+ // <editor-fold desc="Private Utilities">
794+
795+ private static function instantiateWebDriver (string $ driverHost , DesiredCapabilities $ capabilities ): RemoteWebDriver
781796 {
782- return $ this -> desiredCapabilities -> toArray ( );
797+ return RemoteWebDriver:: create ( $ driverHost , $ capabilities );
783798 }
784799
785800 private function getNormalisedBrowserName (): string
0 commit comments