Numerous NightmareJS ratings

NightmareJS works great when I run one assessment, but when I interact with the page, I need to do more assessments as I progress. However, using the docs, I tried a simple example of the grading chain, and I get an error message:

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 (...). appreciate (...). (...). wait is not a function

In this case, I added a wait before the next evaluation in case I need the system to wait for completion, but still it does not work.

+6
source share
1 answer

The thing is, evaluate() returns Promise , which is a Javascript subject, not a nightmare.

Thus, Promise has then and catch methods, among other things, but obviously does not have a wait method.

I believe this , and this resource will help you understand the concept a little better.

Apply the concept to your scenario, the code will 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 that the second call to evaluate is inside the first callback then .

+6
source

Source: https://habr.com/ru/post/1011781/


All Articles