4

So basically I'm trying to design a generic method using selenium, that waits for all the background network calls to finish (Including any Ajax, Angular, API calls). I know there is a way to get number of active ajax using JQuery.active, but that works only when the application is build upon JQuery framework. Moreover, i want something that waits for all the network calls to finish and not something specific to Ajax alone.

So here i'm trying to get the number of network calls that are happening currently in the background and wait for it to be zero.

However, that's my idea of handling it, any new ideas or suggestions are welcomed.

0

1 Answer 1

5

Here's an example to wait for no pending Ajax request with Chrome:

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
import json


def send_chrome(driver, cmd, params={}) :
  resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
  url = driver.command_executor._url + resource
  body = json.dumps({'cmd': cmd, 'params': params})
  response = driver.command_executor._request('POST', url, body)
  if response.get('status'):
    raise Exception(response.get('value'))
  return response.get('value')

def extend_xhr_chrome(driver) :
  send_chrome(driver, "Page.addScriptToEvaluateOnNewDocument", {\
      "source":
        "(function(){"
        "  var send = XMLHttpRequest.prototype.send;"
        "  var release = function(){ --XMLHttpRequest.active };"
        "  var onloadend = function(){ setTimeout(release, 1) };"
        "  XMLHttpRequest.active = 0;"
        "  XMLHttpRequest.prototype.send = function() {"
        "    ++XMLHttpRequest.active;"
        "    this.addEventListener('loadend', onloadend, true);"
        "    send.apply(this, arguments);"
        "  };"
        "})();"
    })

def is_xhr_idle_chrome(driver) :
    return send_chrome(driver, 'Runtime.evaluate', {\
      'returnByValue': True,\
      'expression': "XMLHttpRequest.active == 0"
    })['result']['value']


# launch Chrome
driver = webdriver.Chrome()

# extend XMLHttpRequest
extend_xhr_chrome(driver)

driver.get("https://stackoverflow.com")

# wait for no pending request
WebDriverWait(driver, 20, 0.08) \
  .until(is_xhr_idle_chrome)
3
  • Can we have it as browser independent ? Because my test may run on different browsers based on needs. Mar 1, 2018 at 13:33
  • 1
    You can inject the JavaScript with execute_script to make it browser independent. Though you'll have to do it every time a new page is loaded and it won't account for the requests that are already running.
    – Florent B.
    Mar 1, 2018 at 13:39
  • 1
    It worked for me this way, I manually injected the JS into DOM and the quried for XMLHttpRequest.active, that gave me the number of active calls - Voila :) Mar 1, 2018 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.