forked from jsdom/jsdom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make jsdom browserifiable and add browser test runner
To run the tests: `node test/browser-runner.js`. The runner accepts the same options as `test/runner`, as well as some optional arguments that control the test setup.
- Loading branch information
Showing
11 changed files
with
437 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
gmon.out | ||
v8.log | ||
node_modules | ||
test/worker-bundle.js |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = function (o) { | ||
o.getGlobal = function () { | ||
return o; | ||
}; | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
window._browserRunner = { | ||
events: [] | ||
}; | ||
|
||
var worker = new Worker('./worker-bundle.js'); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var consoleEl = document.querySelector('.console'); | ||
|
||
function fire(event, detail) { | ||
window._browserRunner.events.push({ | ||
event: event, | ||
detail: detail | ||
}); | ||
} | ||
|
||
worker.onmessage = function (e) { | ||
if (e.data.method) { | ||
switch (e.data.method) { | ||
case 'fire': | ||
fire(e.data.params.event, e.data.params.data); | ||
break; | ||
case 'console': | ||
fire('console', e.data.params); | ||
console[e.data.params.level].apply(console, e.data.params.message); | ||
break; | ||
case 'ready': | ||
fire('ready'); | ||
worker.postMessage(location.search.slice(1)); | ||
break; | ||
default: | ||
console.error('Unknown method', e.data.method); | ||
} | ||
} | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
require('colors'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var wd = require('wd'); | ||
var Q = require('q'); | ||
var browser; | ||
|
||
var optimist = require('./runner-options'); | ||
|
||
optimist. | ||
usage('Run the jsdom test suite in a browser via WebDriver'). | ||
describe('http-port', 'port to run test server on (defaults to pid + 20000)'). | ||
describe('web-driver-port', 'port to run Selenium on (defaults to pid + 20000)'). | ||
describe('verbose-web-driver', 'print verbose output from wd to stdout'). | ||
describe('verbose-browser-console', 'print browser console to stdout'); | ||
|
||
var argv = optimist.argv; | ||
|
||
if (argv.help) { | ||
optimist.showHelp(); | ||
process.exit(); | ||
} | ||
|
||
var httpPort = argv['http-port'] || process.pid + 20000; | ||
var wdPort = argv['web-driver-port'] || httpPort + 10000; | ||
|
||
/** | ||
* Return the body of a function as a string | ||
* | ||
* wd should do this for us, but it doesn't | ||
*/ | ||
function getFnBody(fn) { | ||
var src = fn.toString(); | ||
return src.slice(src.indexOf('{') + 1, src.lastIndexOf('}')); | ||
} | ||
|
||
function run() { | ||
var passed = false; | ||
browser.init({ browserName: 'chrome' }). | ||
then(function () { | ||
return browser.setAsyncScriptTimeout(5000); | ||
}). | ||
then(function () { | ||
return browser.get([ | ||
'http://localhost:', | ||
httpPort, | ||
'/test?', | ||
require('querystring').stringify(argv) | ||
].join('')); | ||
}). | ||
then(function (result) { | ||
function browserPoll() { | ||
var events = window._browserRunner.events; | ||
|
||
return events.splice(0, events.length); | ||
} | ||
|
||
var deferred = Q.defer(); | ||
|
||
var runner = new EventEmitter(); | ||
require('./runner-display')(runner, argv, function (err) { | ||
This comment has been minimized.
Sorry, something went wrong.
domenic
|
||
passed = !err; | ||
deferred.resolve(); | ||
}); | ||
var nodeunitTypes = require('nodeunit').types; | ||
|
||
function poll() { | ||
browser. | ||
execute(getFnBody(browserPoll)). | ||
then(function (events) { | ||
var done = false; | ||
|
||
events.forEach(function (event) { | ||
switch (event.event) { | ||
case 'testDone': | ||
case 'moduleDone': | ||
runner.emit(event.event, | ||
event.detail[0], | ||
nodeunitTypes.assertionList( | ||
event.detail[1].map(nodeunitTypes.assertion))); | ||
break; | ||
case 'log': | ||
runner.emit(event.event, | ||
nodeunitTypes.assertion(event.detail[0])); | ||
break; | ||
case 'done': | ||
runner.emit(event.event, | ||
nodeunitTypes.assertionList( | ||
event.detail[0].map(nodeunitTypes.assertion))); | ||
break; | ||
case 'console': | ||
case 'http': | ||
case 'status': | ||
case 'command': | ||
browser. | ||
emit.apply(browser, [event.event].concat(event.detail)); | ||
break; | ||
default: | ||
runner.emit.apply(runner, [event.event].concat(event.detail)); | ||
} | ||
|
||
if (event.detail && event.event === 'done') { | ||
done = true; | ||
} | ||
}); | ||
|
||
if (!done) { | ||
setTimeout(poll, 50); | ||
} | ||
}); | ||
} | ||
|
||
poll(); | ||
return deferred.promise; | ||
}). | ||
fin(function () { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return browser.quit(); | ||
}). | ||
fin(function () { | ||
process.exit(passed ? 0 : 1); | ||
}). | ||
done(); | ||
} | ||
|
||
// browserify and run the tests | ||
require('child_process').exec( | ||
This comment has been minimized.
Sorry, something went wrong.
domenic
|
||
'node_modules/browserify/bin/cmd.js test/worker.js -o test/worker-bundle.js', | ||
function (err, stdout, stderr) { | ||
if (err) { | ||
console.log(stdout.toString()); | ||
console.log('Failed to browserify test/worker'); | ||
console.log(err); | ||
process.exit(1); | ||
return; | ||
} | ||
|
||
// start web server | ||
var httpServer = require('http-server').createServer().listen(httpPort); | ||
|
||
// set up webdriver | ||
browser = wd.promiseRemote({ | ||
port: wdPort | ||
}); | ||
|
||
if (argv['verbose-web-driver']) { | ||
// really verbose wd logging | ||
browser.on('status', function (info) { | ||
console.log(info.cyan); | ||
}); | ||
browser.on('command', function (eventType, command, response) { | ||
console.log(' > ' + eventType.cyan, command, (response || '').grey); | ||
}); | ||
browser.on('http', function (method, path, data) { | ||
console.log(' > ' + method.magenta, path, (data || '').grey); | ||
}); | ||
} | ||
|
||
if (argv['verbose-browser-console']) { | ||
browser.on('console', function (detail) { | ||
console[detail.level].apply(console, detail.message); | ||
}); | ||
} | ||
|
||
// start selenium | ||
This comment has been minimized.
Sorry, something went wrong. |
||
var selenium = require('selenium-standalone'); | ||
var wdServer = selenium({ | ||
stdio: 'pipe' | ||
}, ['-port', wdPort]); | ||
|
||
// time out after a default of 30 seconds | ||
var h = setTimeout(function () { | ||
console.log('Timed out waiting for selenium server to start'); | ||
wdServer.kill(); | ||
process.exit(1); | ||
}, argv.wdTimeout || 30 * 1000); | ||
|
||
// Wait for selenium server to start. | ||
wdServer.stdout.on('data', function (output) { | ||
if (output.toString().indexOf('Started org.openqa.jetty.jetty.Server') >= 0) { | ||
clearTimeout(h); | ||
run(); | ||
} | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<title>Browserify in worker test</title> | ||
</head> | ||
<body> | ||
<pre class='console'></pre> | ||
<script src="./browser-main.js"></script> | ||
</body> | ||
</html> |
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
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
Oops, something went wrong.
What are the additions to this file for? They seem bad?