【问题标题】:Equivalent of jQuery.active in javascript Selenium Web Driverjavascript Selenium Web Driver 中 jQuery.active 的等价物
【发布时间】:2019-03-02 01:34:34
【问题描述】:

我正在使用 Selenium webdriver 与某些网站进行交互。
如果网站使用 jQuery,我们可以使用 jQuery.active 获取待处理的 AJAX 请求:

 JavascriptExecutor jsx = (JavascriptExecutor) driver;

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

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

如果网站没有使用jQuery,我们如何计算XMLHttpRequest请求的数量?

【问题讨论】:

  • 或者我可以为每个XMLHttpRequest 请求使用事件监听器并增加一个计数器...
  • 任何人都可以提供一些对我有帮助的提示吗?

标签: javascript jquery selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

这里是一个如何使用 nightwatch 自定义命令等待 AJAX 请求的示例。

一个命令来初始化计数器。在每个send 上,如果会增加计数器,在open 上会减少它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
}

然后另一个自定义命令等待

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
}

【讨论】:

    【解决方案2】:

    将其保存在您的网站中,并从 selenium 中调用它。我认为没有任何类似的内置 js 功能。我认为这不是您要寻找的答案,但正如我上面所说的,javascript 本身并没有这样的功能。

    如果您无法编辑或向您的网站添加新脚本,您仍然可以运行该脚本。

    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;
        })()
    
    ");
    

    【讨论】:

    • 该网站不属于我。所以我不能在那里添加一些代码。 selenium 有什么方法可以在网站中注入一些函数/脚本。
    • 只需将该脚本仅功能部分包装到 Int totAjaxRequest = (Int)jsx.executeScript("{{here}}");答案已更新
    • 很难用。上面的函数因为每次都会重新初始化计数器
    猜你喜欢
    • 1970-01-01
    • 2014-02-27
    • 2013-08-25
    • 1970-01-01
    • 2013-10-08
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多