Skip to content

Commit 6d23850

Browse files
committed
getActiveTabIdentifier and getCurrentUri methods implementation for W3C client
1 parent 08662cf commit 6d23850

3 files changed

Lines changed: 73 additions & 13 deletions

File tree

src/Client/W3CClient.php

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ function (ResponseInterface $response) use ($tabLookupDeferred) {
217217
$tabLookupDeferred->resolve($tabIdentifiers);
218218
} catch (Throwable $exception) {
219219
$reason = new RuntimeException(
220-
'Unable to open a selenium hub session (response deserialization).',
220+
'Unable to get tab identifiers (response deserialization).',
221221
0,
222222
$exception
223223
);
@@ -226,25 +226,52 @@ function (ResponseInterface $response) use ($tabLookupDeferred) {
226226
}
227227
},
228228
function (Throwable $rejectionReason) use ($tabLookupDeferred) {
229-
$reason = new RuntimeException('Unable to open a selenium hub session (request).', 0, $rejectionReason);
229+
$reason = new RuntimeException('Unable to get tab identifiers (request).', 0, $rejectionReason);
230230

231231
$tabLookupDeferred->reject($reason);
232232
}
233233
);
234234

235-
$tabIdentifierListPromise = $tabLookupDeferred->promise();
235+
$identifierListPromise = $tabLookupDeferred->promise();
236236

237-
return $tabIdentifierListPromise;
237+
return $identifierListPromise;
238238
}
239239

240240
/**
241241
* {@inheritDoc}
242242
*/
243243
public function getActiveTabIdentifier(string $sessionIdentifier): PromiseInterface
244244
{
245-
// TODO: Implement getActiveTabIdentifier() method.
245+
$requestUri = sprintf(
246+
'http://%s:%d/wd/hub/session/%s/window',
247+
$this->_options['server']['host'],
248+
$this->_options['server']['port'],
249+
$sessionIdentifier
250+
);
246251

247-
return reject(new RuntimeException('Not implemented.'));
252+
$requestHeaders = [
253+
'Content-Type' => 'application/json; charset=UTF-8',
254+
];
255+
256+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
257+
258+
$tabIdentifierPromise = $responsePromise
259+
->then(
260+
function (ResponseInterface $response) {
261+
$tabIdentifier = $this->deserializeResponse($response);
262+
263+
return $tabIdentifier;
264+
}
265+
)
266+
->then(
267+
null,
268+
function (Throwable $rejectionReason) {
269+
throw new RuntimeException('Unable to get an identifier for the active tab.', 0, $rejectionReason);
270+
}
271+
)
272+
;
273+
274+
return $tabIdentifierPromise;
248275
}
249276

250277
/**
@@ -301,9 +328,36 @@ function (Throwable $rejectionReason) {
301328
*/
302329
public function getCurrentUri(string $sessionIdentifier): PromiseInterface
303330
{
304-
// TODO: Implement getUri() method.
331+
$requestUri = sprintf(
332+
'http://%s:%d/wd/hub/session/%s/url',
333+
$this->_options['server']['host'],
334+
$this->_options['server']['port'],
335+
$sessionIdentifier
336+
);
305337

306-
return reject(new RuntimeException('Not implemented.'));
338+
$requestHeaders = [
339+
'Content-Type' => 'application/json; charset=UTF-8',
340+
];
341+
342+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
343+
344+
$currentUriPromise = $responsePromise
345+
->then(
346+
function (ResponseInterface $response) {
347+
$uriCurrent = $this->deserializeResponse($response);
348+
349+
return $uriCurrent;
350+
}
351+
)
352+
->then(
353+
null,
354+
function (Throwable $rejectionReason) {
355+
throw new RuntimeException('Unable to get a current URI.', 0, $rejectionReason);
356+
}
357+
)
358+
;
359+
360+
return $currentUriPromise;
307361
}
308362

309363
/**

src/ClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function setActiveTab(string $sessionIdentifier, string $tabIdentifier):
110110

111111
/**
112112
* Returns a promise that will be resolved when the remote WebDriver service confirms URI navigation within
113-
* currently active (focused) tab ({@link getActiveTabIdentifier()}).
113+
* currently active (focused) tab ({@link getActiveTabIdentifier()})
114114
*
115115
* @param string $sessionIdentifier Session identifier for Selenium Grid server (hub)
116116
* @param string $uri Website URL or any other resource identifier, to open in the active (focused)

src/SeleniumHubDriver.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,12 @@ public function getTabIdentifiers(string $sessionIdentifier): PromiseInterface
136136
*/
137137
public function getActiveTabIdentifier(string $sessionIdentifier): PromiseInterface
138138
{
139-
// TODO: Implement getActiveTabIdentifier() method.
139+
$tabIdentifierPromise = $this->hubClient->getActiveTabIdentifier($sessionIdentifier);
140140

141-
return reject(new RuntimeException('Not implemented.'));
141+
return $this->timeoutInterceptor->applyTimeout(
142+
$tabIdentifierPromise,
143+
'Unable to complete a get active tab command.'
144+
);
142145
}
143146

144147
/**
@@ -166,9 +169,12 @@ public function openUri(string $sessionIdentifier, string $uri): PromiseInterfac
166169
*/
167170
public function getCurrentUri(string $sessionIdentifier): PromiseInterface
168171
{
169-
// TODO: Implement getCurrentUri() method.
172+
$currentUriPromise = $this->hubClient->getCurrentUri($sessionIdentifier);
170173

171-
return reject(new RuntimeException('Not implemented.'));
174+
return $this->timeoutInterceptor->applyTimeout(
175+
$currentUriPromise,
176+
'Unable to complete a get current uri command.'
177+
);
172178
}
173179

174180
/**

0 commit comments

Comments
 (0)