Skip to content

Commit 217af76

Browse files
committed
extracting an active element identifier (getActiveElementIdentifier, W3C client)
1 parent a3a1b0c commit 217af76

3 files changed

Lines changed: 70 additions & 21 deletions

File tree

src/Client/W3CClient.php

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -449,19 +449,7 @@ public function getElementIdentifier(string $sessionIdentifier, string $xpathQue
449449
$elementIdentifierPromise = $responsePromise
450450
->then(
451451
function (ResponseInterface $response) {
452-
$responseBody = (string) $response->getBody();
453-
preg_match(
454-
'/(element(?:-[a-z\d]{4}){4}[a-z\d]{8})[":\s]+([a-z\d]{8}(?:-[a-z\d]{4}){4}[a-z\d]{8})/Ui',
455-
$responseBody,
456-
$matches
457-
);
458-
459-
if (!isset($matches[1], $matches[2])) {
460-
// todo: locate an error message or set it as "undefined error"
461-
throw new RuntimeException('Unable to locate an element identifier in the response.');
462-
}
463-
464-
$elementIdentifier = [$matches[1] => $matches[2]];
452+
$elementIdentifier = $this->extractElementIdentifier($response);
465453

466454
return $elementIdentifier;
467455
}
@@ -480,11 +468,42 @@ function (Throwable $rejectionReason) {
480468
/**
481469
* {@inheritDoc}
482470
*/
483-
public function getActiveElement(string $sessionIdentifier): PromiseInterface
471+
public function getActiveElementIdentifier(string $sessionIdentifier): PromiseInterface
484472
{
485-
// TODO: Implement getActiveElement() method.
473+
$requestUri = sprintf(
474+
'http://%s:%d/wd/hub/session/%s/element/active',
475+
$this->_options['server']['host'],
476+
$this->_options['server']['port'],
477+
$sessionIdentifier
478+
);
486479

487-
return reject(new RuntimeException('Not implemented.'));
480+
$requestHeaders = [
481+
'Content-Type' => 'application/json; charset=UTF-8',
482+
];
483+
484+
$responsePromise = $this->httpClient->get($requestUri, $requestHeaders);
485+
486+
$elementIdentifierPromise = $responsePromise
487+
->then(
488+
function (ResponseInterface $response) {
489+
$elementIdentifier = $this->extractElementIdentifier($response);
490+
491+
return $elementIdentifier;
492+
}
493+
)
494+
->then(
495+
null,
496+
function (Throwable $rejectionReason) {
497+
throw new RuntimeException(
498+
'Unable to get an identifier of the active element.',
499+
0,
500+
$rejectionReason
501+
);
502+
}
503+
)
504+
;
505+
506+
return $elementIdentifierPromise;
488507
}
489508

490509
/**
@@ -494,7 +513,7 @@ public function getElementVisibility(string $sessionIdentifier, array $elementId
494513
{
495514
// todo: safer checks (or hide internals behind a transfer object/contract)
496515
$elementHandle = array_key_first($elementIdentifier);
497-
if (!isset($elementIdentifier[$elementHandle])) {
516+
if (!is_string($elementHandle)) {
498517
throw new RuntimeException('Unexpected format for the element identifier.');
499518
}
500519

@@ -796,4 +815,31 @@ private function deserializeResponse(ResponseInterface $response)
796815

797816
return $bodyDeserialized['value'];
798817
}
818+
819+
/**
820+
* Returns an element identifier, which has to be extracted from the response message (a surgical approach)
821+
*
822+
* @param ResponseInterface $response PSR-7 response message from the Selenium hub with action results
823+
*
824+
* @return array
825+
*/
826+
private function extractElementIdentifier(ResponseInterface $response): array
827+
{
828+
$responseBody = (string) $response->getBody();
829+
830+
preg_match(
831+
'/(element(?:-[a-z\d]{4}){4}[a-z\d]{8})[":\s]+([a-z\d]{8}(?:-[a-z\d]{4}){4}[a-z\d]{8})/Ui',
832+
$responseBody,
833+
$matches
834+
);
835+
836+
if (!isset($matches[1], $matches[2])) {
837+
// todo: locate an error message or set it as "undefined error"
838+
throw new RuntimeException('Unable to locate element identifier parts in the response.');
839+
}
840+
841+
$elementIdentifier = [$matches[1] => $matches[2]];
842+
843+
return $elementIdentifier;
844+
}
799845
}

src/ClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function getElementIdentifier(string $sessionIdentifier, string $xpathQue
174174
*
175175
* @return PromiseInterface<array>
176176
*/
177-
public function getActiveElement(string $sessionIdentifier): PromiseInterface;
177+
public function getActiveElementIdentifier(string $sessionIdentifier): PromiseInterface;
178178

179179
/**
180180
* Returns a promise that resolves to a boolean, representing element visibility status on the currently active

src/SeleniumHubDriver.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,14 @@ public function getElementIdentifier(string $sessionIdentifier, string $xpathQue
206206
/**
207207
* {@inheritDoc}
208208
*/
209-
public function getActiveElement(string $sessionIdentifier): PromiseInterface
209+
public function getActiveElementIdentifier(string $sessionIdentifier): PromiseInterface
210210
{
211-
// TODO: Implement getActiveElement() method.
211+
$elementIdentifierPromise = $this->hubClient->getActiveElementIdentifier($sessionIdentifier);
212212

213-
return reject(new RuntimeException('Not implemented.'));
213+
return $this->timeoutInterceptor->applyTimeout(
214+
$elementIdentifierPromise,
215+
'Unable to complete a get active element identifier command.'
216+
);
214217
}
215218

216219
/**

0 commit comments

Comments
 (0)