2016-10-27 16 views
6

जब मैं एक मूल्यांकन चला रहा हूं, तो दुःस्वप्न जेएस बहुत अच्छा काम करता है, लेकिन जब मैं पृष्ठ से बातचीत करता हूं तो मुझे चीजों को पारित करने के लिए और अधिक मूल्यांकन करने की आवश्यकता होती है। हालांकि डॉक्स का उपयोग करते हुए मैं मूल्यांकन चेनिंग का एक सरल नमूना की कोशिश की और मैं एक त्रुटि मिलती है:दुःस्वप्न जेएस एकाधिक मूल्यांकन

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

त्रुटि:

लेखन त्रुटि:।। Nightmare.goto (...) प्रतीक्षा (...) प्रकार (...)। क्लिक करें (...)। प्रतीक्षा करें (...)। मूल्यांकन (...)। फिर (...)। प्रतीक्षा एक समारोह नहीं है

इस मामले में मैंने प्रतीक्षा की अगले मूल्यांकन से पहले मुझे सिस्टम को पूर्ण होने की प्रतीक्षा करने की आवश्यकता थी लेकिन फिर भी यह काम नहीं कर रहा है।

उत्तर

6

बात यह है कि evaluate()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 कॉलबैक अंदर है लगेगा।

+0

आपकी मदद के लिए बहुत बहुत धन्यवाद, कुछ और परीक्षण करने के बाद भी मुझे कुछ त्रुटियां मिल रही थी जब तक कि मुझे पता चला कि मैं चाई से अपेक्षा करने के बजाय लोड हो रहा था। –

+0

इसे और अधिक विस्तार से देखने के बाद ऐसा लगता है कि जब मुझे 100 नेस्टेड परीक्षण लिखना है, तो यह प्रबंधित करने के लिए एक गड़बड़ होगी, क्या नेस्टेड evals लिखने के लिए और अधिक सुरुचिपूर्ण है? मैं एक वादा खिलाने की सोच रहा था लेकिन अगर वादा घोंसला होना है तो काम नहीं कर सकता है। –

+0

कुछ और परीक्षणों के बाद मैं निम्नलिखित समाधान के साथ आया हूं: http://stackoverflow.com/questions/40297378/nightmarejs-multiple-reports-from-same-test/40298901#40298901 यह मुझे अलगाव रखने की अनुमति देता है कि मैं अभी के लिए जरूरी है –

संबंधित मुद्दे