6
6
use Behat \Mink \Exception \ExpectationException ;
7
7
use Behat \Mink \Exception \ResponseTextException ;
8
8
use Behat \Mink \Exception \ElementNotFoundException ;
9
+ use WebDriver \Exception \StaleElementReference ;
10
+ use Behat \Behat \Tester \Exception \PendingException ;
9
11
10
12
class BrowserContext extends BaseContext
11
13
{
12
14
private $ timeout ;
13
15
private $ dateFormat = 'dmYHi ' ;
16
+ private $ timerStartedAt ;
14
17
15
18
public function __construct ($ timeout = 1 )
16
19
{
@@ -25,6 +28,16 @@ public function closeBrowser()
25
28
$ this ->getSession ()->stop ();
26
29
}
27
30
31
+ /**
32
+ * @BeforeScenario
33
+ *
34
+ * @When (I )start timing now
35
+ */
36
+ public function startTimer ()
37
+ {
38
+ $ this ->timerStartedAt = time ();
39
+ }
40
+
28
41
/**
29
42
* Set login / password for next HTTP authentication
30
43
*
@@ -164,21 +177,35 @@ public function iWaitUntilISee($text)
164
177
*/
165
178
public function iWaitSecondsUntilISeeInTheElement ($ count , $ text , $ element )
166
179
{
180
+ $ startTime = time ();
167
181
$ this ->iWaitSecondsForElement ($ count , $ element );
168
182
169
183
$ expected = str_replace ('\\" ' , '" ' , $ text );
170
- $ node = $ this ->getSession ()->getPage ()->find ('css ' , $ element );
171
184
$ message = "The text ' $ expected' was not found after a $ count seconds timeout " ;
172
185
173
- $ this ->assertContains ($ expected , $ node ->getText (), $ message );
186
+ $ found = false ;
187
+ do {
188
+ try {
189
+ usleep (1000 );
190
+ $ node = $ this ->getSession ()->getPage ()->find ('css ' , $ element );
191
+ $ this ->assertContains ($ expected , $ node ->getText (), $ message );
192
+ return ;
193
+ }
194
+ catch (ExpectationException $ e ) {
195
+ /* Intentionaly leave blank */
196
+ }
197
+ catch (StaleElementReference $ e ) {
198
+ // assume page reloaded whilst we were still waiting
199
+ }
200
+ } while (!$ found && (time () - $ startTime < $ count ));
174
201
}
175
202
176
203
/**
177
204
* @Then (I )wait :count second(s)
178
205
*/
179
206
public function iWaitSeconds ($ count )
180
207
{
181
- sleep ($ count );
208
+ usleep ($ count * 1000000 );
182
209
}
183
210
184
211
/**
@@ -213,6 +240,7 @@ public function iWaitSecondsForElement($count, $element)
213
240
214
241
do {
215
242
try {
243
+ usleep (1000 );
216
244
$ node = $ this ->getSession ()->getPage ()->findAll ('css ' , $ element );
217
245
$ this ->assertCount (1 , $ node );
218
246
$ found = true ;
@@ -221,7 +249,7 @@ public function iWaitSecondsForElement($count, $element)
221
249
/* Intentionnaly leave blank */
222
250
}
223
251
}
224
- while (time () - $ startTime < $ count );
252
+ while (! $ found && ( time () - $ startTime < $ count) );
225
253
226
254
if ($ found === false ) {
227
255
$ message = "The element ' $ element' was not found after a $ count seconds timeout " ;
@@ -376,4 +404,32 @@ public function switchToMainFrame()
376
404
{
377
405
$ this ->getSession ()->switchToIFrame ();
378
406
}
407
+
408
+ /**
409
+ * test time from when the scenario started
410
+ *
411
+ * @Then (the )total elapsed time should be :comparison than :expected seconds
412
+ * @Then (the )total elapsed time should be :comparison to :expected seconds
413
+ */
414
+ public function elapsedTime ($ comparison , $ expected )
415
+ {
416
+ $ elapsed = time () - $ this ->timerStartedAt ;
417
+
418
+ switch ($ comparison ) {
419
+ case 'less ' :
420
+ $ this ->assertTrue ($ elapsed < $ expected , "Elapsed time ' $ elapsed' is not less than ' $ expected' seconds. " );
421
+ break ;
422
+
423
+ case 'more ' :
424
+ $ this ->assertTrue ($ elapsed > $ expected , "Elapsed time ' $ elapsed' is not more than ' $ expected' seconds. " );
425
+ break ;
426
+
427
+ case 'equal ' :
428
+ $ this ->assertTrue ($ elapsed === $ expected , "Elapsed time ' $ elapsed' is not ' $ expected' seconds. " );
429
+ break ;
430
+
431
+ default :
432
+ throw new PendingException ("Unknown comparison ' $ comparison'. Use 'less', 'more' or 'equal' " );
433
+ }
434
+ }
379
435
}
0 commit comments