NightmareJS多次评估 [英] NightmareJS multiple evaluations

查看:107
本文介绍了NightmareJS多次评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行一个评估时,NightmareJS效果很好,但是当我与页面进行交互时,随着事情的进行,我需要进行更多的评估.但是,使用文档我尝试了一个简单的链式评估示例,但出现错误:

NightmareJS works great when I am running one evaluation, but as I interact with the page I need to do more evaluations as things pass. However using the docs I tried a simple sample of chaining evaluations and I get an error:

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(...).然后(...).wait不是函数

TypeError: nightmare.goto(...).wait(...).type(...).click(...).wait(...).evaluate(...).then(...).wait is not a function

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

In this case I added a wait before the next evaluation in case I needed to let the system wait for a complete but still it is not working.

推荐答案

问题是evaluate()返回

The thing is that evaluate() returns a Promise, which is a Javascript thing and not a Nightmare thing.

因此Promise具有thencatch等方法,但是显然没有wait方法.

So a Promise has a then and catch, among others, methods, but clearly does not have a wait method.

我有事答案和资源可以帮助您更好地理解该概念.

I thing this answer and this resource can help you understand the concept a little better.

将该概念应用于您的方案,代码将如下所示

Apply the concept to your scenario, the code would look like this

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回调中进行的.

Notice how the second call to evaluate is inside the first then callback.

这篇关于NightmareJS多次评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆