Skip to content

Commit 513ab20

Browse files
committed
getTabIdentifiers method implementation for W3C client
1 parent e973620 commit 513ab20

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ $socketConnector = new SocketConnector(
7272
],
7373
);
7474
$browser = new Browser($loop, $socketConnector);
75+
$browser = $browser->withRejectErrorResponse(false);
7576

7677
$hubClient = new W3CClient(
7778
$browser,

src/Client/W3CClient.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class W3CClient implements ClientInterface
6262
*
6363
* ```
6464
* $loop = \React\EventLoop\Factory::create();
65+
*
6566
* $browser = new \React\Http\Browser($loop);
67+
* $browser = $browser->withRejectErrorResponse(false);
6668
*
6769
* $hubClient = new \Itnelo\React\WebDriver\Client\W3CClient(
6870
* $browser,
@@ -149,8 +151,8 @@ function (ResponseInterface $response) use ($sessionOpeningDeferred) {
149151
preg_match('/sessionid[":\s]+([a-z\d]{32})/Ui', $responseBody, $matches);
150152

151153
if (!isset($matches[1])) {
152-
// todo: locate an error message or set it as "undefined error".
153-
throw new RuntimeException('Unable to locate session identifier in the response.');
154+
// todo: locate an error message or set it as "undefined error"
155+
throw new RuntimeException('Unable to locate a session identifier in the response.');
154156
}
155157

156158
$sessionIdentifier = $matches[1];
@@ -192,9 +194,54 @@ public function removeSession(string $sessionIdentifier): PromiseInterface
192194
*/
193195
public function getTabIdentifiers(string $sessionIdentifier): PromiseInterface
194196
{
195-
// TODO: Implement getTabIdentifiers() method.
197+
$tabLookupDeferred = new Deferred();
196198

197-
return reject(new RuntimeException('Not implemented.'));
199+
$requestUri = sprintf(
200+
'http://%s:%d/wd/hub/session/%s/window/handles',
201+
$this->_options['server']['host'],
202+
$this->_options['server']['port'],
203+
$sessionIdentifier
204+
);
205+
206+
$requestHeaders = [
207+
'Content-Type' => 'application/json; charset=UTF-8',
208+
];
209+
210+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
211+
212+
$responsePromise->then(
213+
function (ResponseInterface $response) use ($tabLookupDeferred) {
214+
try {
215+
$responseBody = (string) $response->getBody();
216+
$bodyDeserialized = json_decode($responseBody, true);
217+
218+
if (!array_key_exists('value', $bodyDeserialized) || !is_array($bodyDeserialized['value'])) {
219+
// todo: locate an error message or set it as "undefined error"
220+
throw new RuntimeException('Unable to locate tab identifiers in the response.');
221+
}
222+
223+
$tabIdentifiers = $bodyDeserialized['value'];
224+
$tabLookupDeferred->resolve($tabIdentifiers);
225+
} catch (Throwable $exception) {
226+
$reason = new RuntimeException(
227+
'Unable to open a selenium hub session (response deserialization).',
228+
0,
229+
$exception
230+
);
231+
232+
$tabLookupDeferred->reject($reason);
233+
}
234+
},
235+
function (Throwable $rejectionReason) use ($tabLookupDeferred) {
236+
$reason = new RuntimeException('Unable to open a selenium hub session (request).', 0, $rejectionReason);
237+
238+
$tabLookupDeferred->reject($reason);
239+
}
240+
);
241+
242+
$tabIdentifierListPromise = $tabLookupDeferred->promise();
243+
244+
return $tabIdentifierListPromise;
198245
}
199246

200247
/**

src/SeleniumHubDriver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,12 @@ public function removeSession(string $sessionIdentifier): PromiseInterface
139139
*/
140140
public function getTabIdentifiers(string $sessionIdentifier): PromiseInterface
141141
{
142-
// TODO: Implement getTabIdentifiers() method.
142+
$sessionIdentifierPromise = $this->hubClient->getTabIdentifiers($sessionIdentifier);
143143

144-
return reject(new RuntimeException('Not implemented.'));
144+
return $this->timeoutInterceptor->applyTimeout(
145+
$sessionIdentifierPromise,
146+
'Unable to complete a tab lookup command.'
147+
);
145148
}
146149

147150
/**

src/WebDriverFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ function (OptionsResolver $commandOptionsResolver) {
120120
$socketConnector = new SocketConnector($loop, $optionsResolved['browser']);
121121
$httpClient = new Browser($loop, $socketConnector);
122122

123+
// Selenium hub sends some valid responses with 5xx status codes, so we need to disable eager promise rejection
124+
// to properly parse error message and other details from the body.
125+
$httpClient = $httpClient->withRejectErrorResponse(false);
126+
123127
$hubClient = new W3CClient($httpClient, ['server' => $optionsResolved['hub']]);
124128
$timeoutInterceptor = new TimeoutInterceptor($loop, $optionsResolved['command']['timeout']);
125129

0 commit comments

Comments
 (0)