Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NightmareJS multiple evaluations

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

Error:

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.

like image 864
Helmut Granda Avatar asked Oct 27 '16 18:10

Helmut Granda


1 Answers

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

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

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

like image 161
felipe_gdr Avatar answered Sep 21 '22 12:09

felipe_gdr