1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * Integration module for QUnit tests running in browser tests.
9 * - Sets QUnit.autostart to false, so that the browser test can hook the test
10 * results callback before the test starts.
11 * - Implements a text-based test reporter to report test results back to the
15 (function(QUnit, automationController, exports) {
19 var TestReporter = function() {
20 this.errorMessage_ = '';
21 this.failedTestsCount_ = 0;
22 this.failedAssertions_ = [];
25 TestReporter.prototype.init = function(qunit) {
26 qunit.testDone(this.onTestDone_.bind(this));
27 qunit.log(this.onAssertion_.bind(this));
30 TestReporter.prototype.onTestDone_ = function(details) {
31 if (this.failedAssertions_.length > 0) {
32 this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n';
33 this.errorMessage_ += this.failedAssertions_.map(
34 function(assertion, index){
35 return ' ' + (index + 1) + '. ' + assertion.message + '\n' +
36 ' ' + assertion.source;
38 this.failedAssertions_ = [];
39 this.failedTestsCount_++;
43 TestReporter.prototype.onAssertion_ = function(details) {
44 if (!details.result) {
45 this.failedAssertions_.push(details);
49 TestReporter.prototype.getErrorMessage = function(){
50 var errorMessage = '';
51 if (this.failedTestsCount_ > 0) {
52 var test = (this.failedTestsCount_ > 1) ? 'tests' : 'test';
53 errorMessage = this.failedTestsCount_ + ' ' + test + ' failed:\n';
54 errorMessage += this.errorMessage_;
59 var BrowserTestHarness = function(qunit, domAutomationController, reporter) {
61 this.automationController_ = domAutomationController;
62 this.reporter_ = reporter;
65 BrowserTestHarness.prototype.init = function() {
66 this.qunit_.config.autostart = false;
69 BrowserTestHarness.prototype.run = function() {
70 this.reporter_.init(this.qunit_);
72 this.qunit_.done(function(details){
73 this.automationController_.send(JSON.stringify({
74 passed: details.passed == details.total,
75 errorMessage: this.reporter_.getErrorMessage()
80 // The browser test runs chrome with the flag --dom-automation, which creates
81 // the window.domAutomationController object. This allows the test suite to
82 // JS-encoded data back to the browser test.
83 if (automationController) {
85 console.error('browser_test_harness.js must be included after QUnit.js.');
89 var testHarness = new BrowserTestHarness(
94 exports.browserTestHarness = testHarness;
97 })(window.QUnit, window.domAutomationController, window);