[英]NightmareJS multiple evaluations
當我運行一個評估時,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(.. 。)。等待不是一個功能
在這種情況下,我在下一次評估之前添加了一個等待,以防我需要讓系統等待完成,但仍然無法正常工作。
問題是evaluate()
返回一個Promise ,它是一個Javascript的東西,而不是一個夢魘的東西。
所以Promise有一個then
和catch
等方法,但顯然沒有wait
方法。
將概念應用於您的場景,代碼將如下所示
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
回調。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.