mocha-phantomjs-core
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -21,34 +21,32 @@ (function(){ | ||
Object.defineProperty(window, 'initMochaPhantomJS', { | ||
value: function () { | ||
// Mocha needs a process.stdout.write in order to change the cursor position. | ||
Mocha.process = Mocha.process || {} | ||
Mocha.process.stdout = Mocha.process.stdout || process.stdout | ||
Mocha.process.stdout.write = function(s) { window.callPhantom({ stdout: s }) } | ||
function shimMochaProcess(M) { | ||
// Mocha needs a process.stdout.write in order to change the cursor position. | ||
M.process = M.process || {} | ||
M.process.stdout = M.process.stdout || process.stdout | ||
M.process.stdout.write = function(s) { window.callPhantom({ stdout: s }) } | ||
window.callPhantom({ getColWith: true }) | ||
} | ||
var origRun = mocha.run, origUi = mocha.ui | ||
mocha.ui = function() { | ||
var retval = origUi.apply(mocha, arguments) | ||
window.callPhantom({ configureMocha: true }) | ||
mocha.reporter = function() {} | ||
return retval | ||
function shimMochaInstance(m) { | ||
var origRun = m.run, origUi = m.ui | ||
m.ui = function() { | ||
var retval = origUi.apply(mocha, arguments) | ||
window.callPhantom({ configureMocha: true }) | ||
m.reporter = function() {} | ||
return retval | ||
} | ||
m.run = function() { | ||
window.callPhantom({ testRunStarted: m.suite.suites.length }) | ||
m.runner = origRun.apply(mocha, arguments) | ||
if (m.runner.stats && m.runner.stats.end) { | ||
window.callPhantom({ testRunEnded: m.runner }) | ||
} else { | ||
m.runner.on('end', function() { | ||
window.callPhantom({ testRunEnded: m.runner }) | ||
}) | ||
} | ||
mocha.run = function() { | ||
window.callPhantom({ testRunStarted: mocha.suite.suites.length }) | ||
mocha.runner = origRun.apply(mocha, arguments) | ||
if (mocha.runner.stats && mocha.runner.stats.end) { | ||
window.callPhantom({ testRunEnded: mocha.runner }) | ||
} else { | ||
mocha.runner.on('end', function() { | ||
window.callPhantom({ testRunEnded: mocha.runner }) | ||
}) | ||
} | ||
return mocha.runner | ||
} | ||
return m.runner | ||
} | ||
} | ||
delete window.initMochaPhantomJS | ||
}, | ||
configurable: true | ||
}) | ||
Object.defineProperty(window, 'checkForMocha', { | ||
@@ -72,2 +70,35 @@ value: function() { | ||
if ('mozInnerScreenX' in window) { | ||
// in slimerjs, we can stub out a setter to shim Mocha. phantomjs 2 fails | ||
// to allow the property to be reconfigured... | ||
Object.defineProperty(window, 'mocha', { | ||
get: function() { return undefined }, | ||
set: function(m) { | ||
shimMochaInstance(m) | ||
delete window.mocha | ||
window.mocha = m | ||
}, | ||
configurable: true | ||
}) | ||
Object.defineProperty(window, 'Mocha', { | ||
get: function() { return undefined }, | ||
set: function(m) { | ||
delete window.Mocha | ||
window.Mocha = m | ||
shimMochaProcess(m) | ||
}, | ||
configurable: true | ||
}) | ||
} else { | ||
Object.defineProperty(window, 'initMochaPhantomJS', { | ||
value: function () { | ||
shimMochaProcess(Mocha) | ||
shimMochaInstance(mocha) | ||
delete window.initMochaPhantomJS | ||
}, | ||
configurable: true | ||
}) | ||
} | ||
// Mocha needs the formating feature of console.log so copy node's format function and | ||
@@ -74,0 +105,0 @@ // monkey-patch it into place. This code is copied from node's, links copyright applies. |
@@ -5,16 +5,27 @@ var | ||
fs = require('fs'), | ||
stderr = system.stderr || system.stdout, | ||
url = system.args[1], | ||
reporter = system.args[2] || 'spec', | ||
config = JSON.parse(system.args[3] || '{}'), | ||
configured = false, | ||
runStarted = false, | ||
isSlimer = 'MozApplicationEvent' in window, | ||
config = {}, | ||
hookData | ||
try { | ||
config = JSON.parse(system.args[3] || '{}') | ||
} catch (e) { | ||
console.log(e) | ||
console.log('Bad JSON options') | ||
phantom.exit(255) | ||
} | ||
if (!url) { | ||
system.stdout.writeLine("Usage: phantomjs mocha-phantomjs-core.js URL REPORTER [CONFIG-AS-JSON]") | ||
phantom.exit(-1) | ||
system.stdout.writeLine("Usage: " + (isSlimer ? 'slimerjs' : 'phantomjs') + " mocha-phantomjs-core.js URL REPORTER [CONFIG-AS-JSON]") | ||
phantom.exit(255) | ||
} | ||
if (phantom.version.major < 1 || (phantom.version.major === 1 && phantom.version.minor < 9)) { | ||
system.stderr.writeLine('mocha-phantomjs requires PhantomJS > 1.9.1') | ||
stderr.writeLine('mocha-phantomjs requires PhantomJS > 1.9.1') | ||
phantom.exit(-2) | ||
@@ -34,3 +45,3 @@ } | ||
if (msg) { | ||
system.stderr.writeLine(msg) | ||
stderr.writeLine(msg) | ||
} | ||
@@ -50,4 +61,4 @@ return phantom.exit(errno || 1) | ||
catch (e) { | ||
system.stderr.writeLine('Error loading hooks: ' + e.message) | ||
phantom.exit(-3) | ||
stderr.writeLine('Error loading hooks: ' + e.message) | ||
phantom.exit(253) | ||
} | ||
@@ -73,3 +84,3 @@ } else { | ||
if (!config.ignoreResourceErrors) { | ||
return system.stderr.writeLine("Error loading resource " + resErr.url + " (" + resErr.errorCode + "). Details: " + resErr.errorString) | ||
return stderr.writeLine("Error loading resource " + resErr.url + " (" + resErr.errorCode + "). Details: " + resErr.errorString) | ||
} | ||
@@ -90,2 +101,8 @@ } | ||
page.injectJs('browser-shim.js') | ||
if (isSlimer && config.settings && config.settings.userAgent) { | ||
page.evaluate(function(ua) { | ||
navigator.__defineGetter__('userAgent', function() { return ua }) | ||
}, config.settings.userAgent) | ||
} | ||
} | ||
@@ -105,2 +122,6 @@ page.onResourceReceived = function(resource) { | ||
page.render(data.screenshot + '.png') | ||
} else if (data.configureColWidth) { | ||
page.evaluate(function(columns) { | ||
Mocha.reporters.Base.window.width = columns | ||
}, parseInt(system.env.COLUMNS || 75) * .75 | 0) | ||
} else if (data.configureMocha) { | ||
@@ -121,3 +142,7 @@ configureMocha() | ||
} | ||
phantom.exit(data.testRunEnded.failures) | ||
setTimeout(function() { | ||
phantom.exit(data.testRunEnded.failures) | ||
}, 100) | ||
} else if (data.sendEvent) { | ||
page.sendEvent.apply(page, data.sendEvent) | ||
} | ||
@@ -134,21 +159,20 @@ } | ||
var timeout = config.timeout || 10000 | ||
var loadTimeout = config.loadTimeout || 10000 | ||
setTimeout(function() { | ||
if (!configured) { | ||
if (page.evaluate(function() { return !window.mocha })) { | ||
fail('mocha was not found in the page within ' + timeout + 'ms of the page loading.') | ||
fail('mocha was not found in the page within ' + loadTimeout + 'ms of the page loading.') | ||
} else if (page.evaluate(function() { return window.initMochaPhantomJS })) { | ||
fail('Likely due to external resource loading and timing, your tests require calling `window.initMochaPhantomJS()` before calling any mocha setup functions. See https://github.com/nathanboktae/mocha-phantomjs-core/issues/12') | ||
} else { | ||
fail('mocha was not initialized within ' + timeout + 'ms of the page loading. Make sure to call `mocha.ui` or `mocha.setup`.') | ||
fail('mocha was not initialized within ' + loadTimeout + 'ms of the page loading. Make sure to call `mocha.ui` or `mocha.setup`.') | ||
} | ||
} else if (!runStarted) { | ||
fail('mocha.run() was not called within ' + timeout + 'ms of the page loading.') | ||
fail('mocha.run() was not called within ' + loadTimeout + 'ms of the page loading.') | ||
} | ||
}, timeout) | ||
}, loadTimeout) | ||
} | ||
function configureMocha() { | ||
page.evaluate(function(config, env, columns) { | ||
Mocha.reporters.Base.window.width = columns | ||
page.evaluate(function(config, env) { | ||
mocha.env = env | ||
@@ -158,2 +182,5 @@ | ||
mocha.bail(config.bail) | ||
if (config.timeout) { | ||
mocha.timeout(config.timeout) | ||
} | ||
if (config.grep) { | ||
@@ -165,3 +192,3 @@ mocha.grep(config.grep) | ||
} | ||
}, config, system.env, parseInt(system.env.COLUMNS || 75) * .75 | 0) | ||
}, config, system.env) | ||
@@ -171,4 +198,9 @@ // setup a the reporter | ||
// we failed to set the reporter - likely a 3rd party reporter than needs to be wrapped | ||
var customReporter = fs.read(reporter), | ||
wrapper = function() { | ||
try { | ||
var customReporter = fs.read(reporter) | ||
} catch(e) { | ||
fail('Unable to open file \'' + reporter + '\'') | ||
} | ||
var wrapper = function() { | ||
var exports, module, process, require; | ||
@@ -175,0 +207,0 @@ require = function(what) { |
{ | ||
"name": "mocha-phantomjs-core", | ||
"description": "Run mocha tests in phantomjs", | ||
"description": "Run client-side mocha tests in phantomjs or slimerjs", | ||
"keywords": [ | ||
"phantomjs", | ||
"slimerjs", | ||
"mocha", | ||
@@ -12,3 +13,3 @@ "test", | ||
], | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"author": "Nathan Black <nathan@nathanblack.org> (https://github.com/nathanboktae)", | ||
@@ -15,0 +16,0 @@ "licenses": [ |
@@ -1,13 +0,13 @@ | ||
# Run Mocha tests in PhantomJS | ||
# Run client-side Mocha tests in PhantomJS or SlimerJS | ||
[![Join the chat at https://gitter.im/nathanboktae/mocha-phantomjs-core](https://badges.gitter.im/nathanboktae/mocha-phantomjs-core.svg)](https://gitter.im/nathanboktae/mocha-phantomjs-core?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![Build Status](https://secure.travis-ci.org/nathanboktae/mocha-phantomjs-core.png)](http://travis-ci.org/nathanboktae/mocha-phantomjs-core) | ||
So now that you got your tests [Mocha](http://mochajs.org/) running on a simple flat HTML file, now how do you run them in your CI environment? [Karma](http://karma-runner.github.io/)? what is this `karma.conf.js` file I have to write? and some background runner task? how do I grep over just a few tests? wait I need a to also install a driver for phantomjs too? bleck. | ||
So now that you got your tests [Mocha](http://mochajs.org/) running on a simple flat HTML file, now how do you run them in your CI environment? [Karma](http://karma-runner.github.io/)? what is this `karma.conf.js` file I have to write? and some background runner task? how do I grep over just a few tests? wait I need a to also install a launcher for phantomjs or slimerjs too? bleck. | ||
Rather than force you to redo your test harness and local development testing, simply run `phantomjs mocha-phantomjs-core.js spec tests/mytests.html` and be done with it. `mocha-phantomjs-core` builds on top of what you already have, with no high barrier to entry like Karma. | ||
## The core of [mocha-phantomjs](https://github.com/metaskills/mocha-phantomjs) | ||
New in 2.0 is [SlimerJS](https://slimerjs.org) support! There are [some bugs](https://github.com/laurentj/slimerjs/issues/created_by/nathanboktae) still to be worked out, but now you can run your tests headless on the latest firefox version instead of an old QtWebKit! | ||
This project is the core phantomjs code for `mocha-phantomjs`. If you are a regular user wanting to use it from the command line, you'll want to go there. If you are a build plugin author, or want direct control over how phantomjs is invoked, you are in the right place. | ||
## Installation | ||
@@ -22,3 +22,3 @@ | ||
``` | ||
phantomjs mocha-phantomjs-core.js <TESTS> <REPORTER> <CONFIG as JSON> | ||
<phantomjs|slimerjs> mocha-phantomjs-core.js <TESTS> <REPORTER> <CONFIG as JSON> | ||
``` | ||
@@ -60,5 +60,9 @@ | ||
#### `loadTimeout` | ||
Time in milliseconds after the page loads that `mocha.run` needs to be called. Defaults to 10 seconds. | ||
#### `timeout` | ||
Time in milliseconds after the page loads that `mocha.run` needs to be called. Defaults to 10 seconds. | ||
Sets mocha's root suite timeout. Defers to mocha's default if omitted. | ||
@@ -104,2 +108,15 @@ #### `viewportSize` | ||
## Send event | ||
`mocha-phantomjs-core` supports [sending events](http://phantomjs.org/api/webpage/method/send-event.html) | ||
from your test code to allow for more ouside testing. For example, to trigger an external `click` event: | ||
```javascript | ||
if (window.callPhantom) { | ||
window.callPhantom({ | ||
sendEvent: ['click', 10, 10] // array of arguments | ||
}); | ||
} | ||
``` | ||
## Environment variables | ||
@@ -106,0 +123,0 @@ |
52404715
356
152