Skip to content

Instantly share code, notes, and snippets.

@achudars
Created March 8, 2015 10:51
Show Gist options
  • Save achudars/606f83fecf5a1e9e8717 to your computer and use it in GitHub Desktop.
Save achudars/606f83fecf5a1e9e8717 to your computer and use it in GitHub Desktop.
CasperJS capture
var searchAttributes = ['form[action="/search"]','[class*="search"]','[name*="search"]','[id*="search"]','[type="search"]','[placeholder*="search"]'];
/*console.log('Loading a web page');
var page = require('webpage').create(),
system = require('system'),
t;
var url = 'http://localhost:8080/interactions/search.html';*/
/*t = Date.now();
page.open(url, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading ' + url);
console.log('Loading time ' + t + ' msec');
}
var fs = require('fs');
fs.write(myfile, myData, 'w');
page.render('example.png');
phantom.exit();
});
console.log("loaded");*/
//casperjs
var casper = require('casper').create({
clientScripts: []
//verbose: true,
//logLevel: "debug"
});
var url = 'http://localhost:8080/interactions/search.html';
var url = 'http://www.amazon.co.uk/';
var url = 'http://www.google.com/';
var links = [];
var writeToFile = function(contents) {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var myfile = "data-"+year + "-" + month + "-" + day+".html";
var SCXML_stateOpen = '<state id="'+"TEST_ID"+'">';
var SCXML_transition = '<transition event="'+"TEST_EVENT"+'" target="'+"TEST_TARGET_ID"+'"/>';
var SCXML_datamodelOpen = '<datamodel>';
var SCXML_data = contents;
var SCXML_datamodelClose = '</datamodel>';
var SCXML_stateClose = '</state>'
var SCXML = SCXML_stateOpen + SCXML_transition + SCXML_datamodelOpen + SCXML_data + SCXML_datamodelClose + SCXML_stateClose;
try {
var fs = require('fs');
fs.write(myfile, SCXML, 'w');
} catch(e) {
console.log("Error when writing to file. Details: " + e);
} finally {
console.log("Writing to file was successful.");
}
}
var getAllNodes = function(that) {
var items = that.evaluate(function () {
var nodes = $(n).closest('form');
return [].map.call(nodes, function(node) {
return node.outerHTML;
});
});
// that.echo(items);
return items;
}
var getLinks = function() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href');
});
}
casper.start(url, function() {
// inject jQuery to simplify selectors, especially fetching parent nodes
this.page.injectJs('scripts/jQuery-2.1.3.min.js');
// get current URL
console.log(this.getCurrentUrl());
// STEP #1: get DOM of URL
console.log("STEP #1");
var data = this.evaluate(function() {
return document;
});
var dataAsHTML = data.all[0].outerHTML;
/*if (this.exists('select,form')) {
this.echo('form heading exists');
}*/
// STEP #2: find {SEARCH}
console.log("STEP #2");
// search for 'casperjs' from google form
this.fill('form[action="/search"]', { q: 'casperjs' }, true);
//getAllNodes(this).fill('form[action="/search"]', { q: 'casperjs' }, true);
console.log("STEP #3");
// STEP # : write to file
//writeToFile(getAllNodes(this));
//writeToFile(dataAsHTML);
});
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
});
casper.run(function() {
// echo results in some pretty fashion
this.echo(links.length + ' links found:');
this.echo(' - ' + links.join('\n - ')).exit();
});
//casper.run();
/*==============================================================================*/
/* Casper generated Wed Feb 18 2015 20:39:50 GMT+0000 (GMT Standard Time) */
/*==============================================================================*/
casper.test.begin('Resurrectio test', function(test) {
casper.start('http://www.amazon.co.uk/');
casper.waitForSelector("form[name=site-search] input[name='field-keywords']",
function success() {
test.assertExists("form[name=site-search] input[name='field-keywords']");
this.click("form[name=site-search] input[name='field-keywords']");
},
function fail() {
test.assertExists("form[name=site-search] input[name='field-keywords']");
});
casper.waitForSelector("input[name='field-keywords']",
function success() {
this.sendKeys("input[name='field-keywords']", "book\r");
},
function fail() {
test.assertExists("input[name='field-keywords']");
});
casper.waitForSelector("form[name=site-search] input[type=submit][value='Go']",
function success() {
test.assertExists("form[name=site-search] input[type=submit][value='Go']");
this.click("form[name=site-search] input[type=submit][value='Go']");
},
function fail() {
test.assertExists("form[name=site-search] input[type=submit][value='Go']");
});
/* submit form */
casper.waitForSelector(".botmButtonComponent.botmBuyButtonMiddle",
function success() {
test.assertExists(".botmButtonComponent.botmBuyButtonMiddle");
this.click(".botmButtonComponent.botmBuyButtonMiddle");
},
function fail() {
test.assertExists(".botmButtonComponent.botmBuyButtonMiddle");
});
/* submit form */
casper.run(function() {test.done();});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment