junit.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _utils = require("playwright-core/lib/utils");
  9. var _base = require("./base");
  10. var _empty = _interopRequireDefault(require("./empty"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class JUnitReporter extends _empty.default {
  28. constructor(options = {}) {
  29. super();
  30. this.config = void 0;
  31. this.suite = void 0;
  32. this.timestamp = void 0;
  33. this.startTime = void 0;
  34. this.totalTests = 0;
  35. this.totalFailures = 0;
  36. this.totalSkipped = 0;
  37. this.outputFile = void 0;
  38. this.resolvedOutputFile = void 0;
  39. this.stripANSIControlSequences = false;
  40. this.outputFile = options.outputFile || reportOutputNameFromEnv();
  41. this.stripANSIControlSequences = options.stripANSIControlSequences || false;
  42. }
  43. printsToStdio() {
  44. return !this.outputFile;
  45. }
  46. onConfigure(config) {
  47. this.config = config;
  48. }
  49. onBegin(suite) {
  50. this.suite = suite;
  51. this.timestamp = new Date();
  52. this.startTime = (0, _utils.monotonicTime)();
  53. if (this.outputFile) {
  54. (0, _utils.assert)(this.config.configFile || _path.default.isAbsolute(this.outputFile), 'Expected fully resolved path if not using config file.');
  55. this.resolvedOutputFile = this.config.configFile ? _path.default.resolve(_path.default.dirname(this.config.configFile), this.outputFile) : this.outputFile;
  56. }
  57. }
  58. async onEnd(result) {
  59. const duration = (0, _utils.monotonicTime)() - this.startTime;
  60. const children = [];
  61. for (const projectSuite of this.suite.suites) {
  62. for (const fileSuite of projectSuite.suites) children.push(await this._buildTestSuite(projectSuite.title, fileSuite));
  63. }
  64. const tokens = [];
  65. const self = this;
  66. const root = {
  67. name: 'testsuites',
  68. attributes: {
  69. id: process.env[`PLAYWRIGHT_JUNIT_SUITE_ID`] || '',
  70. name: process.env[`PLAYWRIGHT_JUNIT_SUITE_NAME`] || '',
  71. tests: self.totalTests,
  72. failures: self.totalFailures,
  73. skipped: self.totalSkipped,
  74. errors: 0,
  75. time: duration / 1000
  76. },
  77. children
  78. };
  79. serializeXML(root, tokens, this.stripANSIControlSequences);
  80. const reportString = tokens.join('\n');
  81. if (this.resolvedOutputFile) {
  82. await _fs.default.promises.mkdir(_path.default.dirname(this.resolvedOutputFile), {
  83. recursive: true
  84. });
  85. await _fs.default.promises.writeFile(this.resolvedOutputFile, reportString);
  86. } else {
  87. console.log(reportString);
  88. }
  89. }
  90. async _buildTestSuite(projectName, suite) {
  91. let tests = 0;
  92. let skipped = 0;
  93. let failures = 0;
  94. let duration = 0;
  95. const children = [];
  96. for (const test of suite.allTests()) {
  97. ++tests;
  98. if (test.outcome() === 'skipped') ++skipped;
  99. if (!test.ok()) ++failures;
  100. for (const result of test.results) duration += result.duration;
  101. await this._addTestCase(suite.title, test, children);
  102. }
  103. this.totalTests += tests;
  104. this.totalSkipped += skipped;
  105. this.totalFailures += failures;
  106. const entry = {
  107. name: 'testsuite',
  108. attributes: {
  109. name: suite.title,
  110. timestamp: this.timestamp.toISOString(),
  111. hostname: projectName,
  112. tests,
  113. failures,
  114. skipped,
  115. time: duration / 1000,
  116. errors: 0
  117. },
  118. children
  119. };
  120. return entry;
  121. }
  122. async _addTestCase(suiteName, test, entries) {
  123. var _properties$children2;
  124. const entry = {
  125. name: 'testcase',
  126. attributes: {
  127. // Skip root, project, file
  128. name: test.titlePath().slice(3).join(' › '),
  129. // filename
  130. classname: suiteName,
  131. time: test.results.reduce((acc, value) => acc + value.duration, 0) / 1000
  132. },
  133. children: []
  134. };
  135. entries.push(entry);
  136. // Xray Test Management supports testcase level properties, where additional metadata may be provided
  137. // some annotations are encoded as value attributes, other as cdata content; this implementation supports
  138. // Xray JUnit extensions but it also agnostic, so other tools can also take advantage of this format
  139. const properties = {
  140. name: 'properties',
  141. children: []
  142. };
  143. for (const annotation of test.annotations) {
  144. var _properties$children;
  145. const property = {
  146. name: 'property',
  147. attributes: {
  148. name: annotation.type,
  149. value: annotation !== null && annotation !== void 0 && annotation.description ? annotation.description : ''
  150. }
  151. };
  152. (_properties$children = properties.children) === null || _properties$children === void 0 ? void 0 : _properties$children.push(property);
  153. }
  154. if ((_properties$children2 = properties.children) !== null && _properties$children2 !== void 0 && _properties$children2.length) entry.children.push(properties);
  155. if (test.outcome() === 'skipped') {
  156. entry.children.push({
  157. name: 'skipped'
  158. });
  159. return;
  160. }
  161. if (!test.ok()) {
  162. entry.children.push({
  163. name: 'failure',
  164. attributes: {
  165. message: `${_path.default.basename(test.location.file)}:${test.location.line}:${test.location.column} ${test.title}`,
  166. type: 'FAILURE'
  167. },
  168. text: (0, _base.stripAnsiEscapes)((0, _base.formatFailure)(this.config, test).message)
  169. });
  170. }
  171. const systemOut = [];
  172. const systemErr = [];
  173. for (const result of test.results) {
  174. systemOut.push(...result.stdout.map(item => item.toString()));
  175. systemErr.push(...result.stderr.map(item => item.toString()));
  176. for (const attachment of result.attachments) {
  177. if (!attachment.path) continue;
  178. let attachmentPath = _path.default.relative(this.config.rootDir, attachment.path);
  179. try {
  180. if (this.resolvedOutputFile) attachmentPath = _path.default.relative(_path.default.dirname(this.resolvedOutputFile), attachment.path);
  181. } catch {
  182. systemOut.push(`\nWarning: Unable to make attachment path ${attachment.path} relative to report output file ${this.outputFile}`);
  183. }
  184. try {
  185. await _fs.default.promises.access(attachment.path);
  186. systemOut.push(`\n[[ATTACHMENT|${attachmentPath}]]\n`);
  187. } catch {
  188. systemErr.push(`\nWarning: attachment ${attachmentPath} is missing`);
  189. }
  190. }
  191. }
  192. // Note: it is important to only produce a single system-out/system-err entry
  193. // so that parsers in the wild understand it.
  194. if (systemOut.length) entry.children.push({
  195. name: 'system-out',
  196. text: systemOut.join('')
  197. });
  198. if (systemErr.length) entry.children.push({
  199. name: 'system-err',
  200. text: systemErr.join('')
  201. });
  202. }
  203. }
  204. function serializeXML(entry, tokens, stripANSIControlSequences) {
  205. const attrs = [];
  206. for (const [name, value] of Object.entries(entry.attributes || {})) attrs.push(`${name}="${escape(String(value), stripANSIControlSequences, false)}"`);
  207. tokens.push(`<${entry.name}${attrs.length ? ' ' : ''}${attrs.join(' ')}>`);
  208. for (const child of entry.children || []) serializeXML(child, tokens, stripANSIControlSequences);
  209. if (entry.text) tokens.push(escape(entry.text, stripANSIControlSequences, true));
  210. tokens.push(`</${entry.name}>`);
  211. }
  212. // See https://en.wikipedia.org/wiki/Valid_characters_in_XML
  213. const discouragedXMLCharacters = /[\u0000-\u0008\u000b-\u000c\u000e-\u001f\u007f-\u0084\u0086-\u009f]/g;
  214. function escape(text, stripANSIControlSequences, isCharacterData) {
  215. if (stripANSIControlSequences) text = (0, _base.stripAnsiEscapes)(text);
  216. if (isCharacterData) {
  217. text = '<![CDATA[' + text.replace(/]]>/g, ']]&gt;') + ']]>';
  218. } else {
  219. const escapeRe = /[&"'<>]/g;
  220. text = text.replace(escapeRe, c => ({
  221. '&': '&amp;',
  222. '"': '&quot;',
  223. "'": '&apos;',
  224. '<': '&lt;',
  225. '>': '&gt;'
  226. })[c]);
  227. }
  228. text = text.replace(discouragedXMLCharacters, '');
  229. return text;
  230. }
  231. function reportOutputNameFromEnv() {
  232. if (process.env[`PLAYWRIGHT_JUNIT_OUTPUT_NAME`]) return _path.default.resolve(process.cwd(), process.env[`PLAYWRIGHT_JUNIT_OUTPUT_NAME`]);
  233. return undefined;
  234. }
  235. var _default = exports.default = JUnitReporter;