|
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" |
|
0 commit comments