Created
April 29, 2015 23:05
-
-
Save rbg246/ede4e895af755c7c8417 to your computer and use it in GitHub Desktop.
Example of Mocha and SpookyJS iterating through a series of pages, running example tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function pageParser() { | |
return { | |
url : window.location.href, | |
title : $('title').text(), | |
description : $('meta[name="description"]').attr('content') | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
var Spooky = require('spooky'); | |
} catch (e) { | |
var Spooky = require('../lib/spooky'); | |
} | |
var links = ['http://www.google.com', 'http://www.richardgaunt.com', 'http://en.wikipedia.org/wiki/Spooky_the_Tuff_Little_Ghost']; | |
var Links = require('array-wrapper'); | |
var links = new Links(links); | |
var builder = function (url, callback) { | |
var spooky = new Spooky({ | |
child: { | |
transport: 'http' | |
}, | |
casper: { | |
logLevel: 'debug', | |
verbose: true, | |
clientScripts: ['./jquery.min.js', './page-tests.js'] | |
} | |
}, function (err) { | |
if (err) { | |
e = new Error('Failed to initialize SpookyJS'); | |
e.details = err; | |
throw e; | |
} | |
spooky.start( | |
url); | |
spooky.then(function () { | |
this.emit('data', this.evaluate(function () { | |
var $ = jQuery; | |
var data = pageParser(); | |
return data; | |
})); | |
}); | |
spooky.run(); | |
}); | |
callback(spooky); | |
return spooky; | |
}; | |
var callback = function (spooky) { | |
spooky.on('error', function (e, stack) { | |
console.error(e); | |
if (stack) { | |
console.log(stack); | |
} | |
}); | |
spooky.on('data', function (data) { | |
console.log(data); | |
if (links.next()) runTests(); | |
}); | |
spooky.on('log', function (log) { | |
if (log.space === 'remote') { | |
console.log(log.message.replace(/ \- .*/, '')); | |
} | |
}); | |
}; | |
var runTests = function () { | |
var testName = 'testing - ' + links.get(); | |
describe (testName, function () { | |
this.timeout(30000); | |
beforeEach (function () { | |
}); | |
it ('should work', function (done) { | |
var spooky = new builder(links.get(), callback); | |
spooky.on('data', function(data) { | |
console.log('data received', data); | |
done(); | |
}); | |
}); | |
}); | |
} | |
describe ('Running Tests on Links', function () { | |
this.timeout(30000); | |
runTests(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment