Skip to content

Commit a3a1b0c

Browse files
committed
getElementVisibility and keypressElement implementations (W3C client)
1 parent c980a7d commit a3a1b0c

2 files changed

Lines changed: 88 additions & 8 deletions

File tree

src/Client/W3CClient.php

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,43 @@ public function getActiveElement(string $sessionIdentifier): PromiseInterface
492492
*/
493493
public function getElementVisibility(string $sessionIdentifier, array $elementIdentifier): PromiseInterface
494494
{
495-
// TODO: Implement getElementVisibility() method.
495+
// todo: safer checks (or hide internals behind a transfer object/contract)
496+
$elementHandle = array_key_first($elementIdentifier);
497+
if (!isset($elementIdentifier[$elementHandle])) {
498+
throw new RuntimeException('Unexpected format for the element identifier.');
499+
}
496500

497-
return reject(new RuntimeException('Not implemented.'));
501+
$requestUri = sprintf(
502+
'http://%s:%d/wd/hub/session/%s/element/%s/displayed',
503+
$this->_options['server']['host'],
504+
$this->_options['server']['port'],
505+
$sessionIdentifier,
506+
$elementIdentifier[$elementHandle]
507+
);
508+
509+
$requestHeaders = [
510+
'Content-Type' => 'application/json; charset=UTF-8',
511+
];
512+
513+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
514+
515+
$visibilityStatusPromise = $responsePromise
516+
->then(
517+
function (ResponseInterface $response) {
518+
$visibilityStatus = $this->deserializeResponse($response);
519+
520+
return $visibilityStatus;
521+
}
522+
)
523+
->then(
524+
null,
525+
function (Throwable $rejectionReason) {
526+
throw new RuntimeException('Unable to get visibility status for the element.', 0, $rejectionReason);
527+
}
528+
)
529+
;
530+
531+
return $visibilityStatusPromise;
498532
}
499533

500534
/**
@@ -515,9 +549,45 @@ public function keypressElement(
515549
array $elementIdentifier,
516550
string $keySequence
517551
): PromiseInterface {
518-
// TODO: Implement keypressElement() method.
552+
// todo: safer checks (or hide internals behind a transfer object/contract)
553+
$elementHandle = array_key_first($elementIdentifier);
554+
if (!is_string($elementHandle)) {
555+
throw new RuntimeException('Unexpected format for the element identifier.');
556+
}
519557

520-
return reject(new RuntimeException('Not implemented.'));
558+
$requestUri = sprintf(
559+
'http://%s:%d/wd/hub/session/%s/element/%s/value',
560+
$this->_options['server']['host'],
561+
$this->_options['server']['port'],
562+
$sessionIdentifier,
563+
$elementIdentifier[$elementHandle]
564+
);
565+
566+
$requestHeaders = [
567+
'Content-Type' => 'application/json; charset=UTF-8',
568+
];
569+
570+
$requestContents = json_encode(['text' => $keySequence]);
571+
572+
$responsePromise = $this->httpClient->post($requestUri, $requestHeaders, $requestContents);
573+
574+
$keypressConfirmationPromise = $responsePromise
575+
->then(
576+
function (ResponseInterface $response) {
577+
$this->onCommandConfirmation($response, 'Keypress command is not confirmed.');
578+
579+
return null;
580+
}
581+
)
582+
->then(
583+
null,
584+
function (Throwable $rejectionReason) {
585+
throw new RuntimeException('Unable to apply keyboard keys to the element.', 0, $rejectionReason);
586+
}
587+
)
588+
;
589+
590+
return $keypressConfirmationPromise;
521591
}
522592

523593
/**

src/SeleniumHubDriver.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,12 @@ public function getActiveElement(string $sessionIdentifier): PromiseInterface
218218
*/
219219
public function getElementVisibility(string $sessionIdentifier, array $elementIdentifier): PromiseInterface
220220
{
221-
// TODO: Implement getElementVisibility() method.
221+
$visibilityStatusPromise = $this->hubClient->getElementVisibility($sessionIdentifier, $elementIdentifier);
222222

223-
return reject(new RuntimeException('Not implemented.'));
223+
return $this->timeoutInterceptor->applyTimeout(
224+
$visibilityStatusPromise,
225+
'Unable to complete a get element visibility command.'
226+
);
224227
}
225228

226229
/**
@@ -241,9 +244,16 @@ public function keypressElement(
241244
array $elementIdentifier,
242245
string $keySequence
243246
): PromiseInterface {
244-
// TODO: Implement keypressElement() method.
247+
$keypressConfirmationPromise = $this->hubClient->keypressElement(
248+
$sessionIdentifier,
249+
$elementIdentifier,
250+
$keySequence
251+
);
245252

246-
return reject(new RuntimeException('Not implemented.'));
253+
return $this->timeoutInterceptor->applyTimeout(
254+
$keypressConfirmationPromise,
255+
'Unable to complete a keypress element command.'
256+
);
247257
}
248258

249259
/**

0 commit comments

Comments
 (0)