testreporter.js 2.4 KB

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