javascript - NightmareJS 多重评估

标签 javascript mocha.js chai nightmare

当我运行一个评估时,NightmareJS 工作得很好,但是当我与页面交互时,我需要在事情通过时做更多的评估。但是,使用文档我尝试了一个简单的链接评估示例,但出现错误:

describe('test google search results', function() {
  this.timeout(15000);
  it('should find the nightmare github link first', function(done) {
    var nightmare = Nightmare({show: true})
    nightmare
      .goto('http://google.com')
      .wait(1000)
      .type('form[action*="/search"] [name=q]', 'github nightmare')
      .click('form[action*="/search"] [type=submit]')
      .wait(1000)//.wait('#rcnt')
      .evaluate(function () {
        return document.querySelector('div.rc h3.r a').href
      })
      .then(function(link) {
        console.log("TESTING 1");
        expect(link).to.equal('https://github.com/segmentio/nightmare');
      })
      .wait()
      .evaluate(function () {
        return document.querySelector('div.rc h3.r a').href
      })
      .end()
      .then(function(link) {
        console.log("TESTING 2");
        expect(link).to.equal('https://github.com/segmentio/nightmare');
        done();
      })
  });
});

错误:

TypeError: nightmare.goto(...).wait(...).type(...).click(...).wait(...).evaluate(...).then (...).wait 不是函数

在这种情况下,我在下一次评估之前添加了一个等待时间,以防我需要让系统等待完成但仍然无法正常工作。

最佳答案

事情是 evaluate() 返回一个 Promise ,这是一个 Javascript 的东西,而不是一个 Nightmare 。

所以 Promise 有 thencatch 等方法,但显然没有 wait 方法。

我的事this答案和这个resource可以帮助您更好地理解这个概念。

将这个概念应用到你的场景中,代码看起来像这样

describe('test google search results', function() {
  this.timeout(15000);
  it('should find the nightmare github link first', function(done) {
    var nightmare = Nightmare({show: true})

    nightmare
      .goto('http://google.com')
      .wait(1000)
      .type('form[action*="/search"] [name=q]', 'github nightmare')
      .click('form[action*="/search"] [type=submit]')
      .wait(1000)//.wait('#rcnt')
      .evaluate(function () {
        return document.querySelector('div.rc h3.r a').href
      })
      .then(function(link) {
        console.log("TESTING 1");
        expect(link).to.equal('https://github.com/segmentio/nightmare');

        nightmare.evaluate(function () {
          return document.querySelector('div.rc h3.r a').href
        })
        .end()
        .then(function(link) {
          console.log("TESTING 2");
          expect(link).to.equal('https://github.com/segmentio/nightmare');
          done();
        })

      }).catch(function(error) {
        done(new Error(error))
      })
  });
});

请注意第二次调用 evaluate 是如何在第一个 then 回调中进行的。

关于javascript - NightmareJS 多重评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292143/

相关文章:

javascript - 纽约市与 webpack 4 使用 .tmp 文件夹

javascript - 用 sinon.stub stub $.ajax 的响应,同时保持延迟功能(使用 mocha + testem)

javascript - 监视点击事件调用的函数

javascript - 如何处理嵌套同步 promise

typescript - 如何测试 Mocha & Chai + TypeScript + SystemJS + JSPM + Angular + Three.js

javascript - 无法在 JavaScript 中显示 JSON 对象字段

javascript - 输入类型 ="file"拍照选项

node.js - 使用 Mocha 进行 Node 应用程序测试和 LCOV 报告生成

javascript - JavaScript 变量的范围(XUL 相关)

javascript - 如何将纯 JS 代码转换为 jQuery?