我正在使用Nightwatch来编写浏览器自动化。我的Nightwatch命令的executeAsync函数有问题。
用于executeAsync的Nightwatch文档:
在页面中注入一段JavaScript,以便在当前选定框架的上下文中执行。假定执行的脚本是异步的,并且评估脚本的结果将返回给客户端。
异步脚本命令可能不会跨越页面加载。如果在等待脚本结果时触发了卸载事件,则应将错误返回给客户端。
this.demoTest = function (browser) {
browser.executeAsync(function(data, done) {
someAsyncOperation(function() {
done(true);
});
}, [imagedata], function(result) {
// ...
});
};
当异步任务完成时,将调用最后一个可选参数,该参数应该是一个函数。
如何检查异步任务是否已开始执行?我想在浏览器执行异步任务的Javascript主体后立即执行操作。有没有办法找出executeAsync是否已在Nightwatch代码中开始执行?
该executeAsync
呼叫将被保持同步和行为像execute
在excution流。要异步执行某些代码,您首先需要使用放下脚本,execute
然后使用来等待结果executeAsync
。
这是一个例子:
'Demo asynchronous script' : function (client) {
client.timeoutsAsyncScript(10000);
client.url('http://stackoverflow.com/');
// execute a piece of script asynchroniously
client.execute(function(data) {
window._asyncResult = undefined;
setTimeout(function(){
window._asyncResult = "abcde";
}, 2000);
}, ["1234"]);
// execute a task while the asynchroniously script is running
client.assert.title('Stack Overflow');
// wait for the asynchronous script to set a result
client.executeAsync(function(done) {
(function fn(){
if(window._asyncResult !== undefined)
return done(window._asyncResult);
setTimeout(fn, 30);
})();
}, [], function(result) {
// evaluate the result
client.assert.equal(result.value, "abcde");
});
client.end();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句