teleEmitter.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TeleReporterEmitter = void 0;
  6. var _path = _interopRequireDefault(require("path"));
  7. var _utils = require("playwright-core/lib/utils");
  8. var _config = require("../common/config");
  9. var _teleReceiver = require("../isomorphic/teleReceiver");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class TeleReporterEmitter {
  27. constructor(messageSink, skipBuffers) {
  28. this._messageSink = void 0;
  29. this._rootDir = void 0;
  30. this._skipBuffers = void 0;
  31. this._messageSink = messageSink;
  32. this._skipBuffers = skipBuffers;
  33. }
  34. version() {
  35. return 'v2';
  36. }
  37. onConfigure(config) {
  38. this._rootDir = config.rootDir;
  39. this._messageSink({
  40. method: 'onConfigure',
  41. params: {
  42. config: this._serializeConfig(config)
  43. }
  44. });
  45. }
  46. onBegin(suite) {
  47. const projects = suite.suites.map(projectSuite => this._serializeProject(projectSuite));
  48. for (const project of projects) this._messageSink({
  49. method: 'onProject',
  50. params: {
  51. project
  52. }
  53. });
  54. this._messageSink({
  55. method: 'onBegin',
  56. params: undefined
  57. });
  58. }
  59. onTestBegin(test, result) {
  60. result[idSymbol] = (0, _utils.createGuid)();
  61. this._messageSink({
  62. method: 'onTestBegin',
  63. params: {
  64. testId: test.id,
  65. result: this._serializeResultStart(result)
  66. }
  67. });
  68. }
  69. onTestEnd(test, result) {
  70. const testEnd = {
  71. testId: test.id,
  72. expectedStatus: test.expectedStatus,
  73. annotations: test.annotations,
  74. timeout: test.timeout
  75. };
  76. this._messageSink({
  77. method: 'onTestEnd',
  78. params: {
  79. test: testEnd,
  80. result: this._serializeResultEnd(result)
  81. }
  82. });
  83. }
  84. onStepBegin(test, result, step) {
  85. step[idSymbol] = (0, _utils.createGuid)();
  86. this._messageSink({
  87. method: 'onStepBegin',
  88. params: {
  89. testId: test.id,
  90. resultId: result[idSymbol],
  91. step: this._serializeStepStart(step)
  92. }
  93. });
  94. }
  95. onStepEnd(test, result, step) {
  96. this._messageSink({
  97. method: 'onStepEnd',
  98. params: {
  99. testId: test.id,
  100. resultId: result[idSymbol],
  101. step: this._serializeStepEnd(step)
  102. }
  103. });
  104. }
  105. onError(error) {
  106. this._messageSink({
  107. method: 'onError',
  108. params: {
  109. error
  110. }
  111. });
  112. }
  113. onStdOut(chunk, test, result) {
  114. this._onStdIO('stdout', chunk, test, result);
  115. }
  116. onStdErr(chunk, test, result) {
  117. this._onStdIO('stderr', chunk, test, result);
  118. }
  119. _onStdIO(type, chunk, test, result) {
  120. const isBase64 = typeof chunk !== 'string';
  121. const data = isBase64 ? chunk.toString('base64') : chunk;
  122. this._messageSink({
  123. method: 'onStdIO',
  124. params: {
  125. testId: test === null || test === void 0 ? void 0 : test.id,
  126. resultId: result ? result[idSymbol] : undefined,
  127. type,
  128. data,
  129. isBase64
  130. }
  131. });
  132. }
  133. async onEnd(result) {
  134. const resultPayload = {
  135. status: result.status,
  136. startTime: result.startTime.getTime(),
  137. duration: result.duration
  138. };
  139. this._messageSink({
  140. method: 'onEnd',
  141. params: {
  142. result: resultPayload
  143. }
  144. });
  145. }
  146. async onExit() {}
  147. printsToStdio() {
  148. return false;
  149. }
  150. _serializeConfig(config) {
  151. var _FullConfigInternal$f;
  152. return {
  153. configFile: this._relativePath(config.configFile),
  154. globalTimeout: config.globalTimeout,
  155. maxFailures: config.maxFailures,
  156. metadata: config.metadata,
  157. rootDir: config.rootDir,
  158. version: config.version,
  159. workers: config.workers,
  160. listOnly: !!((_FullConfigInternal$f = _config.FullConfigInternal.from(config)) !== null && _FullConfigInternal$f !== void 0 && _FullConfigInternal$f.cliListOnly)
  161. };
  162. }
  163. _serializeProject(suite) {
  164. const project = suite.project();
  165. const report = {
  166. id: (0, _config.getProjectId)(project),
  167. metadata: project.metadata,
  168. name: project.name,
  169. outputDir: this._relativePath(project.outputDir),
  170. repeatEach: project.repeatEach,
  171. retries: project.retries,
  172. testDir: this._relativePath(project.testDir),
  173. testIgnore: (0, _teleReceiver.serializeRegexPatterns)(project.testIgnore),
  174. testMatch: (0, _teleReceiver.serializeRegexPatterns)(project.testMatch),
  175. timeout: project.timeout,
  176. suites: suite.suites.map(fileSuite => {
  177. return this._serializeSuite(fileSuite);
  178. }),
  179. grep: (0, _teleReceiver.serializeRegexPatterns)(project.grep),
  180. grepInvert: (0, _teleReceiver.serializeRegexPatterns)(project.grepInvert || []),
  181. dependencies: project.dependencies,
  182. snapshotDir: this._relativePath(project.snapshotDir),
  183. teardown: project.teardown
  184. };
  185. return report;
  186. }
  187. _serializeSuite(suite) {
  188. const result = {
  189. type: suite._type,
  190. title: suite.title,
  191. fileId: suite._fileId,
  192. parallelMode: suite._parallelMode,
  193. location: this._relativeLocation(suite.location),
  194. suites: suite.suites.map(s => this._serializeSuite(s)),
  195. tests: suite.tests.map(t => this._serializeTest(t))
  196. };
  197. return result;
  198. }
  199. _serializeTest(test) {
  200. return {
  201. testId: test.id,
  202. title: test.title,
  203. location: this._relativeLocation(test.location),
  204. retries: test.retries
  205. };
  206. }
  207. _serializeResultStart(result) {
  208. return {
  209. id: result[idSymbol],
  210. retry: result.retry,
  211. workerIndex: result.workerIndex,
  212. parallelIndex: result.parallelIndex,
  213. startTime: +result.startTime
  214. };
  215. }
  216. _serializeResultEnd(result) {
  217. return {
  218. id: result[idSymbol],
  219. duration: result.duration,
  220. status: result.status,
  221. errors: result.errors,
  222. attachments: this._serializeAttachments(result.attachments)
  223. };
  224. }
  225. _serializeAttachments(attachments) {
  226. return attachments.map(a => {
  227. return {
  228. ...a,
  229. // There is no Buffer in the browser, so there is no point in sending the data there.
  230. base64: a.body && !this._skipBuffers ? a.body.toString('base64') : undefined
  231. };
  232. });
  233. }
  234. _serializeStepStart(step) {
  235. var _step$parent;
  236. return {
  237. id: step[idSymbol],
  238. parentStepId: (_step$parent = step.parent) === null || _step$parent === void 0 ? void 0 : _step$parent[idSymbol],
  239. title: step.title,
  240. category: step.category,
  241. startTime: +step.startTime,
  242. location: this._relativeLocation(step.location)
  243. };
  244. }
  245. _serializeStepEnd(step) {
  246. return {
  247. id: step[idSymbol],
  248. duration: step.duration,
  249. error: step.error
  250. };
  251. }
  252. _relativeLocation(location) {
  253. if (!location) return location;
  254. return {
  255. ...location,
  256. file: this._relativePath(location.file)
  257. };
  258. }
  259. _relativePath(absolutePath) {
  260. if (!absolutePath) return absolutePath;
  261. return _path.default.relative(this._rootDir, absolutePath);
  262. }
  263. }
  264. exports.TeleReporterEmitter = TeleReporterEmitter;
  265. const idSymbol = Symbol('id');