【问题标题】:Nightwatch: Is there a way to find out if executeAsync has executed the javascript?Nightwatch:有没有办法找出 executeAsync 是否执行了 javascript?
【发布时间】:2016-05-05 07:06:55
【问题描述】:

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

executeAsync 的 Nightwatch 文档:

在页面中注入一个sn-p的JavaScript来执行 当前选定帧的上下文。执行的脚本是 假设是异步的并且评估脚本的结果是 返回给客户。

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

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

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

如何检查异步任务是否已开始执行?我想在浏览器执行异步任务的 Javascript 正文后立即做一些事情。有没有办法查明 executeAsync 是否已在 Nightwatch 代码中开始执行?

【问题讨论】:

    标签: javascript selenium nightwatch.js


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      相关资源
      最近更新 更多