Installing CasperJS on Windows: How to do it correctly?
Asked Answered
T

8

35

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.

Thinkable answered 15/2, 2013 at 12:2 Comment(3)
You need to give more info than just "nothing happens". Many Windows users follow the steps in casperjs.org/installation.html#windows and happily use CasperJS without problems.Heuser
@ Ariya Hidayat Wel thanks for the quick response i followed the information on that page and when i try to run casperjs i get un error that says "Phantomjs is not recognized as an internal or external command, operable program or batch file" where am i going wrong please?Thinkable
I think this question is correctly answered. Please consider choosing an answer that helped you.Grane
D
65

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.

Dimphia answered 11/7, 2013 at 3:49 Comment(7)
I should note since I called it poor documentation, it looks like they made a couple of improvements in the install instructions at that link since I originally posted this answer.Dimphia
that is nice thanks. I got to phantomjs page that just like you said, they assume you already install phantomjs. because they left out that instructions, i got error that phantomjs is not recognized internal or external command.Buonomo
Really helpful, thumbs upRefugee
using phantomjs from nuget, casperjs 1.0.2 from website, i had to add the 4 lines to bootstrap.js as described here: github.com/n1k0/casperjs/issues/1150Thinker
With phantomjs-2.1.1, I had to set the PATH to C:\phantomjs\binPneumonoultramicroscopicsilicovolcanoconiosis
With latest phantomjs and casperjs PATH should contain ;C:\phantomjs\bin;C:\casperjs\binSteersman
Install Python as well.Grateful
T
9

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

Theocritus answered 30/10, 2014 at 9:54 Comment(0)
V
7

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.

Vistula answered 31/10, 2013 at 17:30 Comment(3)
Are you actually using version 1.1.0-beta3? I don't think it's been officially released yet, so you'll have to download the master branch off the Github page. It should include a casperjs.exe file in the bin directory.Joceline
Hi Hexid - good advice, I downloaded 1.1.0-beta3 from casperjs.org. unfortunately there was no .exe file (could be because it is still beta). There was a .bat file located in casperjs\batchbin. Luckily casperjs still worked.Vistula
casperjs.org only has 1.1-beta1. You'd have to download it from github.com/n1k0/casperjsJoceline
T
4

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.

Thinkable answered 17/2, 2013 at 10:41 Comment(1)
Please add this as an edit to your question instead of answering it with an answer. Also consider selecting an answer to close this question.Grane
S
3

Many of the other answers are out of date. Since it seems like the correct install process keeps changing all the time, I suspect my answer will be out of date also in a few days, but as of March 19, 2015, this is the answer. The other answers, even highly upvoted ones, do not work, so don't waste your time. Good luck. Casperjs seems quite good.

If you download PhantomJS 2.0 and casperjs 1.1-beta3 zip file and put the batchbin directory in the path and put phantom exe in that same folder, then run casperjs, on Windows 8.1 I get the error "CasperJS needs PhantomJS v1.x".

So, I got the latest casperjs direct from github, which has no batchbin directory, so I put just the C:\casperjs\bin directory in the path instead and this worked (sort of, I mean it seems to work well enough for me - although running casperjs c:\casperjs\tests\selftest.js seems to have a number of failing tests).

So to recap, the bad news is only the very latest bleeding edge casperjs works. The good news is the install is 1,2,3 simple:

  1. Clone latest casperjs from github into c:\casperjs.
  2. Copy phantomjs.exe (ver 2.0) to c:\casperjs\bin
  3. Add c:\casperjs\bin to your windows path
Shipmaster answered 19/3, 2015 at 18:15 Comment(2)
I don't know if this is necessary or not, but I also put Python34 into my path which seemed to have a positive effect. Thanks for the updated instructions.Ossetic
This worked for me today with casperjs 1.1.1 and phantomjs 2.1.1. Nowhere can I find step 2 on the casper website, but nothing works until you do that.Shanon
H
2

Ok guys. So I think this thread needs refreshing for 2018!

So with npm, the effort is reduced significantly. Given that you have npm installed, open terminal and go to your project:

cd your_project_name

Now install casperjs. Use --save-dev, --save, -g or none as needed:

npm install --save-dev casperjs

Now install phantomjs. To do this, you should install phantomjs-prebuilt, because PhantomJS team changed their package name:

npm install --save-dev phantomjs-prebuilt

Run your spec:

casperjs your_spec_name.js

Hachmann answered 8/3, 2018 at 12:11 Comment(1)
I had to copy node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe to node_modules\casperjs\bin, and then call casperjs using the path to it (node_modules\casperjs\bin\casperjs MyTestScript.js)Predella
M
1

Just in case you're using a notebook with dual graphic cards like I do: choose one of them to prevent issues. This article helped me out:

casperJS not finishing on windows

the casperJS documentation is pretty clear about it but I had no clue what to do until I read the notice above.

Margetts answered 6/3, 2015 at 14:1 Comment(0)
R
1

Fast forward to 2015... 5-steps win7 howto:

  1. choco: PS me> iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
  2. git: choco install git -y
  3. phantomjs 2.0: choco install phantomjs -y
  4. casperjs source: git clone https://github.com/n1k0/casperjs -b phantomjs-2
  5. Add the location of casperjs/bin to PATH

Done. You can now casperjs --version and live happily ever after.

Reverberate answered 4/11, 2015 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.