|
| 1 | +require('colors'); |
| 2 | +var EventEmitter = require('events').EventEmitter; |
| 3 | +var wd = require('wd'); |
| 4 | +var Q = require('q'); |
| 5 | +var browser; |
| 6 | + |
| 7 | +var optimist = require('./runner-options'); |
| 8 | + |
| 9 | +optimist. |
| 10 | + usage('Run the jsdom test suite in a browser via WebDriver'). |
| 11 | + describe('http-port', 'port to run test server on (defaults to pid + 20000)'). |
| 12 | + describe('web-driver-port', 'port to run Selenium on (defaults to pid + 20000)'). |
| 13 | + describe('verbose-web-driver', 'print verbose output from wd to stdout'). |
| 14 | + describe('verbose-browser-console', 'print browser console to stdout'); |
| 15 | + |
| 16 | +var argv = optimist.argv; |
| 17 | + |
| 18 | +if (argv.help) { |
| 19 | + optimist.showHelp(); |
| 20 | + process.exit(); |
| 21 | +} |
| 22 | + |
| 23 | +var httpPort = argv['http-port'] || process.pid + 20000; |
| 24 | +var wdPort = argv['web-driver-port'] || httpPort + 10000; |
| 25 | + |
| 26 | +/** |
| 27 | + * Return the body of a function as a string |
| 28 | + * |
| 29 | + * wd should do this for us, but it doesn't |
| 30 | + */ |
| 31 | +function getFnBody(fn) { |
| 32 | + var src = fn.toString(); |
| 33 | + return src.slice(src.indexOf('{') + 1, src.lastIndexOf('}')); |
| 34 | +} |
| 35 | + |
| 36 | +function run() { |
| 37 | + var passed = false; |
| 38 | + browser.init({ browserName: 'chrome' }). |
| 39 | + then(function () { |
| 40 | + return browser.setAsyncScriptTimeout(5000); |
| 41 | + }). |
| 42 | + then(function () { |
| 43 | + return browser.get([ |
| 44 | + 'http://localhost:', |
| 45 | + httpPort, |
| 46 | + '/test?', |
| 47 | + require('querystring').stringify(argv) |
| 48 | + ].join('')); |
| 49 | + }). |
| 50 | + then(function (result) { |
| 51 | + function browserPoll() { |
| 52 | + var events = window._browserRunner.events; |
| 53 | + |
| 54 | + return events.splice(0, events.length); |
| 55 | + } |
| 56 | + |
| 57 | + var deferred = Q.defer(); |
| 58 | + |
| 59 | + var runner = new EventEmitter(); |
| 60 | + require('./runner-display')(runner, argv, function (err) { |
| 61 | + passed = !err; |
| 62 | + deferred.resolve(); |
| 63 | + }); |
| 64 | + var nodeunitTypes = require('nodeunit').types; |
| 65 | + |
| 66 | + function poll() { |
| 67 | + browser. |
| 68 | + execute(getFnBody(browserPoll)). |
| 69 | + then(function (events) { |
| 70 | + var done = false; |
| 71 | + |
| 72 | + events.forEach(function (event) { |
| 73 | + switch (event.event) { |
| 74 | + case 'testDone': |
| 75 | + case 'moduleDone': |
| 76 | + runner.emit(event.event, |
| 77 | + event.detail[0], |
| 78 | + nodeunitTypes.assertionList( |
| 79 | + event.detail[1].map(nodeunitTypes.assertion))); |
| 80 | + break; |
| 81 | + case 'log': |
| 82 | + runner.emit(event.event, |
| 83 | + nodeunitTypes.assertion(event.detail[0])); |
| 84 | + break; |
| 85 | + case 'done': |
| 86 | + runner.emit(event.event, |
| 87 | + nodeunitTypes.assertionList( |
| 88 | + event.detail[0].map(nodeunitTypes.assertion))); |
| 89 | + break; |
| 90 | + case 'console': |
| 91 | + case 'http': |
| 92 | + case 'status': |
| 93 | + case 'command': |
| 94 | + browser. |
| 95 | + emit.apply(browser, [event.event].concat(event.detail)); |
| 96 | + break; |
| 97 | + default: |
| 98 | + runner.emit.apply(runner, [event.event].concat(event.detail)); |
| 99 | + } |
| 100 | + |
| 101 | + if (event.detail && event.event === 'done') { |
| 102 | + done = true; |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + if (!done) { |
| 107 | + setTimeout(poll, 50); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + poll(); |
| 113 | + return deferred.promise; |
| 114 | + }). |
| 115 | + fin(function () { |
| 116 | + return browser.quit(); |
| 117 | + }). |
| 118 | + fin(function () { |
| 119 | + process.exit(passed ? 0 : 1); |
| 120 | + }). |
| 121 | + done(); |
| 122 | +} |
| 123 | + |
| 124 | +// browserify and run the tests |
| 125 | +require('child_process').exec( |
| 126 | + 'node_modules/browserify/bin/cmd.js test/worker.js -o test/worker-bundle.js', |
| 127 | + function (err, stdout, stderr) { |
| 128 | + if (err) { |
| 129 | + console.log(stdout.toString()); |
| 130 | + console.log('Failed to browserify test/worker'); |
| 131 | + console.log(err); |
| 132 | + process.exit(1); |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + // start web server |
| 137 | + var httpServer = require('http-server').createServer().listen(httpPort); |
| 138 | + |
| 139 | + // set up webdriver |
| 140 | + browser = wd.promiseRemote({ |
| 141 | + port: wdPort |
| 142 | + }); |
| 143 | + |
| 144 | + if (argv['verbose-web-driver']) { |
| 145 | + // really verbose wd logging |
| 146 | + browser.on('status', function (info) { |
| 147 | + console.log(info.cyan); |
| 148 | + }); |
| 149 | + browser.on('command', function (eventType, command, response) { |
| 150 | + console.log(' > ' + eventType.cyan, command, (response || '').grey); |
| 151 | + }); |
| 152 | + browser.on('http', function (method, path, data) { |
| 153 | + console.log(' > ' + method.magenta, path, (data || '').grey); |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + if (argv['verbose-browser-console']) { |
| 158 | + browser.on('console', function (detail) { |
| 159 | + console[detail.level].apply(console, detail.message); |
| 160 | + }); |
| 161 | + } |
| 162 | + |
| 163 | + // start selenium |
| 164 | + var selenium = require('selenium-standalone'); |
| 165 | + var wdServer = selenium({ |
| 166 | + stdio: 'pipe' |
| 167 | + }, ['-port', wdPort]); |
| 168 | + |
| 169 | + // time out after a default of 30 seconds |
| 170 | + var h = setTimeout(function () { |
| 171 | + console.log('Timed out waiting for selenium server to start'); |
| 172 | + wdServer.kill(); |
| 173 | + process.exit(1); |
| 174 | + }, argv.wdTimeout || 30 * 1000); |
| 175 | + |
| 176 | + // Wait for selenium server to start. |
| 177 | + wdServer.stdout.on('data', function (output) { |
| 178 | + if (output.toString().indexOf('Started org.openqa.jetty.jetty.Server') >= 0) { |
| 179 | + clearTimeout(h); |
| 180 | + run(); |
| 181 | + } |
| 182 | + }); |
| 183 | + }); |
0 commit comments