Skip to content

Commit c9c994d

Browse files
committed
getSessionIdentifiers method implementation (W3C client)
1 parent 1bc9ccc commit c9c994d

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/Client/W3CClient.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
*/
4040
class W3CClient implements ClientInterface
4141
{
42+
/**
43+
* A pattern to recognize session identifier in the WebDriver response
44+
*
45+
* @var string
46+
*/
47+
private const PATTERN_SESSION_IDENTIFIER = '/[":\s]+([a-z\d]{32})/Ui';
48+
4249
/**
4350
* Sends commands to the Selenium Grid endpoint using W3C protocol over HTTP
4451
*
@@ -116,9 +123,44 @@ function (OptionsResolver $serverOptionsResolver) {
116123
*/
117124
public function getSessionIdentifiers(): PromiseInterface
118125
{
119-
// todo: implementation
126+
$requestUri = sprintf(
127+
'http://%s:%d/wd/hub/sessions',
128+
$this->_options['server']['host'],
129+
$this->_options['server']['port']
130+
);
120131

121-
return reject(new RuntimeException('Not implemented.'));
132+
$requestHeaders = [
133+
'Content-Type' => 'application/json; charset=UTF-8',
134+
];
135+
136+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
137+
138+
$identifierListPromise = $responsePromise
139+
->then(
140+
function (ResponseInterface $response) {
141+
$dataArray = $this->deserializeResponse($response);
142+
$payload = $dataArray['message'] ?? '';
143+
144+
preg_match_all(self::PATTERN_SESSION_IDENTIFIER, $payload, $matches);
145+
146+
if (!isset($matches[1]) || 1 > count($matches[1])) {
147+
throw new RuntimeException('Unable to locate a list of session identifiers in the response.');
148+
}
149+
150+
$identifierList = $matches[1];
151+
152+
return $identifierList;
153+
}
154+
)
155+
->then(
156+
null,
157+
function (Throwable $rejectionReason) {
158+
throw new RuntimeException('Unable to get a list of session identifiers.', 0, $rejectionReason);
159+
}
160+
)
161+
;
162+
163+
return $identifierListPromise;
122164
}
123165

124166
/**
@@ -148,7 +190,7 @@ public function createSession(): PromiseInterface
148190
function (ResponseInterface $response) use ($sessionOpeningDeferred) {
149191
try {
150192
$responseBody = (string) $response->getBody();
151-
preg_match('/sessionid[":\s]+([a-z\d]{32})/Ui', $responseBody, $matches);
193+
preg_match(self::PATTERN_SESSION_IDENTIFIER, $responseBody, $matches);
152194

153195
if (!isset($matches[1])) {
154196
// todo: locate an error message or set it as "undefined error"

src/SeleniumHubDriver.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ public function createSession(): PromiseInterface
104104
*/
105105
public function getSessionIdentifiers(): PromiseInterface
106106
{
107-
// todo: implementation
107+
$identifierListPromise = $this->hubClient->getSessionIdentifiers();
108108

109-
return reject(new RuntimeException('Not implemented.'));
109+
return $this->timeoutInterceptor->applyTimeout(
110+
$identifierListPromise,
111+
'Unable to complete a get session identifiers command.'
112+
);
110113
}
111114

112115
/**
@@ -127,10 +130,10 @@ public function removeSession(string $sessionIdentifier): PromiseInterface
127130
*/
128131
public function getTabIdentifiers(string $sessionIdentifier): PromiseInterface
129132
{
130-
$sessionIdentifierPromise = $this->hubClient->getTabIdentifiers($sessionIdentifier);
133+
$tabIdentifierListPromise = $this->hubClient->getTabIdentifiers($sessionIdentifier);
131134

132135
return $this->timeoutInterceptor->applyTimeout(
133-
$sessionIdentifierPromise,
136+
$tabIdentifierListPromise,
134137
'Unable to complete a tab lookup command.'
135138
);
136139
}

0 commit comments

Comments
 (0)