|
16 | 16 | namespace Itnelo\React\WebDriver\Client; |
17 | 17 |
|
18 | 18 | use Itnelo\React\WebDriver\ClientInterface; |
| 19 | +use Psr\Http\Message\ResponseInterface; |
19 | 20 | use React\Http\Browser; |
| 21 | +use React\Promise\Deferred; |
20 | 22 | use React\Promise\PromiseInterface; |
| 23 | +use RuntimeException; |
21 | 24 | use Symfony\Component\OptionsResolver\Exception\ExceptionInterface as OptionsResolverExceptionInterface; |
22 | 25 | use Symfony\Component\OptionsResolver\OptionsResolver; |
| 26 | +use Throwable; |
23 | 27 |
|
24 | 28 | /** |
25 | 29 | * W3C compliant WebDriver client for Selenium Grid server (hub) that performs asynchronously. |
@@ -129,15 +133,66 @@ function (OptionsResolver $requestOptionsResolver) { |
129 | 133 | */ |
130 | 134 | public function getSessionIdentifiers(): PromiseInterface |
131 | 135 | { |
132 | | - // TODO: Implement getSessionIdentifiers() method. |
| 136 | + // todo |
133 | 137 | } |
134 | 138 |
|
135 | 139 | /** |
136 | 140 | * {@inheritDoc} |
137 | 141 | */ |
138 | 142 | public function createSession(): PromiseInterface |
139 | 143 | { |
140 | | - // TODO: Implement createSession() method. |
| 144 | + $sessionOpeningDeferred = new Deferred(); |
| 145 | + |
| 146 | + $requestUri = sprintf( |
| 147 | + 'http://%s:%d/wd/hub/session', |
| 148 | + $this->_options['server']['host'], |
| 149 | + $this->_options['server']['port'] |
| 150 | + ); |
| 151 | + |
| 152 | + $requestHeaders = [ |
| 153 | + 'Content-Type' => 'application/json; charset=UTF-8', |
| 154 | + ]; |
| 155 | + |
| 156 | + // todo: implement custom executable args / prefs support (omitted in the interface) |
| 157 | + $requestContents = '{"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"prefs":{"intl.accept_languages":"RU-ru,ru,en-US,en"},"args":["--user-data-dir=\/opt\/google\/chrome\/profiles"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","goog:chromeOptions":{"prefs":{"intl.accept_languages":"RU-ru,ru,en-US,en"},"args":["--user-data-dir=\/opt\/google\/chrome\/profiles"]}}}'; |
| 158 | + |
| 159 | + $responsePromise = $this->httpClient->post($requestUri, $requestHeaders, $requestContents); |
| 160 | + |
| 161 | + $responsePromise->then( |
| 162 | + function (ResponseInterface $response) use ($sessionOpeningDeferred) { |
| 163 | + try { |
| 164 | + $responseBody = (string) $response->getBody(); |
| 165 | + preg_match('/sessionid[":\s]+([a-z\d]{32})/Ui', $responseBody, $matches); |
| 166 | + |
| 167 | + if (!isset($matches[1])) { |
| 168 | + // todo: locate an error message or set it as "undefined error". |
| 169 | + throw new RuntimeException('Unable to locate session identifier in the response.'); |
| 170 | + } |
| 171 | + |
| 172 | + $sessionIdentifier = $matches[1]; |
| 173 | + $sessionOpeningDeferred->resolve($sessionIdentifier); |
| 174 | + } catch (Throwable $exception) { |
| 175 | + $reason = new RuntimeException( |
| 176 | + 'Unable to open a selenium hub session (response deserialization).', |
| 177 | + 0, |
| 178 | + $exception |
| 179 | + ); |
| 180 | + |
| 181 | + $sessionOpeningDeferred->reject($reason); |
| 182 | + } |
| 183 | + }, |
| 184 | + function (Throwable $rejectionReason) use ($sessionOpeningDeferred) { |
| 185 | + $reason = new RuntimeException('Unable to open a selenium hub session (request).', 0, $rejectionReason); |
| 186 | + |
| 187 | + $sessionOpeningDeferred->reject($reason); |
| 188 | + } |
| 189 | + ); |
| 190 | + |
| 191 | + $sessionIdentifierPromise = $sessionOpeningDeferred->promise(); |
| 192 | + |
| 193 | + // todo: apply timeout |
| 194 | + |
| 195 | + return $sessionIdentifierPromise; |
141 | 196 | } |
142 | 197 |
|
143 | 198 | /** |
|
0 commit comments