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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With