testTracing.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. "use strict";
  2. var __create = Object.create;
  3. var __defProp = Object.defineProperty;
  4. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  5. var __getOwnPropNames = Object.getOwnPropertyNames;
  6. var __getProtoOf = Object.getPrototypeOf;
  7. var __hasOwnProp = Object.prototype.hasOwnProperty;
  8. var __export = (target, all) => {
  9. for (var name in all)
  10. __defProp(target, name, { get: all[name], enumerable: true });
  11. };
  12. var __copyProps = (to, from, except, desc) => {
  13. if (from && typeof from === "object" || typeof from === "function") {
  14. for (let key of __getOwnPropNames(from))
  15. if (!__hasOwnProp.call(to, key) && key !== except)
  16. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  17. }
  18. return to;
  19. };
  20. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  21. // If the importer is in node compatibility mode or this is not an ESM
  22. // file that has been converted to a CommonJS file using a Babel-
  23. // compatible transform (i.e. "__esModule" has not been set), then set
  24. // "default" to the CommonJS "module.exports" for node compatibility.
  25. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  26. mod
  27. ));
  28. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  29. var testTracing_exports = {};
  30. __export(testTracing_exports, {
  31. TestTracing: () => TestTracing,
  32. testTraceEntryName: () => testTraceEntryName
  33. });
  34. module.exports = __toCommonJS(testTracing_exports);
  35. var import_fs = __toESM(require("fs"));
  36. var import_path = __toESM(require("path"));
  37. var import_utils = require("playwright-core/lib/utils");
  38. var import_zipBundle = require("playwright-core/lib/zipBundle");
  39. var import_util = require("../util");
  40. const testTraceEntryName = "test.trace";
  41. const version = 8;
  42. let traceOrdinal = 0;
  43. class TestTracing {
  44. constructor(testInfo, artifactsDir) {
  45. this._traceEvents = [];
  46. this._temporaryTraceFiles = [];
  47. this._didFinishTestFunctionAndAfterEachHooks = false;
  48. this._testInfo = testInfo;
  49. this._artifactsDir = artifactsDir;
  50. this._tracesDir = import_path.default.join(this._artifactsDir, "traces");
  51. this._contextCreatedEvent = {
  52. version,
  53. type: "context-options",
  54. origin: "testRunner",
  55. browserName: "",
  56. options: {},
  57. platform: process.platform,
  58. wallTime: Date.now(),
  59. monotonicTime: (0, import_utils.monotonicTime)(),
  60. sdkLanguage: "javascript"
  61. };
  62. this._appendTraceEvent(this._contextCreatedEvent);
  63. }
  64. _shouldCaptureTrace() {
  65. if (this._options?.mode === "on")
  66. return true;
  67. if (this._options?.mode === "retain-on-failure")
  68. return true;
  69. if (this._options?.mode === "on-first-retry" && this._testInfo.retry === 1)
  70. return true;
  71. if (this._options?.mode === "on-all-retries" && this._testInfo.retry > 0)
  72. return true;
  73. if (this._options?.mode === "retain-on-first-failure" && this._testInfo.retry === 0)
  74. return true;
  75. return false;
  76. }
  77. async startIfNeeded(value) {
  78. const defaultTraceOptions = { screenshots: true, snapshots: true, sources: true, attachments: true, _live: false, mode: "off" };
  79. if (!value) {
  80. this._options = defaultTraceOptions;
  81. } else if (typeof value === "string") {
  82. this._options = { ...defaultTraceOptions, mode: value === "retry-with-trace" ? "on-first-retry" : value };
  83. } else {
  84. const mode = value.mode || "off";
  85. this._options = { ...defaultTraceOptions, ...value, mode: mode === "retry-with-trace" ? "on-first-retry" : mode };
  86. }
  87. if (!this._shouldCaptureTrace()) {
  88. this._options = void 0;
  89. return;
  90. }
  91. if (!this._liveTraceFile && this._options._live) {
  92. this._liveTraceFile = { file: import_path.default.join(this._tracesDir, `${this._testInfo.testId}-test.trace`), fs: new import_utils.SerializedFS() };
  93. this._liveTraceFile.fs.mkdir(import_path.default.dirname(this._liveTraceFile.file));
  94. const data = this._traceEvents.map((e) => JSON.stringify(e)).join("\n") + "\n";
  95. this._liveTraceFile.fs.writeFile(this._liveTraceFile.file, data);
  96. }
  97. }
  98. didFinishTestFunctionAndAfterEachHooks() {
  99. this._didFinishTestFunctionAndAfterEachHooks = true;
  100. }
  101. artifactsDir() {
  102. return this._artifactsDir;
  103. }
  104. tracesDir() {
  105. return this._tracesDir;
  106. }
  107. traceTitle() {
  108. return [import_path.default.relative(this._testInfo.project.testDir, this._testInfo.file) + ":" + this._testInfo.line, ...this._testInfo.titlePath.slice(1)].join(" \u203A ");
  109. }
  110. generateNextTraceRecordingName() {
  111. const ordinalSuffix = traceOrdinal ? `-recording${traceOrdinal}` : "";
  112. ++traceOrdinal;
  113. const retrySuffix = this._testInfo.retry ? `-retry${this._testInfo.retry}` : "";
  114. return `${this._testInfo.testId}${retrySuffix}${ordinalSuffix}`;
  115. }
  116. _generateNextTraceRecordingPath() {
  117. const file = import_path.default.join(this._artifactsDir, (0, import_utils.createGuid)() + ".zip");
  118. this._temporaryTraceFiles.push(file);
  119. return file;
  120. }
  121. traceOptions() {
  122. return this._options;
  123. }
  124. maybeGenerateNextTraceRecordingPath() {
  125. if (this._didFinishTestFunctionAndAfterEachHooks && this._shouldAbandonTrace())
  126. return;
  127. return this._generateNextTraceRecordingPath();
  128. }
  129. _shouldAbandonTrace() {
  130. if (!this._options)
  131. return true;
  132. const testFailed = this._testInfo.status !== this._testInfo.expectedStatus;
  133. return !testFailed && (this._options.mode === "retain-on-failure" || this._options.mode === "retain-on-first-failure");
  134. }
  135. async stopIfNeeded() {
  136. if (!this._options)
  137. return;
  138. const error = await this._liveTraceFile?.fs.syncAndGetError();
  139. if (error)
  140. throw error;
  141. if (this._shouldAbandonTrace()) {
  142. for (const file of this._temporaryTraceFiles)
  143. await import_fs.default.promises.unlink(file).catch(() => {
  144. });
  145. return;
  146. }
  147. const zipFile = new import_zipBundle.yazl.ZipFile();
  148. if (!this._options?.attachments) {
  149. for (const event of this._traceEvents) {
  150. if (event.type === "after")
  151. delete event.attachments;
  152. }
  153. }
  154. if (this._options?.sources) {
  155. const sourceFiles = /* @__PURE__ */ new Set();
  156. for (const event of this._traceEvents) {
  157. if (event.type === "before") {
  158. for (const frame of event.stack || [])
  159. sourceFiles.add(frame.file);
  160. }
  161. }
  162. for (const sourceFile of sourceFiles) {
  163. await import_fs.default.promises.readFile(sourceFile, "utf8").then((source) => {
  164. zipFile.addBuffer(Buffer.from(source), "resources/src@" + (0, import_utils.calculateSha1)(sourceFile) + ".txt");
  165. }).catch(() => {
  166. });
  167. }
  168. }
  169. const sha1s = /* @__PURE__ */ new Set();
  170. for (const event of this._traceEvents.filter((e) => e.type === "after")) {
  171. for (const attachment of event.attachments || []) {
  172. let contentPromise;
  173. if (attachment.path)
  174. contentPromise = import_fs.default.promises.readFile(attachment.path).catch(() => void 0);
  175. else if (attachment.base64)
  176. contentPromise = Promise.resolve(Buffer.from(attachment.base64, "base64"));
  177. const content = await contentPromise;
  178. if (content === void 0)
  179. continue;
  180. const sha1 = (0, import_utils.calculateSha1)(content);
  181. attachment.sha1 = sha1;
  182. delete attachment.path;
  183. delete attachment.base64;
  184. if (sha1s.has(sha1))
  185. continue;
  186. sha1s.add(sha1);
  187. zipFile.addBuffer(content, "resources/" + sha1);
  188. }
  189. }
  190. const traceContent = Buffer.from(this._traceEvents.map((e) => JSON.stringify(e)).join("\n"));
  191. zipFile.addBuffer(traceContent, testTraceEntryName);
  192. await new Promise((f) => {
  193. zipFile.end(void 0, () => {
  194. zipFile.outputStream.pipe(import_fs.default.createWriteStream(this._generateNextTraceRecordingPath())).on("close", f);
  195. });
  196. });
  197. const tracePath = this._testInfo.outputPath("trace.zip");
  198. await mergeTraceFiles(tracePath, this._temporaryTraceFiles);
  199. this._testInfo.attachments.push({ name: "trace", path: tracePath, contentType: "application/zip" });
  200. }
  201. appendForError(error) {
  202. const rawStack = error.stack?.split("\n") || [];
  203. const stack = rawStack ? (0, import_util.filteredStackTrace)(rawStack) : [];
  204. this._appendTraceEvent({
  205. type: "error",
  206. message: this._formatError(error),
  207. stack
  208. });
  209. }
  210. _formatError(error) {
  211. const parts = [error.message || String(error.value)];
  212. if (error.cause)
  213. parts.push("[cause]: " + this._formatError(error.cause));
  214. return parts.join("\n");
  215. }
  216. appendStdioToTrace(type, chunk) {
  217. this._appendTraceEvent({
  218. type,
  219. timestamp: (0, import_utils.monotonicTime)(),
  220. text: typeof chunk === "string" ? chunk : void 0,
  221. base64: typeof chunk === "string" ? void 0 : chunk.toString("base64")
  222. });
  223. }
  224. appendBeforeActionForStep(options) {
  225. this._appendTraceEvent({
  226. type: "before",
  227. callId: options.stepId,
  228. stepId: options.stepId,
  229. parentId: options.parentId,
  230. startTime: (0, import_utils.monotonicTime)(),
  231. class: "Test",
  232. method: options.category,
  233. title: options.title,
  234. params: Object.fromEntries(Object.entries(options.params || {}).map(([name, value]) => [name, generatePreview(value)])),
  235. stack: options.stack,
  236. group: options.group
  237. });
  238. }
  239. appendAfterActionForStep(callId, error, attachments = [], annotations) {
  240. this._appendTraceEvent({
  241. type: "after",
  242. callId,
  243. endTime: (0, import_utils.monotonicTime)(),
  244. attachments: serializeAttachments(attachments),
  245. annotations,
  246. error
  247. });
  248. }
  249. _appendTraceEvent(event) {
  250. this._traceEvents.push(event);
  251. if (this._liveTraceFile)
  252. this._liveTraceFile.fs.appendFile(this._liveTraceFile.file, JSON.stringify(event) + "\n", true);
  253. }
  254. }
  255. function serializeAttachments(attachments) {
  256. if (attachments.length === 0)
  257. return void 0;
  258. return attachments.filter((a) => a.name !== "trace").map((a) => {
  259. return {
  260. name: a.name,
  261. contentType: a.contentType,
  262. path: a.path,
  263. base64: a.body?.toString("base64")
  264. };
  265. });
  266. }
  267. function generatePreview(value, visited = /* @__PURE__ */ new Set()) {
  268. if (visited.has(value))
  269. return "";
  270. visited.add(value);
  271. if (typeof value === "string")
  272. return value;
  273. if (typeof value === "number")
  274. return value.toString();
  275. if (typeof value === "boolean")
  276. return value.toString();
  277. if (value === null)
  278. return "null";
  279. if (value === void 0)
  280. return "undefined";
  281. if (Array.isArray(value))
  282. return "[" + value.map((v) => generatePreview(v, visited)).join(", ") + "]";
  283. if (typeof value === "object")
  284. return "Object";
  285. return String(value);
  286. }
  287. async function mergeTraceFiles(fileName, temporaryTraceFiles) {
  288. temporaryTraceFiles = temporaryTraceFiles.filter((file) => import_fs.default.existsSync(file));
  289. if (temporaryTraceFiles.length === 1) {
  290. await import_fs.default.promises.rename(temporaryTraceFiles[0], fileName);
  291. return;
  292. }
  293. const mergePromise = new import_utils.ManualPromise();
  294. const zipFile = new import_zipBundle.yazl.ZipFile();
  295. const entryNames = /* @__PURE__ */ new Set();
  296. zipFile.on("error", (error) => mergePromise.reject(error));
  297. for (let i = temporaryTraceFiles.length - 1; i >= 0; --i) {
  298. const tempFile = temporaryTraceFiles[i];
  299. const promise = new import_utils.ManualPromise();
  300. import_zipBundle.yauzl.open(tempFile, (err, inZipFile) => {
  301. if (err) {
  302. promise.reject(err);
  303. return;
  304. }
  305. let pendingEntries = inZipFile.entryCount;
  306. inZipFile.on("entry", (entry) => {
  307. let entryName = entry.fileName;
  308. if (entry.fileName === testTraceEntryName) {
  309. } else if (entry.fileName.match(/trace\.[a-z]*$/)) {
  310. entryName = i + "-" + entry.fileName;
  311. }
  312. if (entryNames.has(entryName)) {
  313. if (--pendingEntries === 0)
  314. promise.resolve();
  315. return;
  316. }
  317. entryNames.add(entryName);
  318. inZipFile.openReadStream(entry, (err2, readStream) => {
  319. if (err2) {
  320. promise.reject(err2);
  321. return;
  322. }
  323. zipFile.addReadStream(readStream, entryName);
  324. if (--pendingEntries === 0)
  325. promise.resolve();
  326. });
  327. });
  328. });
  329. await promise;
  330. }
  331. zipFile.end(void 0, () => {
  332. zipFile.outputStream.pipe(import_fs.default.createWriteStream(fileName)).on("close", () => {
  333. void Promise.all(temporaryTraceFiles.map((tempFile) => import_fs.default.promises.unlink(tempFile))).then(() => {
  334. mergePromise.resolve();
  335. }).catch((error) => mergePromise.reject(error));
  336. }).on("error", (error) => mergePromise.reject(error));
  337. });
  338. await mergePromise;
  339. }
  340. // Annotate the CommonJS export names for ESM import in node:
  341. 0 && (module.exports = {
  342. TestTracing,
  343. testTraceEntryName
  344. });