Skip to content

Commit 3d47c2c

Browse files
committed
implementation for wait() and getElementIdentifier() methods (W3C webdriver)
1 parent 8b6f31b commit 3d47c2c

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

src/Client/W3CClient.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ function (ResponseInterface $response) {
293293
if ('{"value":null}' !== $responseBody) {
294294
throw new RuntimeException('URI navigation is not confirmed.');
295295
}
296+
297+
return null;
296298
}
297299
)
298300
->then(
@@ -331,9 +333,50 @@ public function getSource(string $sessionIdentifier): PromiseInterface
331333
*/
332334
public function getElementIdentifier(string $sessionIdentifier, string $xpathQuery): PromiseInterface
333335
{
334-
// TODO: Implement getElementIdentifier() method.
336+
$requestUri = sprintf(
337+
'http://%s:%d/wd/hub/session/%s/element',
338+
$this->_options['server']['host'],
339+
$this->_options['server']['port'],
340+
$sessionIdentifier
341+
);
335342

336-
return reject(new RuntimeException('Not implemented.'));
343+
$requestHeaders = [
344+
'Content-Type' => 'application/json; charset=UTF-8',
345+
];
346+
347+
$requestContents = json_encode(['using' => 'xpath', 'value' => $xpathQuery]);
348+
349+
$responsePromise = $this->httpClient->post($requestUri, $requestHeaders, $requestContents);
350+
351+
$elementIdentifierPromise = $responsePromise
352+
->then(
353+
function (ResponseInterface $response) {
354+
$responseBody = (string) $response->getBody();
355+
preg_match(
356+
'/(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',
357+
$responseBody,
358+
$matches
359+
);
360+
361+
if (!isset($matches[1], $matches[2])) {
362+
// todo: locate an error message or set it as "undefined error"
363+
throw new RuntimeException('Unable to locate an element identifier in the response.');
364+
}
365+
366+
$elementIdentifier = [$matches[1] => $matches[2]];
367+
368+
return $elementIdentifier;
369+
}
370+
)
371+
->then(
372+
null,
373+
function (Throwable $rejectionReason) {
374+
throw new RuntimeException('Unable to get an element identifier (request).', 0, $rejectionReason);
375+
}
376+
)
377+
;
378+
379+
return $elementIdentifierPromise;
337380
}
338381

339382
/**

src/SeleniumHubDriver.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface as ConfigurationExceptionInterface;
2323
use Symfony\Component\OptionsResolver\OptionsResolver;
2424
use function React\Promise\reject;
25-
use function React\Promise\resolve;
25+
use function React\Promise\Timer\resolve;
2626

2727
/**
2828
* Sends action requests to the Selenium Grid server (hub) and controls their async execution
@@ -87,9 +87,9 @@ public function __construct(
8787
*/
8888
public function wait(float $time): PromiseInterface
8989
{
90-
// TODO: Implement wait() method.
90+
$idlePromise = resolve($time, $this->loop);
9191

92-
return reject(new RuntimeException('Not implemented.'));
92+
return $idlePromise;
9393
}
9494

9595
/**
@@ -203,9 +203,12 @@ public function getSource(string $sessionIdentifier): PromiseInterface
203203
*/
204204
public function getElementIdentifier(string $sessionIdentifier, string $xpathQuery): PromiseInterface
205205
{
206-
// TODO: Implement getElementIdentifier() method.
206+
$elementIdentifierPromise = $this->hubClient->getElementIdentifier($sessionIdentifier, $xpathQuery);
207207

208-
return reject(new RuntimeException('Not implemented.'));
208+
return $this->timeoutInterceptor->applyTimeout(
209+
$elementIdentifierPromise,
210+
'Unable to complete a get element identifier command.'
211+
);
209212
}
210213

211214
/**

0 commit comments

Comments
 (0)