Casperjs Hello World in 10 Minutes

This article represents instructions on how to get started with programming casperjs scripts within 10 minutes. In this article, the sample program is written for scraping Amazon.com homepage and printing title of the page.

One of the biggest issues or hurdle, I would say, in getting started with any framework is related with downloading the tool/library, installing and configuring the environment in order to execute the program. Thanks to Docker creators, this is taken care in a very neat manner so much so that one could do following to set up the development environment in literally no time.

  1. Download the image of the development environment.
  2. Create a Docker container using that image in detached mode
  3. Start and attach with the container anytime one want to code and execute the program

Docker Container for Casperjs

For those of you not aware of what are dockers, do check their website, http://www.docker.com. Also, check my previous article on setting up docker on your windows box. In order to get started with Casperjs, first step is to download the image of Casperjs container from following page.

https://github.com/philalex/docker-casperjs

Once the zip file gets downloaded, unzip the file, goto that folder and run following commands to create a container:

# Pull the docker image
docker pull philalex/docker-casperjs
# Command to run the container with image philalex/docker-casperjs
docker run -ti -dP --name casper1 philalex/docker-casperjs

Alternatively, you could just run the following command. It checks if an image is present and the runs the container using that image. In case the image is not present, it downloads the image first and then runs the container.

# Command to run the container with image philalex/docker-casperjs
docker run -ti -dP --name casper1 philalex/docker-casperjs

Once the container is created, execute the following command to start and attach the container:

# Start the container
docker start casper1
# Attach the container
docker attach casper1

Once the container gets started, do the following in the /home folder.

  1. Create the following Casperjs file, namely sample.js
    var casper = require('casper').create();
    casper.start('http://www.amazon.com', function() {
        this.echo(this.getTitle());
    });
    casper.run();
    	
    
  2. Run the following command. The title of amazon.com page is printed.
    casperjs sample.js
    
Ajitesh Kumar
Follow me
Latest posts by Ajitesh Kumar (see all)

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in Javascript, Virtualization, Web. Tagged with , .

Leave a Reply

Your email address will not be published. Required fields are marked *