testreporter.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 (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. };
  60. this.specStarted = function (result) {
  61. };
  62. this.specDone = function (result) {
  63. if (result.failedExpectations.length === 0) {
  64. sendResult('TEST-PASSED', result.description);
  65. } else {
  66. var failedMessages = '';
  67. var items = result.failedExpectations;
  68. for (var i = 0, ii = items.length; i < ii; i++) {
  69. failedMessages += items[i].message + ' ';
  70. }
  71. sendResult('TEST-UNEXPECTED-FAIL', result.description, failedMessages);
  72. }
  73. };
  74. this.suiteDone = function (result) {
  75. };
  76. this.jasmineDone = function () {
  77. setTimeout(sendQuitRequest, 500);
  78. };
  79. };