等待Nightwatch.js中的JavaScript条件

问题描述 投票:1回答:1

我是Nightwatch.js的新手,正在从WD.js库中进行一些测试。 WD.js的一个不错的功能是.waitForConditionInBrowser(),它等待任意JavaScript代码段都是真实的。但是,我无法弄清楚如何在Nightwatch.js中以这种方式与JavaScript交互,或者是否有可能(仔细阅读文档后)。这是我想做的一个例子:

假设我正在加载一个页面,大约3秒钟后my_js_var变为5。在WD.js中,它看起来像这样(最多等待5秒):

.get(my_page_url)
.waitForConditionInBrowser('my_js_var === 5', 5000)
// continue if my_js_var === 5 within 5 s

我什至尝试在Nightwatch中做一些更脏的事情,但我完全不应该了解与JS页面的交互如何工作:

.url(my_page_url)
.pause(5000)
.execute(function() {
    return (my_js_var === 5);
})
// continue if my_js_var === 5 within 5 s

但是无论返回什么,它都将继续,并且不清楚结果将到达何处。

Nightwatch中是否有适当的waitFor函数,如果没有,如何在已评估的JavaScript代码上运行基本断言?

nightwatch.js
1个回答
0
投票

已经有一段时间了,但是我以前经常做这样的事情:

exports.command = function(callback) {
  var self = this;
  this.timeoutsAsyncScript(5000, function() {
    this.executeAsync(function(done) {
        if(my_js_var === 5) {
          return my_js_var === 5
        }
        else {
          done();
        }
      },
      [], 
      function(result) {
        if(typeof(callback) === "function") {
          callback.call(self, result);
        }
      });
  });
  return this;
};

timeoutsAsyncScript中的超时使您可以确保合理的等待时间,以便测试应该失败时相应地也会失败。

© www.soinside.com 2019 - 2024. All rights reserved.