Monday, September 22, 2014

Introduction with CasperJS

CasperJS is an open source navigation scripting & testing utility written in Javascript for the PhantomJS WebKit headless browser and SlimerJS (Gecko). It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar for doing common tasks such as:
  • defining & ordering browsing navigation steps
  • filling & submitting forms
  • clicking & following links
  • capturing screenshots of a page (or part of it)
  • testing remote DOM
  • logging events
  • downloading resources, including binary ones
    writing functional test suites, saving results as JUnit XML
  • scraping Web contents

Sample code of CasperJS for scraping data where scroll pagination is used.
How To Install : http://mariehogebrandt.se/articles/installing-phantomjs-and-casperjs-on-ubuntu
How To Run : casperjs CasperJS_in_scroll_pagination.js
var casper = require('casper').create({
    verbose: true,
    logLevel: 'error',
    pageSettings: {
        loadImages: false,
        loadPlugins: false,
        userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
    }
});
 
var x = require('casper').selectXPath;
var links = [];
function getLinks() {
    var links = document.querySelectorAll('#content li');
    return Array.prototype.map.call(links, function(e) {
        return e.getAttribute('p');
    });
}
 
casper.start('http://andersonferminiano.com/jqueryscrollpagination/', function() {
    this.scrollToBottom();
});
 
casper.wait(10000, function () {
  var numTimes = 5, page = 1;
    casper.repeat(numTimes, function () {
 this.scrollToBottom();
        casper.wait(10000, function () {
 
 })
 
    })
page=page+1;
})
casper.run(function() {
    // echo results in some pretty fashion
 var html = this.evaluate(function() {
  return document; 
 }); 
    this.echo(html.all[0].outerHTML).exit();
});

No comments:

Post a Comment