Using CasperJS within a regular JavaScript file

549 views Asked by At

I am trying to create a NodeJS program that utilizes CasperJS within it. I have run into the error that the module 'casper' cannot be found. As a result, I have tried to npm install spooky --save as I have read around that it is a driver, but I am still getting the same error as I was getting before.

Also, before trying to install SpookyJS, I tried

 var phantom = require('phantom');

 phantom.casperPath = '/path/to/casperjs';
 phantom.injectJs(phantom.casperPath + '/bin/bootstrap.js');

Which then gave me the error that injectJs is not a function. Any and all help appreciated.

2

There are 2 answers

5
Badacadabra On BEST ANSWER

First of all, to contextualize a bit, here is an important reminder for everyone:

While CasperJS is installable via npm, it is not a NodeJS module and will not work with NodeJS out of the box. You cannot load casper by using require(‘casperjs’) in node. Note that CasperJS is not capable of using a vast majority of NodeJS modules out there. Experiment and use your best judgement.

This is where SpookyJS comes into play... But how to make it work? (I assume you are on Linux.)

1. Make sure you have the right environment

  • Node.js >= 0.8
  • PhantomJS >= 1.9
  • CasperJS >= 1.0

Note: SpookyJS works on my computer (Arch Linux) and I have the following setup:

node --version ---> v7.7.4
npm --version ---> 4.4.4
phantomjs --version ---> 2.1.1
casperjs --version ---> 1.1.3

PhantomJS and CasperJS are installed globally.

2. Install SpookyJS locally (and its dependency: tiny-jsonrpc)

Create an empty directory and run npm i spooky tiny-jsonrpc inside. We do not need a package.json here, so you can forget about --save or --save-dev.

3. Test the given example

If SpookyJS is installed, you should have a local node_modules directory. Now, try to run the following command:

node node_modules/spooky/examples/hello.js

If you get "Hello, from Spooky the Tuff Little Ghost - Wikipedia", congrats! You can now integrate SpookyJS in your project, but you will have to respect the syntax presented in hello.js...

2
Dinesh undefined On

you can't include js files to phantom. you need to open a page and then you can includejs into it. page will accept js files. not phantom.

Please refer this

includeJs(url, callback) {void}

Includes external script from the specified url (usually a remote location) on the page and executes the callback upon completion.