Created
September 29, 2015 04:57
-
-
Save dbaba/7d6e96f8d8ebca602306 to your computer and use it in GitHub Desktop.
sinon-chrome0.2.1 with PhantomJS 2.0.0 workaround for an issue where PhantomJS ignores chai's AssertionError.
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
// sinon-chrome (https://github.com/vitalets/sinon-chrome/blob/master/example/test/beforeeach.js) | |
var fs = require('fs'); | |
var page; | |
var beforeLoadFn; | |
beforeEach(function() { | |
page = require('webpage').create(); | |
/** | |
* evaluate a function in the page | |
* @param {function} func the function to evaluate | |
* @param {...} args function arguments | |
* @return {*} the function call result | |
*/ | |
page.evaluate = function (func, args) { | |
var str, arg, argType, i, l; | |
if (!(func instanceof Function || typeof func === 'string' || func instanceof String)) { | |
throw "Wrong use of WebPage#evaluate"; | |
} | |
str = 'function() { try { return (' + func.toString() + ')('; | |
for (i = 1, l = arguments.length; i < l; i++) { | |
arg = arguments[i]; | |
argType = detectType(arg); | |
switch (argType) { | |
case "object": //< for type "object" | |
case "array": //< for type "array" | |
str += JSON.stringify(arg) + "," | |
break; | |
case "date": //< for type "date" | |
str += "new Date(" + JSON.stringify(arg) + ")," | |
break; | |
case "string": //< for type "string" | |
str += quoteString(arg) + ','; | |
break; | |
default: // for types: "null", "number", "function", "regexp", "undefined" | |
str += arg + ','; | |
break; | |
} | |
} | |
// re-throw the caught error as Error type since AssertionError type is silently ignored by phantomjs. why? | |
str = str.replace(/,$/, '') + '); } catch (e) { throw new Error(e); } }'; | |
return this.evaluateJavaScript(str); | |
}; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
// listen page.onError to catch assertions | |
page.onError = function(msg, trace) { | |
var msgStack = [msg]; | |
if (trace && trace.length) { | |
msgStack.push('TRACE:'); | |
trace.forEach(function(t) { | |
msgStack.push(' -> ' + t.file + ': ' + t.line + (t.function ? ' (in function "' + t.function +'")' : '')); | |
}); | |
} | |
// we need try..catch here as mocha throws error that catched by phantom.onError | |
try { | |
mocha.throwError(msgStack.join('\n')); | |
} catch(e) { } | |
}; | |
page.onInitialized = function() { | |
page.injectJs(node_modules + 'chai/chai.js'); | |
page.injectJs(node_modules + 'sinon/pkg/sinon.js'); | |
page.injectJs(node_modules + 'sinon-chrome/chrome.js'); | |
page.injectJs(node_modules + 'sinon-chrome/src/phantom-tweaks.js'); | |
page.evaluate(function() { | |
assert = chai.assert; | |
// for emulating click | |
clickEvent = document.createEvent('MouseEvents'); | |
clickEvent.initMouseEvent('click', true); | |
}); | |
// call additional function defined in tests | |
if (beforeLoadFn) { | |
beforeLoadFn(); | |
} | |
}; | |
}); | |
afterEach(function() { | |
page.close(); | |
beforeLoadFn = null; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment