java web 请求跟踪_如果在网页中发生Ajax请求,则使用Java脚本进行跟踪或通过Selenium Web驱动程序拦截XMLHttpRequest...

这篇博客介绍如何在Java Web应用中跟踪Ajax请求,特别是在无限滚动场景下。通过在网页中注入JavaScript,监听并处理Ajax的生命周期事件,如open、send、onreadystatechange等,以实现动态跟踪Ajax请求的状态,避免使用Selenium WebDriver的静态延迟。文章还提供了两种不同的JavaScript实现方式来监控Ajax请求。
摘要由CSDN通过智能技术生成

对于在无限滚动期间使用Ajax Response和使用jQuery API(或其他操作)的网页,在开始打开网页之前。

//Inject the pooling status variable

js.executeScript("window.status = 'fail';");

//Attach the Ajax call back method

js.executeScript("$(document).ajaxComplete(function() {" +

"status = 'success';});");

步骤1:将保持不变,如原来的问题

步骤2池下面的脚本(这是一个其除去的Thread.Sleep()的需要,并且使逻辑更加动态)

String aStatus = (String)js.executeScript("return status;");

if(aStatus!=null && aStatus.equalsIgnoreCase("success")){

js.executeScript("status = 'fail';");

break poolingLoop;

}

第3步:现在没有必要!

结论:不需要钝头Thread.sleep();一次又一次使用Selenium WebDriver!

只有在Web应用程序中使用jQuery API时,此方法才有效。

编辑: 由于按照由@jayati给出的链接我注入Javascript成为

的Javascript之一:

//XMLHttpRequest instrumentation/wrapping

var startTracing = function (onnew) {

var OldXHR = window.XMLHttpRequest;

// create a wrapper object that has the same interfaces as a regular XMLHttpRequest object

// see http://www.xulplanet.com/references/objref/XMLHttpRequest.html for reference on XHR object

var NewXHR = function() {

var self = this;

var actualXHR = new OldXHR();

// private callbacks (for UI):

// onopen, onsend, onsetrequestheader, onupdate, ...

this.requestHeaders = "";

this.requestBody = "";

// emulate methods from regular XMLHttpRequest object

this.open = function(a, b, c, d, e) {

self.openMethod = a.toUpperCase();

self.openURL = b;

ajaxRequestStarted = 'open';

if (self.onopen != null && typeof(self.onopen) == "function") {

self.onopen(a,b,c,d,e); }

return actualXHR.open(a,b,c,d,e);

}

this.send = function(a) {

ajaxRequestStarted = 'send';

if (self.onsend != null && typeof(this.onsend) == "function") {

self.onsend(a); }

self.requestBody += a;

return actualXHR.send(a);

}

this.setRequestHeader = function(a, b) {

if (self.onsetrequestheader != null && typeof(self.onsetrequestheader) == "function") { self.onsetrequestheader(a, b); }

self.requestHeaders += a + ":" + b + "\r\n";

return actualXHR.setRequestHeader(a, b);

}

this.getRequestHeader = function() {

return actualXHR.getRequestHeader();

}

this.getResponseHeader = function(a) { return actualXHR.getResponseHeader(a); }

this.getAllResponseHeaders = function() { return actualXHR.getAllResponseHeaders(); }

this.abort = function() { return actualXHR.abort(); }

this.addEventListener = function(a, b, c) { return actualXHR.addEventListener(a, b, c); }

this.dispatchEvent = function(e) { return actualXHR.dispatchEvent(e); }

this.openRequest = function(a, b, c, d, e) { return actualXHR.openRequest(a, b, c, d, e); }

this.overrideMimeType = function(e) { return actualXHR.overrideMimeType(e); }

this.removeEventListener = function(a, b, c) { return actualXHR.removeEventListener(a, b, c); }

// copy the values from actualXHR back onto self

function copyState() {

// copy properties back from the actual XHR to the wrapper

try {

self.readyState = actualXHR.readyState;

} catch (e) {}

try {

self.status = actualXHR.status;

} catch (e) {}

try {

self.responseText = actualXHR.responseText;

} catch (e) {}

try {

self.statusText = actualXHR.statusText;

} catch (e) {}

try {

self.responseXML = actualXHR.responseXML;

} catch (e) {}

}

// emulate callbacks from regular XMLHttpRequest object

actualXHR.onreadystatechange = function() {

copyState();

try {

if (self.onupdate != null && typeof(self.onupdate) == "function") { self.onupdate(); }

} catch (e) {}

// onreadystatechange callback

if (self.onreadystatechange != null && typeof(self.onreadystatechange) == "function") { return self.onreadystatechange(); }

}

actualXHR.onerror = function(e) {

ajaxRequestComplete = 'err';

copyState();

try {

if (self.onupdate != null && typeof(self.onupdate) == "function") { self.onupdate(); }

} catch (e) {}

if (self.onerror != null && typeof(self.onerror) == "function") {

return self.onerror(e);

} else if (self.onreadystatechange != null && typeof(self.onreadystatechange) == "function") {

return self.onreadystatechange();

}

}

actualXHR.onload = function(e) {

ajaxRequestComplete = 'loaded';

copyState();

try {

if (self.onupdate != null && typeof(self.onupdate) == "function") { self.onupdate(); }

} catch (e) {}

if (self.onload != null && typeof(self.onload) == "function") {

return self.onload(e);

} else if (self.onreadystatechange != null && typeof(self.onreadystatechange) == "function") {

return self.onreadystatechange();

}

}

actualXHR.onprogress = function(e) {

copyState();

try {

if (self.onupdate != null && typeof(self.onupdate) == "function") { self.onupdate(); }

} catch (e) {}

if (self.onprogress != null && typeof(self.onprogress) == "function") {

return self.onprogress(e);

} else if (self.onreadystatechange != null && typeof(self.onreadystatechange) == "function") {

return self.onreadystatechange();

}

}

if (onnew && typeof(onnew) == "function") { onnew(this); }

}

window.XMLHttpRequest = NewXHR;

}

window.ajaxRequestComplete = 'no';//Make as a global javascript variable

window.ajaxRequestStarted = 'no';

startTracing();

或者的Javascript二:

var startTracing = function (onnew) {

window.ajaxRequestComplete = 'no';//Make as a global javascript variable

window.ajaxRequestStarted = 'no';

XMLHttpRequest.prototype.uniqueID = function() {

if (!this.uniqueIDMemo) {

this.uniqueIDMemo = Math.floor(Math.random() * 1000);

}

return this.uniqueIDMemo;

}

XMLHttpRequest.prototype.oldOpen = XMLHttpRequest.prototype.open;

var newOpen = function(method, url, async, user, password) {

ajaxRequestStarted = 'open';

/*alert(ajaxRequestStarted);*/

this.oldOpen(method, url, async, user, password);

}

XMLHttpRequest.prototype.open = newOpen;

XMLHttpRequest.prototype.oldSend = XMLHttpRequest.prototype.send;

var newSend = function(a) {

var xhr = this;

var onload = function() {

ajaxRequestComplete = 'loaded';

/*alert(ajaxRequestComplete);*/

};

var onerror = function() {

ajaxRequestComplete = 'Err';

/*alert(ajaxRequestComplete);*/

};

xhr.addEventListener("load", onload, false);

xhr.addEventListener("error", onerror, false);

xhr.oldSend(a);

}

XMLHttpRequest.prototype.send = newSend;

}

startTracing();

然后检查java代码中状态变量ajaxRequestStarted, ajaxRequestComplete的状态,可以确定ajax是否已启动或完成。

现在我有办法等到一个Ajax是完整的,我还可以找到,如果阿贾克斯在一些行动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值