Skip to content

Commit c980a7d

Browse files
committed
setActiveTab and getSource methods implementation (W3C client)
1 parent 6d23850 commit c980a7d

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

src/Client/W3CClient.php

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,38 @@ function (Throwable $rejectionReason) {
279279
*/
280280
public function setActiveTab(string $sessionIdentifier, string $tabIdentifier): PromiseInterface
281281
{
282-
// TODO: Implement setActiveTab() method.
282+
$requestUri = sprintf(
283+
'http://%s:%d/wd/hub/session/%s/window',
284+
$this->_options['server']['host'],
285+
$this->_options['server']['port'],
286+
$sessionIdentifier
287+
);
283288

284-
return reject(new RuntimeException('Not implemented.'));
289+
$requestHeaders = [
290+
'Content-Type' => 'application/json; charset=UTF-8',
291+
];
292+
293+
$requestContents = json_encode(['handle' => $tabIdentifier]);
294+
295+
$responsePromise = $this->httpClient->post($requestUri, $requestHeaders, $requestContents);
296+
297+
$switchConfirmationPromise = $responsePromise
298+
->then(
299+
function (ResponseInterface $response) {
300+
$this->onCommandConfirmation($response, 'Tab switch command is not confirmed.');
301+
302+
return null;
303+
}
304+
)
305+
->then(
306+
null,
307+
function (Throwable $rejectionReason) {
308+
throw new RuntimeException('Unable to focus a tab.', 0, $rejectionReason);
309+
}
310+
)
311+
;
312+
313+
return $switchConfirmationPromise;
285314
}
286315

287316
/**
@@ -365,9 +394,36 @@ function (Throwable $rejectionReason) {
365394
*/
366395
public function getSource(string $sessionIdentifier): PromiseInterface
367396
{
368-
// TODO: Implement getSource() method.
397+
$requestUri = sprintf(
398+
'http://%s:%d/wd/hub/session/%s/source',
399+
$this->_options['server']['host'],
400+
$this->_options['server']['port'],
401+
$sessionIdentifier
402+
);
369403

370-
return reject(new RuntimeException('Not implemented.'));
404+
$requestHeaders = [
405+
'Content-Type' => 'application/json; charset=UTF-8',
406+
];
407+
408+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
409+
410+
$sourceCodePromise = $responsePromise
411+
->then(
412+
function (ResponseInterface $response) {
413+
$sourceCode = $this->deserializeResponse($response);
414+
415+
return $sourceCode;
416+
}
417+
)
418+
->then(
419+
null,
420+
function (Throwable $rejectionReason) {
421+
throw new RuntimeException('Unable to get source code of the web resource.', 0, $rejectionReason);
422+
}
423+
)
424+
;
425+
426+
return $sourceCodePromise;
371427
}
372428

373429
/**

src/SeleniumHubDriver.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ public function getActiveTabIdentifier(string $sessionIdentifier): PromiseInterf
149149
*/
150150
public function setActiveTab(string $sessionIdentifier, string $tabIdentifier): PromiseInterface
151151
{
152-
// TODO: Implement setActiveTab() method.
152+
$switchConfirmationPromise = $this->hubClient->setActiveTab($sessionIdentifier, $tabIdentifier);
153153

154-
return reject(new RuntimeException('Not implemented.'));
154+
return $this->timeoutInterceptor->applyTimeout(
155+
$switchConfirmationPromise,
156+
'Unable to complete a set active tab command.'
157+
);
155158
}
156159

157160
/**
@@ -182,9 +185,9 @@ public function getCurrentUri(string $sessionIdentifier): PromiseInterface
182185
*/
183186
public function getSource(string $sessionIdentifier): PromiseInterface
184187
{
185-
// TODO: Implement getSource() method.
188+
$sourceCodePromise = $this->hubClient->getSource($sessionIdentifier);
186189

187-
return reject(new RuntimeException('Not implemented.'));
190+
return $this->timeoutInterceptor->applyTimeout($sourceCodePromise, 'Unable to complete a get source command.');
188191
}
189192

190193
/**

0 commit comments

Comments
 (0)