|
16 | 16 | namespace Itnelo\React\WebDriver\Client; |
17 | 17 |
|
18 | 18 | use Itnelo\React\WebDriver\ClientInterface; |
| 19 | +use React\Http\Browser; |
19 | 20 | use React\Promise\PromiseInterface; |
| 21 | +use Symfony\Component\OptionsResolver\Exception\ExceptionInterface as OptionsResolverExceptionInterface; |
| 22 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
20 | 23 |
|
21 | 24 | /** |
22 | 25 | * W3C compliant WebDriver client for Selenium Grid server (hub) that performs asynchronously. |
|
31 | 34 | */ |
32 | 35 | class W3CClient implements ClientInterface |
33 | 36 | { |
| 37 | + /** |
| 38 | + * Sends commands to the Selenium Grid endpoint using W3C protocol over HTTP |
| 39 | + * |
| 40 | + * @var Browser |
| 41 | + * |
| 42 | + * @see https://www.w3.org/TR/webdriver |
| 43 | + */ |
| 44 | + private Browser $httpClient; |
| 45 | + |
| 46 | + /** |
| 47 | + * Array of options for the client |
| 48 | + * |
| 49 | + * @var array |
| 50 | + */ |
| 51 | + private array $_options; |
| 52 | + |
| 53 | + /** |
| 54 | + * W3CClient constructor. |
| 55 | + * |
| 56 | + * Usage example: |
| 57 | + * |
| 58 | + * ``` |
| 59 | + * $loop = \React\EventLoop\Factory::create(); |
| 60 | + * $browser = new \React\Http\Browser($loop); |
| 61 | + * |
| 62 | + * $webdriver = new \Itnelo\React\WebDriver\Client\W3CClient( |
| 63 | + * $browser, |
| 64 | + * [ |
| 65 | + * 'server' => [ |
| 66 | + * 'host' => 'selenium-hub', |
| 67 | + * 'port' => 4444, |
| 68 | + * ], |
| 69 | + * 'request' => [ |
| 70 | + * 'timeout' => 30, |
| 71 | + * ], |
| 72 | + * ] |
| 73 | + * ); |
| 74 | + * ``` |
| 75 | + * |
| 76 | + * The "request.timeout" option here doesn't correlate with ReactPHP Browser's timeouts and will just cancel a |
| 77 | + * pending promise after the specified time (in seconds); an HTTP request itself, which is handled by the ReactPHP |
| 78 | + * Browser, may (or may not) be completed. Furthermore, the client can reject promise with a runtime exception if |
| 79 | + * underlying browser has decided to stop waiting for the response by its own timeout settings. |
| 80 | + * |
| 81 | + * @param Browser $httpClient Sends commands to the Selenium Grid endpoint using W3C protocol over HTTP |
| 82 | + * @param array $options Array of options for the client |
| 83 | + * |
| 84 | + * @throws OptionsResolverExceptionInterface Whenever an error has been occurred during client configuration |
| 85 | + */ |
| 86 | + public function __construct(Browser $httpClient, array $options = []) |
| 87 | + { |
| 88 | + $this->httpClient = $httpClient; |
| 89 | + |
| 90 | + $optionsResolver = new OptionsResolver(); |
| 91 | + |
| 92 | + $optionsResolver |
| 93 | + ->define('server') |
| 94 | + ->default( |
| 95 | + function (OptionsResolver $serverOptionsResolver) { |
| 96 | + $serverOptionsResolver |
| 97 | + ->define('host') |
| 98 | + ->allowedTypes('string') |
| 99 | + ->default('127.0.0.1') |
| 100 | + ; |
| 101 | + |
| 102 | + $serverOptionsResolver |
| 103 | + ->define('port') |
| 104 | + ->allowedTypes('int') |
| 105 | + ->default(4444) |
| 106 | + ; |
| 107 | + } |
| 108 | + ) |
| 109 | + ; |
| 110 | + |
| 111 | + $optionsResolver |
| 112 | + ->define('request') |
| 113 | + ->default( |
| 114 | + function (OptionsResolver $requestOptionsResolver) { |
| 115 | + $requestOptionsResolver |
| 116 | + ->define('timeout') |
| 117 | + ->allowedTypes('int') |
| 118 | + ->default(30) |
| 119 | + ; |
| 120 | + } |
| 121 | + ) |
| 122 | + ; |
| 123 | + |
| 124 | + $this->_options = $optionsResolver->resolve($options); |
| 125 | + } |
| 126 | + |
34 | 127 | /** |
35 | 128 | * {@inheritDoc} |
36 | 129 | */ |
|
0 commit comments