Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing CasperJS on Windows: How to do it correctly?

I know there is a documentation from CasperJS website about how to install CasperJS on Windows, but bear with me these guys only explained for the pros only.

If you are new to all this CasperJS and PhantomJS world, you dont stand a chance to understand not even spending two days trying to search the net for a better explanation.

I am working on a project that requires a screenshot of each website listed on my project website, looking around i found out that PhantomJS would be great for this task (website screenshot).

I downloaded PhantomJS and and pasted its phantomjs.exe file in system32 which is working well when i use cmd.exe to send command.

I even managed to grab screenshot with the phantomjs.exe only. but my problem came when i noticed the the workload could be easier when these two work together (CasperJS and PhantomJS) as i can even be able to reduce the size of the screenshot when using CasperJS.

In fact the only use that i want CasperJS for is the limiting of the shot size but since yesterday i have been trying to figure out how to make CasperJS work on Windows but with no avail.

I have downloaded CasperJS and tried to install it in many ways also trying to follow the documentation but nothing.

I changed the CasperJS folder name from its download name to CasperJS as the documantation suggest but when i check in the cmd trying to call some commands, nothing happens.

Anyway to cut the story shot can anyone help me in simple terms considering that i am a newbie to explain how CasperJS can be installed on window or if possible with PhantomJS only how can i re-size the iamge the the program produces lets say if i want a 960 to 400px.

like image 331
user2075354 Avatar asked Feb 15 '13 12:02

user2075354


4 Answers

Poor documentation for windows. http://casperjs.org/installation.html#windows

It starts off assuming you have already installed without telling you how to install.

So here it is if anyone else is confused about this. There is no actual install. It's just extracting zip contents to the right place.

  1. download phantomjs for windows from the phantomjs site (it's a zip with binary inside)
  2. extract the contents to C:\phantomjs
  3. download the casperjs zip file from the casperjs website
  4. extract the contents to C:\casperjs
  5. Now you can add the following to the end your system or user PATH variable

    ;C:\phantomjs;C:\casperjs\batchbin

  6. restart cmd.exe to pick up the new path variable or logout/login if you are running Console2 or Conemu terminal emulator (they won't pick up new paths by a simple close and re-open)

Now in the docs it says to run it like this

casperjs.bat myscript.js

Actually since both phantomjs.exe and casperjs.bat are now in the system PATH you can leave off the extension like this.

casperjs myscript.js

And when running phantomjs.exe just run

phantomjs

One more thing. It really doesn't matter where you install as long as you add that path to the system PATH. I installed to C:\usr\phantomjs and C:\usr\casperjs.

like image 53
isimmons Avatar answered Oct 24 '22 08:10

isimmons


I itemize below the method that has served my needs on both my personal Windows and Ubuntu work PC. DO note that my method doesn't fiddle with PATH settings but involves a command you could save somewhere & copy and paste as needed:

Step 1: Gather the prerequisites

  • Download the casperjs and phantomjs versions you want to use
  • Make a directory to contain the things I want to list
  • Extract the downloaded phantomjs & copy its executable into the directory of step b
  • Extract casperjs and rename its folder to casperjs
  • Copy the renamed casperjs folder to the directory of step b
  • Create and save a file config.json to the directory of step b
  • config.json should contain phantomjs configurations as found here: http://phantomjs.org/api/command-line.html

Step 2: Running your script

  • Whenever you want to work with a file, follow the Step 1 details above
  • The next step assumes that you are in the directory created in step b of Step 1 also have a file named first.js
  • On Windows: phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js
  • On Ubuntu: ./phantomjs --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli first.js

Experimental config.js and first.js are listed below:

config.json

{"sslProtocol": "any", "cookiesFile": "biscuit", "maxDiskCacheSize": 1000, "diskCache": true}

first.js

var casper = require('casper').create({
    pageSettings: {
        loadImages: false,
        loadPlugins: true,
        userAgent: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
    }
});
var url = "http://casperjs.org/";

casper.start(url).wait(60 * 1000 * 1, function() {
     casper.echo('1 min has passed');
     casper.capture('casperjs.png');
     casper.exit();
});

casper.run();

Addendum: download and save the details of screenshots.js and run it as

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs --cli screenshots.js http://phantomjs.org

Run Tests: download & save the details of picturefill-test.js and run it as

phantomjs.exe --config=config.json casperjs/bin/bootstrap.js --casper-path=casperjs test --cli picturefill-test.js

like image 9
iChux Avatar answered Oct 24 '22 10:10

iChux


As of CasperJS 1.1.0-DEV Beta 3 you should use this PATH: C:\casperjs\batchbin even though the documentation found here states you need to use C:\casperjs\bin

The reason to this is because C:\casperjs\batchbin includes a .BAT which C:\casperjs\bin does not include anything except some.js files.

like image 7
Bashir Osman Avatar answered Oct 24 '22 10:10

Bashir Osman


after a 3days work, i managed to get it work the problem was with the path and the installation of phantom. i had made the path to a folder but the installation was pointing to the exe file all i had to do was to put the exe file into a folder phantomjs and that was it thank for your help all.

like image 4
user2075354 Avatar answered Oct 24 '22 09:10

user2075354