在获取响应之前等待 ajax 请求完成
我对 mvc 和 webtest 非常陌生,所以请耐心等待。
我有一个客户的观点。客户视图调用 document.ready
上的 javascript 函数。此 javascript 调用对客户控制器中的操作执行 ajax post,该操作将 json 返回到原始 javascript,进而更新客户视图。
在我的 webtest 中,我有一个对客户视图的请求,但是响应是在后续 ajax 调用完成并且视图更新之前返回的,因此我没有最新的页面内容。
有没有办法等到后续请求完成,以便我可以分析包含所有数据的整个页面?或者我的方向是错误的?
这是视图:
<script type="text/javascript" language="javascript">
//Set initial state of list during page load
$(document).ready(function() {
ApplyFilters();
})
</script>
调用此 javascript:
function ApplyFilters() {
.....
$.ajax({
type: "POST",
url: "/Customer/FilterList",
datatype: "html",
beforeSend: function() {
//do some stuff
},
success: function(result) {
...
BindCustomrGrid($find(GridClientID), result);
},
error: function(req, status, error) {
// do some stuff
}
});
}
最后是控制器:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult FilterList(string io, string name, string owner)
{
... // some api calls
return this.Json(new { customerList, serviceException });
}
I'm extremely new to mvc and webtest so please bear wtih me here.
I have a customer view. The customer view calls a javascript function on document.ready
. This javascript call performs an ajax post to an action in the customer controller which returns json to the original javascript which in turn updates the customer view.
In my webtest, I have a request to the customer view, but the response is returned before the subsequent ajax call is completed and the view is updated so I don't have the latest page contents.
Is there a way to wait until that subsequent request is completed so I can analyze the full page with all the data? Or am I going about this in the wrong direction?
here is the view:
<script type="text/javascript" language="javascript">
//Set initial state of list during page load
$(document).ready(function() {
ApplyFilters();
})
</script>
Which calls this javascript:
function ApplyFilters() {
.....
$.ajax({
type: "POST",
url: "/Customer/FilterList",
datatype: "html",
beforeSend: function() {
//do some stuff
},
success: function(result) {
...
BindCustomrGrid($find(GridClientID), result);
},
error: function(req, status, error) {
// do some stuff
}
});
}
and finally the controller:
[AcceptVerbs(HttpVerbs.Post)]
public JsonResult FilterList(string io, string name, string owner)
{
... // some api calls
return this.Json(new { customerList, serviceException });
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我决定做的是发布到从 ajax 请求调用的 url,在自定义验证类中反序列化 json 并在那里验证它。
what I decided to do instead was post to the url that is invoked from the ajax request, deserialize the json in a custom validation class and validate it there.
在这种情况下,我强烈推荐 TestPlan 作为测试工具。它的优点是,当您在页面上查找元素时,它会自动等待它们以允许 JavaScript 完成执行。因此,如果您执行以下操作:
它将等待该按钮存在。显然它不会永远等待。在无显示后端中,它将检查是否有任何 JavaScript 正在运行,如果没有,它将立即返回,因为不可能出现某些内容。
In such cases I highly recommend TestPlan as a testing tool. It has the advantage that when you look for elements on the page it automatically waits for them to allow JavaScript to finish executing. So if you do something like:
It will wait until that button exists. Obviously it won't wait forever. And in the display-less back-end it'll check to see if any JavaScript is running, and if not it'll return immediately since it isn't possible for something to appear.
成功函数在请求返回时运行。但是,您在加载 DOM 时启动此 ajax 调用。这很奇怪,通常会在用户做了某件事后或在一段时间后发出一些 AJAX 请求。
如果您想等到整个页面加载完毕后再发出 AJAX 请求,您可以使用:
the succes function runs when the request is returned. however you start this ajax call on loading the DOM. which is odd, one normally would make some AJAX request after the user did something or after a certain amount of time.
if you want to wait till the whole page is loaded before the AJAX request is made you can use: