Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I use jQuery in CasperJS?

Tags:

casper.start(URL, function() {      casper.page.injectJs('C:/Users/Mike/Documents/n1k0-casperjs-bc0da16/jquery-1.10.2.min.js');     var names = $('span.author-name');     this.echo(names);     this.exit(); } 

ReferenceError: Can't find variable: $

What do I do? I've tried this too when creating the casper instance:

var casper = require('casper').create({      // I've tried both commented lines below      // clientScripts: ['C:/Users/Mike/Documents/n1k0-casperjs-bc0da16/jquery-1.10.2.min.js']     // clientScripts: ['includes/jquery-1.10.2.min.js'] }); 
like image 277
Michael Yaworski Avatar asked Jul 25 '13 14:07

Michael Yaworski


People also ask

Can we use jQuery in Chrome extension?

JQuery: jQuery isn't necessary, But it will be useful in chrome extension at some points like logging in the URL or etc. First, download a version of jQuery from the jQuery CDN and put it in your extension's folder, and add the name of the minified version of jquery. To load it, add it to manifest.

Can I use both JavaScript and jQuery together?

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier.

Can I put JavaScript in jQuery?

Yes, they're both JavaScript, you can use whichever functions are appropriate for the situation. Show activity on this post. That kind of lower-level hacking around the DOM, messing with the inline handlers, and cross-browser quirks thereof, is precisely the reason jQuery was born.

Where do we use jQuery?

JQuery can be used to develop Ajax based applications. It can be used to make code simple, concise and reusable. It simplifies the process of traversal of HTML DOM tree. It can also handle events, perform animation and add ajax support in web applications.


1 Answers

You have evaluate the jQuery code in the browser context using casper.evaluate

execute code as if you were using the browser console.

var nameCount = this.evaluate(function() {     var names = $('span.author-name')     return names.length; }); this.echo(nameCount); 
like image 106
jantimon Avatar answered Oct 14 '22 13:10

jantimon