javascript Selenium Web驱动程序中jQuery.active的等效项
我正在使用Selenium webdriver与某些网站进行交互。javascript Selenium Web驱动程序中jQuery.active的等效项,javascript,jquery,selenium,selenium-webdriver,selenium-firefoxdriver,Javascript,Jquery,Selenium,Selenium Webdriver,Selenium Firefoxdriver,我正在使用Selenium webdriver与某些网站进行交互。 如果网站正在使用jQuery,我们可以使用jQuery.active:获取挂起的AJAX请求 JavascriptExecutor jsx = (JavascriptExecutor) driver; Int-totAjaxRequest=(Int)jsx.executeScript(“jQuery.active”) 如果网站没有使用jQuery,我们如何计算XMLHttpRequest请求的数量?将其保存在网站中,并从se
如果网站正在使用jQuery,我们可以使用
jQuery.active
:获取挂起的AJAX请求
JavascriptExecutor jsx = (JavascriptExecutor) driver;
Int-totAjaxRequest=(Int)jsx.executeScript(“jQuery.active”)
如果网站没有使用jQuery,我们如何计算
XMLHttpRequest
请求的数量?将其保存在网站中,并从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;
})()
");
下面是一个示例,说明如何使用nightwatch自定义命令来等待AJAX请求 一个初始化计数器的命令。每次
send
if将增加计数器,每次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
}
或者我可以为每个
XMLHttpRequest
请求使用事件监听器并增加一个计数器…任何人都可以给出一些对我有帮助的提示。该网站不属于我。所以我不能在那里添加一些代码。在selenium中是否有任何方法可以在网站中插入一些函数/脚本。只需将仅脚本的函数部分包装为Int-totAjaxRequest=(Int)jsx.executeScript(“{here}”);答案是UpdateEdit很难使用。请使用上述函数,因为它每次都会重新初始化计数器
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
}