我正在编写一个验证脚本,使用JQuery设置字段,然后验证它们对JSON blob的持久性。对于文本字段没有问题,但对于需要等待时间的日历,我遇到了麻烦。注意:我不使用Selenium,因为它的目标是创建一个更快的替代品。
示例代码: // setCalendars是一个jQuery包装器
test1 = function()
{
loadPage();
setTimeout(function()
{
setCalendars(fromDate, toDate);
setTimeout(function()
{
assertEqual(fromDate, blob.fromDate);
assertEqual(toDate, blob.toDate);
}
, INTERVAL);
}
, INTERVAL);
}问题是,在设置日历字段之前,test2、test3就开始执行了,这就造成了混乱。
请指教
发布于 2013-12-05 12:46:09
我强烈推荐Tim Caswell的库Step,它是专门为序列化回调执行而设计的。
你上面的代码会变成类似这样的代码:
test1 = function()
{
Step(
loadPage,
function () {
setCalendars(fromDate, toDate);
this(); // triggers the next step
},
function () {
assertEqual(fromDate, blob.fromDate);
assertEqual(toDate, blob.toDate);
}
)
}使用Step,您可以仔细控制何时执行的内容。
如果将代码修改为在每个步骤完成时进行回调,则可以将代码修改为如下所示:
test1 = function()
{
Step(
loadPage(this),
function () {
setCalendars(fromDate, toDate, this);
},
function () {
assertEqual(fromDate, blob.fromDate);
assertEqual(toDate, blob.toDate);
}
)
}注意,Step上下文中的this是触发序列中的下一个“步骤”的回调。
希望这能有所帮助,
Aaron
https://stackoverflow.com/questions/20391223
复制相似问题