title: Wait for all ajax calls to terminate in selenium webdriverjs
body: <p>I'm trying to implement a function that allows me to wait for any running (jQuery) ajax call to terminate in a selenium webdriverjs test environment.
  My current working assumption is that I need to properly integrate the following elements:</p>
  
  <ul>
  <li>Use <code>WebDriver.wait</code> to actually wait for the condition</li>
  <li>Use <code>$(document).ajaxStop(function () {...});</code> to determine if all ajax calls are terminated</li>
  <li>Use <code>WebDriver.executeAsyncScript</code> to invoke the needed JavaScript code in the browser</li>
  </ul>
  
  <p>I imagine something like this:</p>
  
  <pre><code>const PREPARE_SCRIPT = 'window.ajax_running = false;
  $(document).ajaxStart(function () {window.ajax_running = true;});
  $(document).ajaxStop(function () {window.ajax_running = false;});';
  driver.executeAsyncScript(PREPARE_SCRIPT)
  driver.wait(function () {
     return driver.executeAsyncScript('return window.ajax_running === false;');
  });
  </code></pre>
  
  <p>Unfortunately I do not seem to have the needed experience to get all the needed elements to work together or I might just be on the wrong track altogether and there is a better solution.</p>