Nightwatch:有没有办法查明executeSync是否执行了javascript

Nightwatch: Is there a way to find out if executeAsync has executed the javascript?

本文关键字:是否 executeSync 执行 javascript 有没有 Nightwatch      更新时间:2023-09-26

我正在使用Nightwatch编写浏览器自动化。我对守夜命令的executeSync功能有问题。

executeSync:的守夜文档

在页面中注入一段JavaScript,以便在当前选定帧的上下文。执行的脚本是假设是异步的,并且评估脚本的结果是返回给客户端。

异步脚本命令不能跨越页面加载。如果卸载事件在等待脚本结果时被激发,错误应为返回给客户端。

this.demoTest = function (browser) {
   browser.executeAsync(function(data, done) {
     someAsyncOperation(function() {
       done(true);
     });
   }, [imagedata], function(result) {
     // ...
   });
 };

最后一个可选参数应该是一个函数,它将在异步任务完成时调用。

如何检查异步任务是否已开始执行?我想在浏览器执行异步任务的Javascript主体后立即执行一些操作。有没有办法查明executeSync是否已在Nightwatch代码中开始执行?

executeAsync调用保持同步,其行为与执行流中的execute类似。要异步执行某些代码,首先需要用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();
}