2013-04-12 16 views
5

मैं अपने परीक्षण में jQuery इंजेक्षन करने की कोशिश कर रहा हूँ, लेकिन मैं निम्नलिखित त्रुटि मिलती है:

ReferenceError: चर नहीं मिली: $

यह रेल पर एक गहरे लाल रंग का है आवेदन मैं परीक्षण करने की कोशिश कर रहा हूँ, WEBrick पर चल रहा है। यहाँ कोड के सभी है:

var casper = require('casper').create({ 
    clientScripts: ['jquery-1.9.1.min.js'] 
}); 

//make sure page loads 
casper.start('http://127.0.0.1:3000', function() { 
    this.test.assertTitle('EZpub', 'EZpub not loaded'); 
}); 

//make sure all 3 fridges are displayed 
casper.then(function() { 
    //get fridges 
    var fridges = $('a[href^="/fridges/"]'); 
    this.test.assert(fridges.length == 3, 'More or less than 3 fridge links shown'); 
}); 

casper.run(function() { 
    this.echo('Tests complete'); 
}); 

उत्तर

14

प्रलेखन ऐसा लगता है जैसे आप से evaluate() का उपयोग करने के लिए पेज कि

Note The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened; everytime you pass a closure to evaluate(), you're entering the page and execute code as if you were using the browser console.

casper.then(function() { 
    var fridges = casper.evaluate(function(){ 
     // In here, the context of execution (global) is the same 
     // as if you were at the console for the loaded page 
     return $('a[href^="/fridges/"]'); 
    }); 
    this.test.assert(fridges.length == 3, 'More or less than 3 fridge links shown'); 
}); 

हालांकि भरी हुई है के लिए एक संदर्भ प्राप्त करने की आवश्यकता, कृपया ध्यान दें कि आप केवल साधारण ऑब्जेक्ट्स को वापस कर सकते हैं, इसलिए आप मूल्यांकन के बाहर jQuery ऑब्जेक्ट्स तक नहीं पहुंच सकते हैं (यानी, आप जेएस ऑब्जेक्ट नहीं लौट सकते हैं), इसलिए आपको केवल

जैसे परीक्षण करने की आवश्यकता है,
casper.then(function() { 
    var fridgeCount = casper.evaluate(function(){ 
     // In here, the context of execution (global) is the same 
     // as if you were at the console for the loaded page 
     return $('a[href^="/fridges/"]').length; 
    }); 
    this.test.assert(fridgeCount === 3, 'More or less than 3 fridge links shown'); 
});  
+1

मुझे विश्वास नहीं है कि यह समस्या है। यदि मैं गलत तरीके से वर्तनी करता हूं तो मुझे त्रुटि मिलती है: jquey-1.9.1.min.js क्लाइंट साइड इंजेक्शन करने में विफल रहा, जिसे मैं वर्तमान कोड से नहीं प्राप्त कर रहा हूं। – Cailen

+0

@ कैलेन, एक नया उत्तर –

+0

धन्यवाद! मूल्यांकन में इसे लपेटना() सही दृष्टिकोण है। – Cailen

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