Skip to content

Instantly share code, notes, and snippets.

@Grahack
Last active August 29, 2015 13:56
Show Gist options
  • Save Grahack/8970558 to your computer and use it in GitHub Desktop.
Save Grahack/8970558 to your computer and use it in GitHub Desktop.
// Casper file which dumps a page (and recursively continues).
var utils = require('utils');
// https://github.com/ariya/phantomjs/wiki/API-Reference-FileSystem
var fs = require('fs');
var casper = require('casper').create({
verbose: true,
logLevel: 'error',
pageSettings: {
loadImages: false,
loadPlugins: false,
}
});
casper.start('http://127.0.0.1:8000/site/', function() {
this.echo(this.getTitle());
// pages to check
var link_selector = 'a';
var links_info = this.getElementsInfo(link_selector);
var internal_links = [];
for (var i = 0; i < links_info.length; i++) {
internal_links.push(links_info[i].attributes.href);
}
utils.dump(internal_links);
// whole doc dump for comparison
var suffix = '.dump';
var page_name = 'index';
var current_dump = page_name + suffix;
var previous_dump = current_dump + '.old';
var dump = utils.serialize(this.getElementsInfo('html'));
dump = dump.replace(/\\n/g, "\n");
if (fs.exists(current_dump)) {
if (fs.exists(previous_dump)) {
fs.remove(previous_dump);
}
fs.move(current_dump, previous_dump);
fs.write(current_dump, dump, 'w');
// compare the files?
} else {
fs.write(current_dump, dump, 'w');
}
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment