Last active
August 5, 2017 21:34
-
-
Save jmauerhan/f839926ea527ff5e74e7 to your computer and use it in GitHub Desktop.
Behat: Wait For AJAX Angular & jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function waitForAjax() | |
{ | |
$waitTime = 10000; | |
try { | |
//Wait for Angular | |
$angularIsNotUndefined = $this->getSession()->evaluateScript("return (typeof angular != 'undefined')"); | |
if ($angularIsNotUndefined) { | |
//If you run the below code on a page ending in #, the page reloads. | |
if (substr($this->getSession()->getCurrentUrl(), -1) !== '#') { | |
$angular = 'angular.getTestability(document.body).whenStable(function() { | |
window.__testable = true; | |
})'; | |
$this->getSession()->evaluateScript($angular); | |
$this->getSession()->wait($waitTime, 'window.__testable == true'); | |
} | |
/* | |
* Angular JS AJAX can't be detected overall like in jQuery, | |
* but we can check if any of the html elements are marked as showing up when ajax is running, | |
* then wait for them to disappear. | |
*/ | |
$ajaxRunningXPath = "//*[@ng-if='ajax_running']"; | |
$this->waitForElementToDisappear($ajaxRunningXPath, $waitTime); | |
} | |
//Wait for jQuery | |
if ($this->getSession()->evaluateScript("return (typeof jQuery != 'undefined')")) { | |
$this->getSession()->wait($waitTime, '(0 === jQuery.active && 0 === jQuery(\':animated\').length)'); | |
} | |
} catch (Exception $e) { | |
var_dump($e->getMessage()); //Debug here. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment