我正在使用 casperJS 来测试一个应用程序。 问题是,我想捕获使用 waitForSelector() 接收我正在等待的元素实际花费了多长时间。
如果我将日志级别设置为调试,它会在控制台中显示我等待出现的元素需要多长时间:
我想获取该值并将其存储在变量中。 CasperJS/PhantomJS 可以实现这一点吗?如果不是,我可以使用什么框架?
最佳答案
自己动手的方法是自己花时间,这应该足够准确:
var start;
casper.then(function(){
start = new Date().getTime();
});
casper.waitFor(...);
casper.then(function(){
this.echo("waitFor() took " + (new Date().getTime() - start) + " ms");
});
当然,这不太可重用。只需注册 "log"
event 会更容易:
casper.on("log", function(logEntry){
if (logEntry.message.indexOf("waitFor() finished in ") === 0) {
var time = parseInt(logEntry.message.match(/\d+/)[0]);
// TODO: do something with the time
}
});
请注意,这是异步的,但您仍然可以从事件处理程序安排步骤(then*
和 wait*
函数)。
如果您也对超时情况感兴趣,那么您可以注册“waitFor.timeout”
事件。
关于javascript - 如何捕获 CasperJS 中 waitFor() 等待的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39962489/