Selenium2 and Behat: Wait for AJAX to Finish

Recently ran across a case where I needed to wait for AJAX to finish during a Behat/Selenium2 test and couldn't find one. Ran across some examples for telling Selenium2 to wait and telling jQuery to wait for AJAX to finish, and decided to mash things up.

<?php
 
/**
   * Wait for AJAX to finish.
   *
[email protected]/^I wait for AJAX to finish$/
   */
  
public function iWaitForAjaxToFinish() {
    
$this->getSession()->wait(10000'(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
  }
?>

This adds a step to behat Then I wait for AJAX to finish that waits for 10 seconds or AJAX or a jQuery animation to finish. Adjust your times as needed, my test server for this particular project was shared with other resources, so I set mine a bit long. Hope it's useful.