我有一个像这样定义的听众:
function loadFailed() {
casper.echo("load failed catched");
}
casper.on('load.failed', loadFailed);
... some code
casper.removeListener('load.failed', loadFailed);
//redefine loadFailed function
function loadFailed(e) {
flagLoadFailed = true;
}
b.casper.on('load.failed', loadFailed);
然后我在//中使用了两个函数,而不是用新的函数删除第一个实例。
有没有办法在代码中的某处删除侦听器(并可能还原原始侦听器)?
答案 0 :(得分:1)
你尝试过这样的事情: -
casper.on('load.failed', loadFailed)
...
casper.removeListener('load.failed', loadFailed);
function loadFailed(e) {
...
}
对于测试脚本,您可以使用setup和teardown,例如
casper.test.begin('Missing resources', 1, {
setUp: function () {
// Attach the resource listener
casper.on('resource.received', this.onResourceReceived);
},
tearDown: function () {
// Remove the resource listener
casper.removeListener('resource.received', this.onResourceReceived);
},
test: function (test) {
casper.start(...);
casper.run(function () {
test.done();
});
},
onResourceReceived: function (resource) {
if (resource.stage === 'end' && resource.status === 404) {
require('utils').dump(resource);
}
}
});