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

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

我正在使用 Nightwatch 编写浏览器自动化程序。我遇到 Nightwatch 命令的 executeAsync 函数问题。

executeAsync 的 Nightwatch 文档:

Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame. The executed script is assumed to be asynchronous and the result of evaluating the script is returned to the client.

Asynchronous script commands may not span page loads. If an unload event is fired while waiting for a script result, an error should be returned to the client.

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

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

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

executeAsync 调用保持同步,并且在执行流程中表现得像 execute。 要异步执行一些代码,您首先需要使用 execute 启动脚本,然后使用 executeAsync.

等待结果

这是一个例子:

'Demo asynchronous script' : function (client) {
  client.timeoutsAsyncScript(10000);
  client.url('http://whosebug.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();
}