Skip to content

Commit a920b4d

Browse files
committed
W3CClient constructor & options
1 parent d64c9c2 commit a920b4d

3 files changed

Lines changed: 100 additions & 2 deletions

File tree

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
"minimum-stability": "stable",
2424
"require": {
2525
"php": ">=7.4",
26-
"react/promise": "^2.8"
26+
"react/http": "^1.1",
27+
"react/promise": "^2.8",
28+
"symfony/options-resolver": "^5.1"
29+
},
30+
"require-dev": {
31+
"php-webdriver/webdriver": "^1.9"
2732
},
2833
"config": {
2934
"sort-packages": true

src/Client/W3CClient.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
namespace Itnelo\React\WebDriver\Client;
1717

1818
use Itnelo\React\WebDriver\ClientInterface;
19+
use React\Http\Browser;
1920
use React\Promise\PromiseInterface;
21+
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface as OptionsResolverExceptionInterface;
22+
use Symfony\Component\OptionsResolver\OptionsResolver;
2023

2124
/**
2225
* W3C compliant WebDriver client for Selenium Grid server (hub) that performs asynchronously.
@@ -31,6 +34,96 @@
3134
*/
3235
class W3CClient implements ClientInterface
3336
{
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+
34127
/**
35128
* {@inheritDoc}
36129
*/

src/ClientInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getElementIdentifier(string $sessionIdentifier, string $xpathQue
153153
* Returns a promise that resolves to an array, representing identifier (an internal handle) of the currently
154154
* active (focused) element on page.
155155
*
156-
* Resulting value is the same as {@link getElementIdentifier()}.
156+
* Resulting value has the same representation as {@link getElementIdentifier()} resval.
157157
*
158158
* @param string $sessionIdentifier Session identifier for Selenium Grid server (hub)
159159
*

0 commit comments

Comments
 (0)