Skip to content

Commit a521f85

Browse files
committed
Capabilities must be provided when resuing session, do not allow them to be null
Null capabilities causes issues and inconsistencies in various places.
1 parent e6a956c commit a521f85

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

lib/Remote/RemoteWebDriver.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,19 @@ class RemoteWebDriver implements WebDriver, JavaScriptExecutor, WebDriverHasInpu
5656
/**
5757
* @param HttpCommandExecutor $commandExecutor
5858
* @param string $sessionId
59-
* @param WebDriverCapabilities|null $capabilities
59+
* @param WebDriverCapabilities $capabilities
6060
* @param bool $isW3cCompliant false to use the legacy JsonWire protocol, true for the W3C WebDriver spec
6161
*/
6262
protected function __construct(
6363
HttpCommandExecutor $commandExecutor,
6464
$sessionId,
65-
WebDriverCapabilities $capabilities = null,
65+
WebDriverCapabilities $capabilities,
6666
$isW3cCompliant = false
6767
) {
6868
$this->executor = $commandExecutor;
6969
$this->sessionID = $sessionId;
7070
$this->isW3cCompliant = $isW3cCompliant;
71-
72-
if ($capabilities !== null) {
73-
$this->capabilities = $capabilities;
74-
}
71+
$this->capabilities = $capabilities;
7572
}
7673

7774
/**
@@ -147,6 +144,8 @@ public static function create(
147144
* @param int|null $connection_timeout_in_ms Set timeout for the connect phase to remote Selenium WebDriver server
148145
* @param int|null $request_timeout_in_ms Set the maximum time of a request to remote Selenium WebDriver server
149146
* @param bool $isW3cCompliant True to use W3C WebDriver (default), false to use the legacy JsonWire protocol
147+
* @param WebDriverCapabilities|null $existingCapabilities Provide capabilities of the existing previously created
148+
* session. If not provided, we will attempt to read them, but this will only work when using Selenium Grid.
150149
* @return static
151150
*/
152151
public static function createBySessionID(
@@ -157,6 +156,7 @@ public static function createBySessionID(
157156
) {
158157
// BC layer to not break the method signature
159158
$isW3cCompliant = func_num_args() > 4 ? func_get_arg(4) : true;
159+
$existingCapabilities = func_num_args() > 5 ? func_get_arg(5) : null;
160160

161161
$executor = new HttpCommandExecutor($selenium_server_url, null, null);
162162
if ($connection_timeout_in_ms !== null) {
@@ -170,7 +170,13 @@ public static function createBySessionID(
170170
$executor->disableW3cCompliance();
171171
}
172172

173-
return new static($executor, $session_id, null, $isW3cCompliant);
173+
if ($existingCapabilities === null) {
174+
throw new UnknownErrorException(
175+
'Existing capabilities must be provided when reusing previous session.'
176+
);
177+
}
178+
179+
return new static($executor, $session_id, $existingCapabilities, $isW3cCompliant);
174180
}
175181

176182
/**

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ parameters:
2323
# Parameter is intentionally not part of signature to not break BC
2424
- message: '#PHPDoc tag \@param references unknown parameter: \$isW3cCompliant#'
2525
path: 'lib/Remote/RemoteWebDriver.php'
26+
- message: '#PHPDoc tag \@param references unknown parameter: \$existingCapabilities#'
27+
path: 'lib/Remote/RemoteWebDriver.php'
2628

2729
inferPrivatePropertyTypeFromConstructor: true

tests/unit/Remote/RemoteWebDriverTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ class RemoteWebDriverTest extends TestCase
2222

2323
protected function setUp(): void
2424
{
25-
$this->driver = RemoteWebDriver::createBySessionID('session-id', 'http://foo.bar:4444');
25+
$this->driver = RemoteWebDriver::createBySessionID(
26+
'session-id',
27+
'http://foo.bar:4444',
28+
null,
29+
null,
30+
true,
31+
new DesiredCapabilities([])
32+
);
2633
}
2734

2835
/**

0 commit comments

Comments
 (0)