【问题标题】:NightmareJS multiple evaluationsNightmareJS 多次评估
【发布时间】:2016-10-27 18:53:41
【问题描述】:

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 不是函数

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

【问题讨论】:

    标签: javascript mocha.js chai nightmare


    【解决方案1】:

    问题是 evaluate() 返回一个 Promise,这是一个 Javascript 的东西,而不是一个噩梦的东西。

    因此,Promise 有 then 和 catch 等方法,但显然没有 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 回调中进行的。

    【讨论】:

    • 非常感谢您的帮助,在测试了更多之后我仍然遇到一些错误,直到我发现我正在加载应该而不是从 chai 加载。
    • 在更详细地查看这个之后,当我必须编写 100 个嵌套测试时,似乎管理起来会很混乱,有没有更优雅的嵌套 eval 编写?我正在考虑提供一个承诺,但如果承诺必须嵌套,则可能无法工作。
    • 经过更多测试后,我提出了以下解决方案:stackoverflow.com/questions/40297378/… 它允许我保持我现在需要的分离。
    • Nice @HelmutGranda,我正在使用完全相同的方法使用 Nightmare 执行多个顺序测试
    • 除非噩梦在发布后发生了变化,否则我不相信 .evaluate 会返回承诺。 .evaluate 与所有操作一样,是返回 nightmarejs 实例的包装队列方法。实际上,返回承诺的是 .then 方法(nightmare 的 .then 方法是返回承诺链的包装器)。我想知道是否可以通过以下方式链接 .evaluate: .evaluate(...) .evaluate(...) .then(...)
    猜你喜欢
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多