Skip to content

Commit

Permalink
Merge pull request #12 from luka-zitnik/runner-xml
Browse files Browse the repository at this point in the history
Add runner-xml.js
  • Loading branch information
jonkemp authored Dec 2, 2016
2 parents 3116e2a + 398f604 commit d600650
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 2 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"gulp-mocha": "^2.0.0",
"mocha": "^2.4.5",
"phantomjs-prebuilt": "^2.1.7",
"qunitjs": "^1.23.0"
"qunitjs": "^1.23.0",
"xml2js": "^0.4.16"
},
"dependencies": {
"qunit-reporter-junit": "^1.0.2"
}
}
131 changes: 131 additions & 0 deletions runner-xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*global phantom:false, require:false, console:false, window:false, QUnit:false */

(function () {
'use strict';

var url, page, timeout,
args = require('system').args;

// arg[0]: scriptName, args[1...]: arguments
if (args.length < 2) {
console.error('Usage:\n phantomjs [phantom arguments] runner-xml.js [url-of-your-qunit-testsuite] [timeout-in-seconds] [page-properties]');
exit(1);
}

url = args[1];

if (args[2] !== undefined) {
timeout = parseInt(args[2], 10);
}

page = require('webpage').create();

if (args[3] !== undefined) {
try {
var pageProperties = JSON.parse(args[3]);

if (pageProperties) {
for (var prop in pageProperties) {
if (pageProperties.hasOwnProperty(prop)) {
page[prop] = pageProperties[prop];
}
}
}
} catch (e) {
console.error('Error parsing "' + args[3] + '": ' + e);
}
}

// Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
page.onConsoleMessage = function (msg) {
console.log(msg);
};

page.onInitialized = function () {
page.evaluate(addLogging);
};

page.onCallback = function (message) {
var result,
failed;

if (message) {
if (message.name === 'QUnit.done') {
result = message.data;
failed = !result || !result.total || result.failed;

if (!result.total) {
console.error('No tests were executed. Are you loading tests asynchronously?');
}

exit(failed ? 1 : 0);
}
}
};

page.open(url, function (status) {
if (status !== 'success') {
console.error('Unable to access network: ' + status);
exit(1);
} else {
// Cannot do this verification with the 'DOMContentLoaded' handler because it
// will be too late to attach it if a page does not have any script tags.
var qunitMissing = page.evaluate(function () {
return (typeof QUnit === 'undefined' || !QUnit);
});
if (qunitMissing) {
console.error('The `QUnit` object is not present on this page.');
exit(1);
}

// Set a default timeout value if the user does not provide one
if (typeof timeout === 'undefined') {
timeout = 5;
}

if (page.injectJs('./node_modules/qunit-reporter-junit/qunit-reporter-junit.js') === false) {
console.error('Could not inject `qunit-reporter-junit.js`.');
exit(1);
}

// Override default
page.evaluate(function () {
QUnit.jUnitReport = function (report) {
console.log(report.xml);
};
});

// Set a timeout on the test running, otherwise tests with async problems will hang forever
setTimeout(function () {
console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
exit(1);
}, timeout * 1000);

// Do nothing... the callback mechanism will handle everything!
}
});

function addLogging() {
window.document.addEventListener('DOMContentLoaded', function () {

QUnit.done(function (result) {
if (typeof window.callPhantom === 'function') {
window.callPhantom({
'name': 'QUnit.done',
'data': result
});
}
});

}, false);
}

function exit(code) {
if (page) {
page.close();
}
setTimeout(function () {
phantom.exit(code);
}, 0);
}
})();
110 changes: 109 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('qunit-phantomjs-runner runner.js', function () {
process.stdout.write = function (str) {
//out(str);

assert.ok(/Usage:\n phantomjs \[phantom arguments\] runner.js \[url-of-your-qunit-testsuite\] \[timeout-in-seconds\]/.test(str));
assert.ok(/Usage:\r?\n phantomjs \[phantom arguments\] runner.js \[url-of-your-qunit-testsuite\] \[timeout-in-seconds\]/.test(str));
process.stdout.write = out;
cb();
};
Expand Down Expand Up @@ -376,3 +376,111 @@ describe('qunit-phantomjs-runner runner-json.js', function () {
};
});
});

describe('qunit-phantomjs-runner runner-xml.js', function () {
this.timeout(5000);

it('tests should pass', function (cb) {

qunit('test/fixtures/passing.html', '../runner-xml.js');

process.stdout.write = function (str) {
//out(str);

process.stdout.write = out;
require('xml2js').parseString(str, function (err, result) {
assert.equal(result.testsuites.$.tests, 10);
assert.equal(result.testsuites.$.failures, 0);
assert.equal(result.testsuites.$.errors, 0);
cb();
});
};
});

it('tests should fail', function (cb) {

qunit('test/fixtures/failing.html', '../runner-xml.js');

process.stdout.write = function (str) {
//out(str);

process.stdout.write = out;
require('xml2js').parseString(str, function (err, result) {
assert.equal(result.testsuites.$.tests, 10);
assert.equal(result.testsuites.$.failures, 1);
assert.equal(result.testsuites.$.errors, 0);
cb();
});
};
});

it('should error on no tests', function (cb) {

qunit('test/fixtures/no-tests.html', '../runner-xml.js');

process.stdout.write = function (str) {
//out(str);

assert.ok(/No tests were executed. Are you loading tests asynchronously?/.test(str));
process.stdout.write = out;
cb();
};
});

it('should error when QUnit not found', function (cb) {

qunit('test/fixtures/no-qunit.html', '../runner-xml.js');

process.stdout.write = function (str) {
//out(str);

assert.ok(/The `QUnit` object is not present on this page./.test(str));
process.stdout.write = out;
cb();
};
});

it('should time out', function (cb) {
this.timeout(10000);

qunit('test/fixtures/async.html', '../runner-xml.js', 1);

process.stdout.write = function (str) {
//out(str);

assert.ok(/The specified timeout of 1 seconds has expired. Aborting.../.test(str));
process.stdout.write = out;
cb();
};
});

it('should show unable to access network', function (cb) {

qunit('test/fixtures/not-found.html', '../runner-xml.js');

process.stdout.write = function (str) {
//out(str);

assert.ok(/Unable to access network:/.test(str));
process.stdout.write = out;
cb();
};
});

it('should set custom viewport', function (cb) {

qunit('test/fixtures/custom-viewport.html', '../runner-xml.js', 5, '', pageProperties);

process.stdout.write = function (str) {
//out(str);

process.stdout.write = out;
require('xml2js').parseString(str, function (err, result) {
assert(result.testsuites.$.tests, 1);
assert(result.testsuites.$.failures, 0);
assert(result.testsuites.$.errors, 0);
cb();
});
};
});
});

2 comments on commit d600650

@johnjiang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be able to cut a release of this? If you have time, could you also update https://github.com/jonkemp/node-qunit-phantomjs with the args to use the xml runner? If not, I'd be happy to submit a PR.

@jonkemp
Copy link
Owner Author

@jonkemp jonkemp commented on d600650 Dec 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try to cut the release tonight.

Yes, please submit the PR. Thanks!

Please sign in to comment.