diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index d6c8f8a62b9..cccf0cc1e4e 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -328,20 +328,11 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { if (!$timeout) { $timeout = self::get_timeout(); } - if ($microsleep) { - // Will sleep 1/10th of a second by default for self::get_timeout() seconds. - $loops = $timeout * 10; - } else { - // Will sleep for self::get_timeout() seconds. - $loops = $timeout; - } - // DOM will never change on non-javascript case; do not wait or try again. - if (!$this->running_javascript()) { - $loops = 1; - } + $start = microtime(true); + $end = $start + $timeout; - for ($i = 0; $i < $loops; $i++) { + do { // We catch the exception thrown by the step definition to execute it again. try { // We don't check with !== because most of the time closures will return @@ -357,14 +348,13 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { } } - if ($this->running_javascript()) { - if ($microsleep) { - usleep(100000); - } else { - sleep(1); - } + if (!$this->running_javascript()) { + break; } - } + + usleep(100000); + + } while (microtime(true) < $end); // Using coding_exception as is a development issue if no exception has been provided. if (!$exception) { diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index 087a6bfe9b8..8df8d3652b2 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -990,11 +990,7 @@ EOF; // Using the spin method as we want a reduced timeout but there is no need for a 0.1 seconds interval // because in the optimistic case we will timeout. // If all goes good it will throw an ElementNotFoundExceptionn that we will catch. - $this->spin( - function($context, $args) use ($selectortype, $element) { - return $this->find($selectortype, $element); - }, [], behat_base::get_reduced_timeout(), $exception, false - ); + return $this->find($selectortype, $element, $exception, false, behat_base::get_reduced_timeout()); } catch (ElementNotFoundException $e) { // We expect the element to not be found. return;