Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of jQuery.active in javascript Selenium Web Driver

I'm using Selenium webdriver to interact with some website.
If the website is using jQuery we can get the pending AJAX request by using jQuery.active:

 JavascriptExecutor jsx = (JavascriptExecutor) driver;

Int totAjaxRequest = (Int)jsx.executeScript("jQuery.active");

Int totAjaxRequest = (Int)jsx.executeScript("return jQuery.active");

In case if the website is not using jQuery, how could we count the number of XMLHttpRequest requests?

like image 835
शेखर Avatar asked Sep 26 '18 09:09

शेखर


People also ask

Can we use jQuery in selenium?

Selenium WebDriver can be enhanced by jQuery selectors using the jQuery API. However, we need to make sure that the page has jQuery API loaded before using these selectors. The jQuery API provides the find() function through which we can search for elements.

Can I use JavaScript with selenium Webdriver?

Selenium WebDriver with JavaScript is a favorable combination to perform automated UI testing of applications. JavaScript offers efficiency with its well-built and structured patterns and functions, making the script more compact. It offers security and is well supported by a large community of developers.

What is jQuery active?

Show activity on this post. I was trying to find some more information on the following jQuery function: jQuery. active. It is described to test the number of active connections to a server and will evaluate true when the number of connections is zero.


1 Answers

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
}
like image 84
INDIVIDUAL-IT Avatar answered Sep 28 '22 10:09

INDIVIDUAL-IT