如何使用Selenium Web驱动程序PhantomJs跟踪或监视xmlhttp请求(Ajax调用)

时间:2014-11-05 08:43:31

标签: javascript ajax selenium

我需要在Selenium Web驱动程序PhantomJs的当前文档中找到有多少ajax调用待处理,我没有引用ajax调用对象

我尝试执行此javascript:

var s_ajaxListener = new Object();
s_ajaxListener.tempSend = XMLHttpRequest.prototype.send;

function increaseCounter () {
    window.ajaxRequestCount = window.ajaxRequestCount + 1;
}
function decreaseCounter() {
    window.ajaxRequestCount = window.ajaxRequestCount - 1;
}
XMLHttpRequest.prototype.send = function() {
    increaseCounter()
    s_ajaxListener.tempSend.apply(this, arguments);
    this.addEventListener("progress", increaseCounter, false);
    this.addEventListener("load",decreaseCounter , false);
    this.addEventListener("error", decreaseCounter, false);
    this.addEventListener("abort", decreaseCounter, false); 
}

但是,执行此print "ax-count: ", browserInstance.execute_script("return window.ajaxRequestCount")输出时:None

0 个答案:

没有答案