简体   繁体   中英

Wait for all ajax calls to terminate in selenium webdriverjs

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:

  • Use WebDriver.wait to actually wait for the condition
  • Use $(document).ajaxStop(function () {...}); to determine if all ajax calls are terminated
  • Use WebDriver.executeAsyncScript to invoke the needed JavaScript code in the browser

I imagine something like this:

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;');
});

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.

try to use the executeScript (in synchronous mode). Use console.log to debug (tested with Chrome).

Below the official javadoc: http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html

Below is a snippet:

 driver.executeScript("window.ajax_running = true;");
 driver.executeScript("$(document).ajaxStart(function () 
       {console.log(\"ajaxStart\"); window.ajax_running = true;});");
 driver.executeScript("$(document).ajaxStop(function () 
       {console.log(\"ajaxStop\"); window.ajax_running = false;});");
 sysout(driver.executeScript("return window.ajax_running"));

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM