2013-04-12 27 views
5

benim testinde jQuery enjekte çalışıyorum ama aşağıdaki hatayı alıyorum:

ReferenceError: değişken bulunamıyor: Bu raylar üzerinde yakut olduğu $

Uygulama test etmeye çalışıyorum, WEBrick üzerinde çalışıyor. İşte tüm kodu: sen

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.

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

yüklenen sayfasına bir referansı almak için evaluate() kullanmak gerekiyor gibi not,

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

cevap

14

görünüyor belgelerine olduğunu yalnızca basit nesneleri döndürebilirsiniz, bu nedenle jQuery nesnelerine değerlendirmenin dışında erişemezsiniz (yani, bir JS nesnesini iade edemezsiniz), bu nedenle test edilmek için gerekenleri geri döndürmeniz gerekir, örneğin aşağıdaki

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

Sorunun bu inanmıyorum. Ben yanlış yol büyü DO ben hatayı alıyorum: Geçerli koduyla alamıyorum jquey-1.9.1.min.js istemci tarafında, enjekte edilemedi. – Cailen

+0

@Cailen, yeni cevap –

+0

TEŞEKKÜRLER koymak! Değerlendirme() içinde sarmak doğru yaklaşımdır. – Cailen

İlgili konular