testreporter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. var TestReporter = function TestReporter(browser, appPath) {
  24. function send(action, json, cb) {
  25. var r = new XMLHttpRequest();
  26. r.open('POST', action, true);
  27. r.setRequestHeader('Content-Type', 'application/json');
  28. r.onreadystatechange = function sendTaskResultOnreadystatechange(e) {
  29. if (r.readyState === 4) {
  30. if (r.status !== 200) {
  31. send(action, json, cb);
  32. } else {
  33. if (cb) {
  34. cb();
  35. }
  36. }
  37. }
  38. };
  39. json['browser'] = browser;
  40. r.send(JSON.stringify(json));
  41. }
  42. function sendInfo(message) {
  43. send('/info', { message: message });
  44. }
  45. function sendResult(status, description, error) {
  46. var message = {
  47. status: status,
  48. description: description
  49. };
  50. if (typeof error !== 'undefined') {
  51. message['error'] = error;
  52. }
  53. send('/submit_task_results', message);
  54. }
  55. function sendQuitRequest() {
  56. send('/tellMeToQuit?path=' + escape(appPath), {});
  57. }
  58. this.now = function () {
  59. return new Date().getTime();
  60. };
  61. this.jasmineStarted = function (suiteInfo) {
  62. this.runnerStartTime = this.now();
  63. sendInfo('Started unit tests for ' + browser + '.');
  64. };
  65. this.suiteStarted = function (result) {};
  66. this.specStarted = function (result) {};
  67. this.specDone = function (result) {
  68. if (result.failedExpectations.length === 0) {
  69. sendResult('TEST-PASSED', result.description);
  70. } else {
  71. var failedMessages = '';
  72. var items = result.failedExpectations;
  73. for (var i = 0, ii = items.length; i < ii; i++) {
  74. failedMessages += items[i].message + ' ';
  75. }
  76. sendResult('TEST-UNEXPECTED-FAIL', result.description, failedMessages);
  77. }
  78. };
  79. this.suiteDone = function (result) {};
  80. this.jasmineDone = function () {
  81. setTimeout(sendQuitRequest, 500);
  82. };
  83. };