2016-10-27 12 views
6

NightmareJSは1つの評価を実行しているときに効果的ですが、私がページとやり取りするときには、物事が進むにつれて評価を増やす必要があります。しかし、私は、連鎖評価の簡単なサンプルを試してみましたが、私はエラーを取得するドキュメントを使用して: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(...)(...)を待つタイプ(...)。(...)。(...)。(...)。(...)をクリックしてください。システムが完全な状態を待つ必要がある場合に備えて、次の評価の前に、まだ動作していません。

答えて

6

事は、evaluate()は、ナイトメアのものではなく、JavaScriptのものであるPromiseを返します。

したがって、プロミスは、thenおよびcatchの方法をとりますが、明らかにwaitメソッドはありません。

私は事物thisと答え、このresourceはあなたが少し良いコンセプトを理解するのを助けることができます。

があなたのシナリオの概念を適用し、コードがevaluateへの2回目の呼び出しは、最初のthenコールバック内でどのようにこの

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)) 
     }) 
    }); 
}); 

お知らせのようになります。

+0

もう少しテストしていただき、ありがとうございました。チャイから期待するのではなく、読み込み中であることが判明するまで、まだいくつかのエラーが出ていました。 –

+0

これをより詳細に見てみると、100個のネストされたテストを書く必要があるときに管理するのが難しいように思えます。ネストされたエバールを書くためにはよりエレガントですか?私は約束を払うことを考えていたが、約束を入れなければならない場合は、うまくいかないかもしれない。 –

+0

もう少しテストした後、私は次の解決策を考え出しました:http://stackoverflow.com/questions/40297378/nightmarejs-multiple-reports-from-same-test/40298901#40298901今必要です。 –