Reporter.js revision d0199bcbfc68b65683c19c4e3e0c38e238142e7e
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * An object capable of sending test results to a server.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @param {String} url The URL to submit the results to.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @param {Function} format (Optiona) A function that outputs the results in a specific format.
d0199bcbfc68b65683c19c4e3e0c38e238142e7eDav Glass * Default is YUITest.TestFormat.XML.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @constructor
5aab19b8b78189ce79cf89283f2d505566d4e6a8Dav Glass * @namespace Test
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @class Reporter
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
d0199bcbfc68b65683c19c4e3e0c38e238142e7eDav Glass YUITest.Reporter = function(url, format) {
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * The URL to submit the data to.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @type String
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @property url
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this.url = url;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * The formatting function to call when submitting the data.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @type Function
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @property format
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
d0199bcbfc68b65683c19c4e3e0c38e238142e7eDav Glass this.format = format || YUITest.TestFormat.XML;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Extra fields to submit with the request.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @type Object
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @property _fields
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @private
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields = new Object();
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * The form element used to submit the results.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @type HTMLFormElement
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @property _form
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @private
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form = null;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Iframe used as a target for form submission.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @type HTMLIFrameElement
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @property _iframe
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @private
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe = null;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass };
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
d0199bcbfc68b65683c19c4e3e0c38e238142e7eDav Glass YUITest.Reporter.prototype = {
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //restore missing constructor
d0199bcbfc68b65683c19c4e3e0c38e238142e7eDav Glass constructor: YUITest.Reporter,
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Adds a field to the form that submits the results.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @param {String} name The name of the field.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @param {Variant} value The value of the field.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @return {Void}
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @method addField
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass addField : function (name, value){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields[name] = value;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass },
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Removes all previous defined fields.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @return {Void}
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @method addField
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass clearFields : function(){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields = new Object();
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass },
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Cleans up the memory associated with the TestReporter, removing DOM elements
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * that were created.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @return {Void}
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @method destroy
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass destroy : function() {
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass if (this._form){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.parentNode.removeChild(this._form);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form = null;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass if (this._iframe){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.parentNode.removeChild(this._iframe);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe = null;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields = null;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass },
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass /**
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * Sends the report to the server.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @param {Object} results The results object created by TestRunner.
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @return {Void}
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass * @method report
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass */
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass report : function(results){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //if the form hasn't been created yet, create it
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass if (!this._form){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form = document.createElement("form");
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.method = "post";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.style.visibility = "hidden";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.style.position = "absolute";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.style.top = 0;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass document.body.appendChild(this._form);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //IE won't let you assign a name using the DOM, must do it the hacky way
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass try {
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe = document.createElement("<iframe name=\"yuiTestTarget\" />");
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass } catch (ex){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe = document.createElement("iframe");
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.name = "yuiTestTarget";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.src = "javascript:false";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.style.visibility = "hidden";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.style.position = "absolute";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._iframe.style.top = 0;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass document.body.appendChild(this._iframe);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.target = "yuiTestTarget";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //set the form's action
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.action = this.url;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //remove any existing fields
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass while(this._form.hasChildNodes()){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.removeChild(this._form.lastChild);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //create default fields
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields.results = this.format(results);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields.useragent = navigator.userAgent;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._fields.timestamp = (new Date()).toLocaleString();
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //add fields to the form
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass for (var prop in this._fields){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass var value = this._fields[prop];
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass if (this._fields.hasOwnProperty(prop) && (typeof value != "function")){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass var input = document.createElement("input");
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass input.type = "hidden";
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass input.name = prop;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass input.value = value;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.appendChild(input);
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass //remove default fields
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass delete this._fields.results;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass delete this._fields.useragent;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass delete this._fields.timestamp;
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass if (arguments[1] !== false){
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass this._form.submit();
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass }
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass
33d85edf47749fa345d7b636b9b4b9d0d0386f44Dav Glass };