gpt4 book ai didi

javascript - NightmareJS 多重评估

转载 作者:数据小太阳 更新时间:2023-10-29 05:14:33 24 4
gpt4 key购买 nike

当我运行一个评估时,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 有 thencatch 等方法,但显然没有 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com