Equivalent of jQuery.active in javascript Selenium Web Driver Equivalent of jQuery.active in javascript Selenium Web Driver selenium selenium

Equivalent of jQuery.active in javascript Selenium Web Driver


Here an example how the waiting for AJAX requests can be done with nightwatch custom commands.

One command to init the counter. on every send if will increase the counter and on ever open it will decrease it customCommands/initAjaxCounters.js:

exports.command = function () {  this.execute(function () {    window.sumStartedAjaxRequests = 0    window.activeAjaxCount = 0    function isAllXhrComplete () {      window.activeAjaxCount--    }    (function (open) {      XMLHttpRequest.prototype.open = function () {        this.addEventListener('loadend', isAllXhrComplete)        return open.apply(this, arguments)      }    })(XMLHttpRequest.prototype.open)  })  this.execute(function () {    (function (send) {      XMLHttpRequest.prototype.send = function () {        window.activeAjaxCount++        window.sumStartedAjaxRequests++        return send.apply(this, arguments)      }    })(XMLHttpRequest.prototype.send)  })  return this}

and then an other custom command to wait

const sleepWhenOutstandingAjaxCalls = function (result) {  if (result.value > 0) {    this.pause(this.globals.waitForConditionPollInterval)    this.waitForOutstandingAjaxCalls()  }}exports.command = function () {  // init the ajax counter if it hasn't been initialized yet  this.execute('return (typeof window.activeAjaxCount === "undefined")', [], function (result) {    if (result.value === true) {      throw Error('checking outstanding Ajax calls will not work without calling initAjaxCounter() first')    }  })  this.execute(    'return window.activeAjaxCount', [], sleepWhenOutstandingAjaxCalls  )  return this}


Keep this in your website, and call it from selenium. I think there is no any similar built in js function. I don't think this is the answer you are looking for, but as I said above javascript does not have such a function itself.

If you can not edit or add new script to your website, you can still run the script.

Int totAjaxRequest = (Int)jsx.executeScript("    (function(){        var count = 0;        XMLHttpRequest.prototype.nativeSend = XMLHttpRequest.prototype.send;        XMLHttpRequest.prototype.send = function(body) {            this.onreadystatechange  = function(){               switch(this.readyState){                   case 2: count++; break                   case 4: count--; break               }            };            this.nativeSend(body);        };        return count;    })()");