1313use Facebook \WebDriver \WebDriverSelect ;
1414use Facebook \WebDriver \WebDriverBy ;
1515use Facebook \WebDriver \Exception \NoSuchElementException ;
16+ use Facebook \WebDriver \WebDriverExpectedCondition ;
1617use Codeception \Exception \ElementNotFound ;
1718use Codeception \Exception \ModuleConfigException ;
1819use Codeception \Exception \ModuleException ;
1920use Codeception \Util \Uri ;
2021use Codeception \Util \ActionSequence ;
22+ use Codeception \Util \Locator ;
2123use Magento \FunctionalTestingFramework \Util \Protocol \CurlTransport ;
2224use Magento \FunctionalTestingFramework \Util \Protocol \CurlInterface ;
2325use Magento \Setup \Exception ;
@@ -55,6 +57,8 @@ class MagentoWebDriver extends WebDriver
5557 '//div[@data-role="spinner"] '
5658 ];
5759
60+ const DEFAULT_WAIT_TIMEOUT = 10 ;
61+
5862 /**
5963 * The module required fields, to be set in the suite .yml configuration file.
6064 *
@@ -115,6 +119,11 @@ public function _resetConfig()
115119 $ this ->config = ConfigSanitizerUtil::sanitizeWebDriverConfig ($ this ->config );
116120 }
117121
122+ public function getDefaultWaitTimeout ()
123+ {
124+ return getenv ('WAIT_TIMEOUT ' ) ?: self ::DEFAULT_WAIT_TIMEOUT ;
125+ }
126+
118127 /**
119128 * Returns URL of a host.
120129 *
@@ -301,14 +310,148 @@ public function selectMultipleOptions($selectSearchTextField, $selectSearchResul
301310 }
302311 }
303312
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+
304447 /**
305448 * Wait for all Ajax calls to finish.
306449 *
307450 * @param int $timeout
308451 */
309452 public function waitForAjaxLoad ($ timeout = null )
310453 {
311- $ timeout = $ timeout ?? $ this ->_getConfig ()[ ' pageload_timeout ' ] ;
454+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout () ;
312455
313456 try {
314457 $ this ->waitForJS ('return !!window.jQuery && window.jQuery.active == 0; ' , $ timeout );
@@ -327,7 +470,7 @@ public function waitForAjaxLoad($timeout = null)
327470 */
328471 public function waitForPageLoad ($ timeout = null )
329472 {
330- $ timeout = $ timeout ?? $ this ->_getConfig ()[ ' pageload_timeout ' ] ;
473+ $ timeout = $ timeout ?? $ this ->getDefaultWaitTimeout () ;
331474
332475 $ this ->waitForJS ('return document.readyState == "complete" ' , $ timeout );
333476 $ this ->waitForAjaxLoad ($ timeout );
0 commit comments