当我运行一个评估时,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 不是函数
在这种情况下,我在下一次评估之前添加了一个等待时间,以防我需要让系统等待完成但仍然无法正常工作。
最佳答案
事情是 evaluate()
返回一个 Promise ,这是一个 Javascript 的东西,而不是一个 Nightmare 。
所以 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
回调中进行的。
关于javascript - NightmareJS 多重评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292143/