Skip to content

Instantly share code, notes, and snippets.

@jmauerhan
Last active August 5, 2017 21:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmauerhan/f839926ea527ff5e74e7 to your computer and use it in GitHub Desktop.
Save jmauerhan/f839926ea527ff5e74e7 to your computer and use it in GitHub Desktop.
Behat: Wait For AJAX Angular & jQuery
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