Skip to content

Instantly share code, notes, and snippets.

@achudars
Created February 19, 2015 10:47
Show Gist options
  • Save achudars/18eb6c89932ffac69740 to your computer and use it in GitHub Desktop.
Save achudars/18eb6c89932ffac69740 to your computer and use it in GitHub Desktop.
Simple CasperJS Capture of Search Interaction (to be deleted)
//casperjs
var casper = require('casper').create({
pageSettings: {
webSecurityEnabled: false
},
clientScripts: []
//verbose: true,
//logLevel: "debug"
});
// event listeners
casper.on('navigation.requested', function(reurl, navigationType, navigationLocked, isMainFrame) {
console.log('> Trying to navigate to: ' + reurl);
console.log('> Caused by: ' + navigationType);
});
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 getAllParents = function(that) {
var items = that.evaluate(function () {
var nodes = $('[class*="search"],[name*="search"],[id*="search"],[type="search"],[placeholder*="search"],[title*="search"]').closest('form'); // goes up the tree
return [].map.call(nodes, function(node) {
return node.outerHTML;
});
});
//that.echo(items);
return items;
}
var getLinks = function() {
var links = $('div[id*="result"]');
return Array.prototype.map.call(links, function(e) {
//return e.getAttribute('href');
return e.outerHTML;
});
}
casper.start(url, function() {
// inject jQuery to simplify selectors, especially fetching parent nodes
this.page.injectJs('scripts/jQuery-2.1.3.min.js');
//if (this.exists('form')) {
// console.log("form exists");
//this.fillSelectors('form', { 'input:not([type="hidden"])': 'book' }, true);
//this.fillSelectors('form', { '#twotabsearchtextbox': 'book' }, true);
this.sendKeys("input[name='field-keywords']", "book");
this.click("form[name=site-search] input[type=submit][value='Go']");
//}
//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();
this.exit();
});
//casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment