13
13
use Facebook \WebDriver \WebDriverSelect ;
14
14
use Facebook \WebDriver \WebDriverBy ;
15
15
use Facebook \WebDriver \Exception \NoSuchElementException ;
16
+ use Facebook \WebDriver \WebDriverExpectedCondition ;
16
17
use Codeception \Exception \ElementNotFound ;
17
18
use Codeception \Exception \ModuleConfigException ;
18
19
use Codeception \Exception \ModuleException ;
19
20
use Codeception \Util \Uri ;
20
21
use Codeception \Util \ActionSequence ;
22
+ use Codeception \Util \Locator ;
21
23
use Magento \FunctionalTestingFramework \Util \Protocol \CurlTransport ;
22
24
use Magento \FunctionalTestingFramework \Util \Protocol \CurlInterface ;
23
25
use Magento \Setup \Exception ;
@@ -55,6 +57,8 @@ class MagentoWebDriver extends WebDriver
55
57
'//div[@data-role="spinner"] '
56
58
];
57
59
60
+ const DEFAULT_WAIT_TIMEOUT = 10 ;
61
+
58
62
/**
59
63
* The module required fields, to be set in the suite .yml configuration file.
60
64
*
@@ -115,6 +119,11 @@ public function _resetConfig()
115
119
$ this ->config = ConfigSanitizerUtil::sanitizeWebDriverConfig ($ this ->config );
116
120
}
117
121
122
+ public function getDefaultWaitTimeout ()
123
+ {
124
+ return getenv ('WAIT_TIMEOUT ' ) ?: self ::DEFAULT_WAIT_TIMEOUT ;
125
+ }
126
+
118
127
/**
119
128
* Returns URL of a host.
120
129
*
@@ -301,14 +310,148 @@ public function selectMultipleOptions($selectSearchTextField, $selectSearchResul
301
310
}
302
311
}
303
312
313
+ /**
314
+ * Wait for $timeout seconds.
315
+ *
316
+ * @param int|float $timeout secs
317
+ * @throws \Codeception\Exception\TestRuntimeException
318
+ */
319
+ public function wait ($ timeout = null )
320
+ {
321
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
322
+
323
+ if ($ timeout >= 1000 ) {
324
+ throw new TestRuntimeException (
325
+ "
326
+ Waiting for more then 1000 seconds: 16.6667 mins \n
327
+ Please note that wait method accepts number of seconds as parameter. "
328
+ );
329
+ }
330
+ usleep ($ timeout * 1000000 );
331
+ }
332
+
333
+ /**
334
+ * Waits up to $timeout seconds for the given element to change.
335
+ *
336
+ * @param $element
337
+ * @param \Closure $callback
338
+ * @param int $timeout seconds
339
+ * @throws \Codeception\Exception\ElementNotFound
340
+ * @throws \Exception
341
+ */
342
+ public function waitForElementChange ($ element , \Closure $ callback , $ timeout = null )
343
+ {
344
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
345
+
346
+ $ el = $ this ->matchFirstOrFail ($ this ->baseElement , $ element );
347
+ $ checker = function () use ($ el , $ callback ) {
348
+ return $ callback ($ el );
349
+ };
350
+ $ this ->webDriver ->wait ($ timeout )->until ($ checker );
351
+ }
352
+
353
+ /**
354
+ * Waits up to $timeout seconds for an element to appear on the page.
355
+ *
356
+ * @param $element
357
+ * @param int $timeout seconds
358
+ * @throws \Exception
359
+ */
360
+ public function waitForElement ($ element , $ timeout = null )
361
+ {
362
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
363
+
364
+ $ condition = WebDriverExpectedCondition::presenceOfElementLocated ($ this ->getLocator ($ element ));
365
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition );
366
+ }
367
+
368
+ /**
369
+ * Waits up to $timeout seconds for the given element to be visible on the page.
370
+ *
371
+ * @param $element
372
+ * @param int $timeout seconds
373
+ * @throws \Exception
374
+ */
375
+ public function waitForElementVisible ($ element , $ timeout = null )
376
+ {
377
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
378
+
379
+ $ condition = WebDriverExpectedCondition::visibilityOfElementLocated ($ this ->getLocator ($ element ));
380
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition );
381
+ }
382
+
383
+ /**
384
+ * Waits up to $timeout seconds for the given element to become invisible.
385
+ *
386
+ * @param $element
387
+ * @param int $timeout seconds
388
+ * @throws \Exception
389
+ */
390
+ public function waitForElementNotVisible ($ element , $ timeout = null )
391
+ {
392
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
393
+
394
+ $ condition = WebDriverExpectedCondition::invisibilityOfElementLocated ($ this ->getLocator ($ element ));
395
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition );
396
+ }
397
+
398
+ /**
399
+ * Waits up to $timeout seconds for the given string to appear on the page.
400
+ *
401
+ * @param string $text
402
+ * @param int $timeout seconds
403
+ * @param string $selector optional
404
+ * @throws \Exception
405
+ */
406
+ public function waitForText ($ text , $ timeout = null , $ selector = null )
407
+ {
408
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
409
+
410
+ $ message = sprintf (
411
+ 'Waited for %d secs but text %s still not found ' ,
412
+ $ timeout ,
413
+ Locator::humanReadableString ($ text )
414
+ );
415
+ if (!$ selector ) {
416
+ $ condition = WebDriverExpectedCondition::elementTextContains (WebDriverBy::xpath ('//body ' ), $ text );
417
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition , $ message );
418
+ return ;
419
+ }
420
+
421
+ $ condition = WebDriverExpectedCondition::elementTextContains ($ this ->getLocator ($ selector ), $ text );
422
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition , $ message );
423
+ }
424
+
425
+ /**
426
+ * Executes JavaScript and waits up to $timeout seconds for it to return true.
427
+ *
428
+ * @param string $script
429
+ * @param int $timeout seconds
430
+ * @throws \Exception
431
+ */
432
+ public function waitForJS ($ script , $ timeout = null )
433
+ {
434
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout ();
435
+
436
+ $ condition = function ($ wd ) use ($ script ) {
437
+ return $ wd ->executeScript ($ script );
438
+ };
439
+ $ message = sprintf (
440
+ 'Waited for %d secs but script %s still not executed ' ,
441
+ $ timeout ,
442
+ Locator::humanReadableString ($ script )
443
+ );
444
+ $ this ->webDriver ->wait ($ timeout )->until ($ condition , $ message );
445
+ }
446
+
304
447
/**
305
448
* Wait for all Ajax calls to finish.
306
449
*
307
450
* @param int $timeout
308
451
*/
309
452
public function waitForAjaxLoad ($ timeout = null )
310
453
{
311
- $ timeout = $ timeout ?? $ this ->_getConfig ()[ ' pageload_timeout ' ] ;
454
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout () ;
312
455
313
456
try {
314
457
$ this ->waitForJS ('return !!window.jQuery && window.jQuery.active == 0; ' , $ timeout );
@@ -327,7 +470,7 @@ public function waitForAjaxLoad($timeout = null)
327
470
*/
328
471
public function waitForPageLoad ($ timeout = null )
329
472
{
330
- $ timeout = $ timeout ?? $ this ->_getConfig ()[ ' pageload_timeout ' ] ;
473
+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout () ;
331
474
332
475
$ this ->waitForJS ('return document.readyState == "complete" ' , $ timeout );
333
476
$ this ->waitForAjaxLoad ($ timeout );
0 commit comments