|
39 | 39 | */
|
40 | 40 | class W3CClient implements ClientInterface
|
41 | 41 | {
|
| 42 | + /** |
| 43 | + * A pattern to recognize session identifier in the WebDriver response |
| 44 | + * |
| 45 | + * @var string |
| 46 | + */ |
| 47 | + private const PATTERN_SESSION_IDENTIFIER = '/[":\s]+([a-z\d]{32})/Ui'; |
| 48 | + |
42 | 49 | /**
|
43 | 50 | * Sends commands to the Selenium Grid endpoint using W3C protocol over HTTP
|
44 | 51 | *
|
@@ -116,9 +123,44 @@ function (OptionsResolver $serverOptionsResolver) {
|
116 | 123 | */
|
117 | 124 | public function getSessionIdentifiers(): PromiseInterface
|
118 | 125 | {
|
119 |
| - // todo: implementation |
| 126 | + $requestUri = sprintf( |
| 127 | + 'http://%s:%d/wd/hub/sessions', |
| 128 | + $this->_options['server']['host'], |
| 129 | + $this->_options['server']['port'] |
| 130 | + ); |
120 | 131 |
|
121 |
| - return reject(new RuntimeException('Not implemented.')); |
| 132 | + $requestHeaders = [ |
| 133 | + 'Content-Type' => 'application/json; charset=UTF-8', |
| 134 | + ]; |
| 135 | + |
| 136 | + $responsePromise = $this->httpClient->get($requestUri, $requestHeaders); |
| 137 | + |
| 138 | + $identifierListPromise = $responsePromise |
| 139 | + ->then( |
| 140 | + function (ResponseInterface $response) { |
| 141 | + $dataArray = $this->deserializeResponse($response); |
| 142 | + $payload = $dataArray['message'] ?? ''; |
| 143 | + |
| 144 | + preg_match_all(self::PATTERN_SESSION_IDENTIFIER, $payload, $matches); |
| 145 | + |
| 146 | + if (!isset($matches[1]) || 1 > count($matches[1])) { |
| 147 | + throw new RuntimeException('Unable to locate a list of session identifiers in the response.'); |
| 148 | + } |
| 149 | + |
| 150 | + $identifierList = $matches[1]; |
| 151 | + |
| 152 | + return $identifierList; |
| 153 | + } |
| 154 | + ) |
| 155 | + ->then( |
| 156 | + null, |
| 157 | + function (Throwable $rejectionReason) { |
| 158 | + throw new RuntimeException('Unable to get a list of session identifiers.', 0, $rejectionReason); |
| 159 | + } |
| 160 | + ) |
| 161 | + ; |
| 162 | + |
| 163 | + return $identifierListPromise; |
122 | 164 | }
|
123 | 165 |
|
124 | 166 | /**
|
@@ -148,7 +190,7 @@ public function createSession(): PromiseInterface
|
148 | 190 | function (ResponseInterface $response) use ($sessionOpeningDeferred) {
|
149 | 191 | try {
|
150 | 192 | $responseBody = (string) $response->getBody();
|
151 |
| - preg_match('/sessionid[":\s]+([a-z\d]{32})/Ui', $responseBody, $matches); |
| 193 | + preg_match(self::PATTERN_SESSION_IDENTIFIER, $responseBody, $matches); |
152 | 194 |
|
153 | 195 | if (!isset($matches[1])) {
|
154 | 196 | // todo: locate an error message or set it as "undefined error"
|
@@ -235,6 +277,10 @@ function (ResponseInterface $response) use ($tabLookupDeferred) {
|
235 | 277 | try {
|
236 | 278 | $tabIdentifiers = $this->deserializeResponse($response);
|
237 | 279 |
|
| 280 | + if (!is_array($tabIdentifiers)) { |
| 281 | + throw new RuntimeException('Unable to locate tab identifiers in the response.'); |
| 282 | + } |
| 283 | + |
238 | 284 | $tabLookupDeferred->resolve($tabIdentifiers);
|
239 | 285 | } catch (Throwable $exception) {
|
240 | 286 | $reason = new RuntimeException(
|
@@ -281,6 +327,10 @@ public function getActiveTabIdentifier(string $sessionIdentifier): PromiseInterf
|
281 | 327 | function (ResponseInterface $response) {
|
282 | 328 | $tabIdentifier = $this->deserializeResponse($response);
|
283 | 329 |
|
| 330 | + if (!is_string($tabIdentifier)) { |
| 331 | + throw new RuntimeException('Unable to locate a tab identifier in the response.'); |
| 332 | + } |
| 333 | + |
284 | 334 | return $tabIdentifier;
|
285 | 335 | }
|
286 | 336 | )
|
@@ -396,6 +446,10 @@ public function getCurrentUri(string $sessionIdentifier): PromiseInterface
|
396 | 446 | function (ResponseInterface $response) {
|
397 | 447 | $uriCurrent = $this->deserializeResponse($response);
|
398 | 448 |
|
| 449 | + if (!is_string($uriCurrent)) { |
| 450 | + throw new RuntimeException('Unable to locate an URI in the response.'); |
| 451 | + } |
| 452 | + |
399 | 453 | return $uriCurrent;
|
400 | 454 | }
|
401 | 455 | )
|
@@ -433,6 +487,10 @@ public function getSource(string $sessionIdentifier): PromiseInterface
|
433 | 487 | function (ResponseInterface $response) {
|
434 | 488 | $sourceCode = $this->deserializeResponse($response);
|
435 | 489 |
|
| 490 | + if (!is_string($sourceCode)) { |
| 491 | + throw new RuntimeException('Unable to locate source code in the response.'); |
| 492 | + } |
| 493 | + |
436 | 494 | return $sourceCode;
|
437 | 495 | }
|
438 | 496 | )
|
@@ -557,6 +615,10 @@ public function getElementVisibility(string $sessionIdentifier, array $elementId
|
557 | 615 | function (ResponseInterface $response) {
|
558 | 616 | $visibilityStatus = $this->deserializeResponse($response);
|
559 | 617 |
|
| 618 | + if (!is_bool($visibilityStatus)) { |
| 619 | + throw new RuntimeException('Unable to locate a visibility status in the response.'); |
| 620 | + } |
| 621 | + |
560 | 622 | return $visibilityStatus;
|
561 | 623 | }
|
562 | 624 | )
|
@@ -736,7 +798,12 @@ public function getScreenshot(string $sessionIdentifier): PromiseInterface
|
736 | 798 | ->then(
|
737 | 799 | function (ResponseInterface $response) {
|
738 | 800 | $imageContentsEncoded = $this->deserializeResponse($response);
|
739 |
| - $imageContents = base64_decode($imageContentsEncoded); |
| 801 | + |
| 802 | + if (!is_string($imageContentsEncoded)) { |
| 803 | + throw new RuntimeException('Unable to locate screenshot contents in the response.'); |
| 804 | + } |
| 805 | + |
| 806 | + $imageContents = base64_decode($imageContentsEncoded); |
740 | 807 |
|
741 | 808 | return $imageContents;
|
742 | 809 | }
|
|
0 commit comments