Path: blob/trunk/third_party/js/qunit/qunit_test_runner.js
4024 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview QUnit test runner adapter for Selenium's Java test harness.19*20* This script registers QUnit callbacks to track test execution and exposes21* a simple API on window.top that the Java ClosureTestStatement can poll22* to determine when tests are finished and whether they passed.23*/24(function() {25'use strict';2627var results = {28finished: false,29passed: false,30report: '',31failures: []32};3334// Expose the test runner interface on window.top for the Java harness to poll35window.top.QUnitTestRunner = {36isFinished: function() {37return results.finished;38},39isSuccess: function() {40return results.passed;41},42getReport: function() {43return results.report;44}45};4647QUnit.on('testEnd', function(testEnd) {48if (testEnd.status === 'failed') {49var fullName = testEnd.fullName.join(' > ');50var errors = testEnd.errors.map(function(err) {51var msg = err.message || '';52if (err.actual !== undefined && err.expected !== undefined) {53msg += '\n Expected: ' + JSON.stringify(err.expected);54msg += '\n Actual: ' + JSON.stringify(err.actual);55}56if (err.stack) {57msg += '\n Stack: ' + err.stack;58}59return msg;60});61results.failures.push({62name: fullName,63errors: errors64});65}66});6768QUnit.on('runEnd', function(runEnd) {69results.finished = true;70results.passed = runEnd.status === 'passed';7172var lines = [];73lines.push('QUnit Test Results');74lines.push('==================');75lines.push('Status: ' + runEnd.status);76lines.push('Total: ' + runEnd.testCounts.total);77lines.push('Passed: ' + runEnd.testCounts.passed);78lines.push('Failed: ' + runEnd.testCounts.failed);79lines.push('Skipped: ' + runEnd.testCounts.skipped);80lines.push('Todo: ' + runEnd.testCounts.todo);81lines.push('Runtime: ' + runEnd.runtime + 'ms');8283if (results.failures.length > 0) {84lines.push('');85lines.push('Failures:');86lines.push('---------');87results.failures.forEach(function(failure) {88lines.push('');89lines.push('Test: ' + failure.name);90failure.errors.forEach(function(err) {91lines.push(' ' + err);92});93});94}9596results.report = lines.join('\n');97});98})();99100101