1515
1616namespace Itnelo \React \WebDriver ;
1717
18+ use Itnelo \React \WebDriver \Timeout \Interceptor as TimeoutInterceptor ;
1819use React \EventLoop \LoopInterface ;
1920use React \Promise \PromiseInterface ;
2021use RuntimeException ;
2122use Symfony \Component \OptionsResolver \Exception \ExceptionInterface as ConfigurationExceptionInterface ;
2223use Symfony \Component \OptionsResolver \OptionsResolver ;
23- use Throwable ;
2424use function React \Promise \reject ;
25- use function React \Promise \Timer \timeout ;
2625
26+ /**
27+ * Sends action requests to the Selenium Grid server (hub) and controls their async execution
28+ */
2729class SeleniumHubDriver implements WebDriverInterface
2830{
2931 /**
@@ -40,6 +42,13 @@ class SeleniumHubDriver implements WebDriverInterface
4042 */
4143 private ClientInterface $ hubClient ;
4244
45+ /**
46+ * Cancels a driver promise if it isn't resolved within the specified amount of time
47+ *
48+ * @var TimeoutInterceptor
49+ */
50+ private TimeoutInterceptor $ timeoutInterceptor ;
51+
4352 /**
4453 * Array of options for the driver
4554 *
@@ -50,38 +59,26 @@ class SeleniumHubDriver implements WebDriverInterface
5059 /**
5160 * SeleniumHubDriver constructor.
5261 *
53- * @param LoopInterface $loop The event loop reference to manage promise timeouts and other async routines
54- * @param ClientInterface $hubClient Base client implementation for sending commands to the remote hub server
55- * @param array $options Array of options for the driver
62+ * @param LoopInterface $loop The event loop reference to manage promise timeouts
63+ * @param ClientInterface $hubClient Base client implementation for sending commands to the server
64+ * @param TimeoutInterceptor $timeoutInterceptor Cancels a driver promise if it isn't resolved for too long
65+ * @param array $options Array of options for the driver
5666 *
5767 * @throws ConfigurationExceptionInterface Whenever an error has been occurred during driver configuration
5868 */
59- public function __construct (LoopInterface $ loop , ClientInterface $ hubClient , array $ options = [])
60- {
69+ public function __construct (
70+ LoopInterface $ loop ,
71+ ClientInterface $ hubClient ,
72+ TimeoutInterceptor $ timeoutInterceptor ,
73+ array $ options = []
74+ ) {
6175 $ optionsResolver = new OptionsResolver ();
6276
63- $ optionsResolver
64- ->define ('command ' )
65- ->info ('Options to control behavior of the commands, which will be executed on the remote server ' )
66- ->default (
67- function (OptionsResolver $ requestOptionsResolver ) {
68- $ requestOptionsResolver
69- ->define ('timeout ' )
70- ->info (
71- 'Maximum time to wait (in seconds) for command execution '
72- . '(do not correlate with HTTP timeouts) '
73- )
74- ->allowedTypes ('int ' )
75- ->default (30 )
76- ;
77- }
78- )
79- ;
80-
8177 $ this ->_options = $ optionsResolver ->resolve ($ options );
8278
83- $ this ->loop = $ loop ;
84- $ this ->hubClient = $ hubClient ;
79+ $ this ->loop = $ loop ;
80+ $ this ->hubClient = $ hubClient ;
81+ $ this ->timeoutInterceptor = $ timeoutInterceptor ;
8582 }
8683
8784 /**
@@ -111,20 +108,10 @@ public function createSession(): PromiseInterface
111108 {
112109 $ sessionIdentifierPromise = $ this ->hubClient ->createSession ();
113110
114- // applying command timeout.
115- $ commandTimeoutInSeconds = $ this ->_options ['command ' ]['timeout ' ];
116-
117- // global rejection handler for all internal side effects (timeout inclusive).
118- // todo: move to the separate service
119- $ sessionIdentifierTimedPromise = timeout ($ sessionIdentifierPromise , $ commandTimeoutInSeconds , $ this ->loop );
120-
121- $ sessionIdentifierTimedPromise = $ sessionIdentifierTimedPromise ->otherwise (
122- function (Throwable $ rejectionReason ) {
123- throw new RuntimeException ('Unable to finish a session create command. ' , 0 , $ rejectionReason );
124- }
111+ return $ this ->timeoutInterceptor ->applyTimeout (
112+ $ sessionIdentifierPromise ,
113+ 'Unable to complete a session create command. '
125114 );
126-
127- return $ sessionIdentifierTimedPromise ;
128115 }
129116
130117 /**
0 commit comments