我正在使用Nightwatch来编写浏览器自动化。我的Nightwatch命令的executeAsync函数有问题。

execute的Nightwatch文档异步:


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://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();
}

关于javascript - 守夜人: Is there a way to find out if executeAsync has executed the javascript?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37044543/

10-12 22:24