Quaintproject

How to install casperjs on the raspberry pi

Leave a comment


What is CasperJS?

CasperJS is a navigation and testing utility based on phantomJS. phantomJS could be described as a headless browser offering automatic interaction with a website. It’s esspecially useful for testing purposes and development. PhantomJS is based on webkit.

Keep in mind that that casperJs always requires phantomJS. But phantomJs doesn’t require casperJS.

Possibilities?

The possibilities of CasperJS are pretty endless you can do things like

  • content grabbing – why not make a hub for your most favourite topics?
  • check your website for faulty links
  • generating full size! screenshots of your favourite webpage
  • crawl your favourite webblog for keywords and save the links to a file
  • etc.
  • or…

My initial goal was to build a simple flight search without accessing any commercial apis by only using content grabbing. I actually was sick and tired of using engines that list flights by their potential income. I also hate the service fees that are being add towards the end of of the payment process. That’s why I decided to start a private project offering fair pricing and fair logic. …

I’ll present you my solution in a week or two so stay tuned. 😀

Install casperjs

Before we can start with the installation make sure, your raspberry pi is connected to the internet. I am using a PI (B) with a raspbian image, but this guide should also work on a B+ or a Raspberry 2.

At first we have to clone the latest version of casperjs from GitHub. We can do this easily by calling the following command. Please make sure to execute from the home directory, that way we can make sure that the phantomjs and casperjs will both be installed in a directory called casperjs later on.

Lets start by cloning the casperjs from GitHub.

git clone git://github.com/n1k0/casperjs.git

GitCloneCasper

Change into the casperjs directory using the cd prompt:

cd casperjs

As we want to be able to execute casperjs from every where in the system without having to change to this directory we will create a link. It’s similar to Windows system variables.

sudo ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs

Install phantomjs

Clone the latest version

phantomjs is available on bitbucket. You’ll find it here:

Please make sure that your still in the casperjs directory that was created for us previously. That way you can make sure that the installation will work and you can keep everything together in one single directory.
git clone https://github.com/piksel/phantomjs-raspberrypi.git

PhantomJS Git Clone

Don’t worry if this take a few minutes. On my particular installation it took about two minutes to fetch all the necessary data.

Lets make it executable

Before we can make it executable for every user we have to change into the bin directory.

cd phantomjs-raspberrypi/bin

Change into bin directory

Lets change the rights by executing the following command:

sudo chmod +x phantomjs

Chmod PhantomsJS

Create a link for PhantomJs

sudo ln -s /home/pi/casperjs/phantomjs-raspberrypi/bin/phantomjs /bin/phantomjs

SymbolicLinkPhantomJs

Test your installation

Open up a terminal prompt and type in the following two commands after each other.

phantomjs --version
casperjs

As a result you should get an image that looks similar to the following. Your output may vary a bit if you are using a new version of both casper and/or phantomjs. If you get any error make sure that you’ve progressed every single step above.
Test Casperjs PhantomJs Installation

A HelloWorld example

Open up leafpad or any other text editor and paste the following code into the document. What this script will do is that it will call our site and the raspberrypi’s site and then print back the Title of the sites into the console. For faster testing you can just copy and paste the code below. Save your leafpad file as a .js Script like CasperHelloWorld.js directly in your home directory.

HelloWorldScriptLeafpad

var casper = require('casper').create();

casper.start('http://www.quaintproject.wordpress.com/', function() {
    this.echo(this.getTitle());
});

casper.thenOpen('http://raspberrypi.org', function() {
    this.echo(this.getTitle());
});

casper.run();

Then open up a new terminal and make sure that your currently in the home directory and execute the following command.

casperjs CasperHelloWorld.js

Don’t worry if it takes its time until you get a reply. On my test installation it took about 48 seconds till it returned me some result. Internetconnection is of course required, otherwise you will wait forever. I haven’t done any testing with the latest raspberry pi but I’d expect slightly faster results. Tell us about your loading times in the comment box. 😀

HelloWorldResult

Et voila, that’s it for today. I hope that you will find it useful. Like always feel free to comment to share how this article was useful for your project.

Cheers Alex 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s