testInfo.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TestInfoImpl = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _utils = require("playwright-core/lib/utils");
  9. var _timeoutManager = require("./timeoutManager");
  10. var _util = require("../util");
  11. var _testTracing = require("./testTracing");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Copyright Microsoft Corporation. All rights reserved.
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. class TestInfoImpl {
  29. get error() {
  30. return this.errors[0];
  31. }
  32. set error(e) {
  33. if (e === undefined) throw new Error('Cannot assign testInfo.error undefined value!');
  34. this.errors[0] = e;
  35. }
  36. get timeout() {
  37. return this._timeoutManager.defaultSlotTimings().timeout;
  38. }
  39. set timeout(timeout) {
  40. // Ignored.
  41. }
  42. _deadlineForMatcher(timeout) {
  43. const startTime = (0, _utils.monotonicTime)();
  44. const matcherDeadline = timeout ? startTime + timeout : _utils.MaxTime;
  45. const testDeadline = this._timeoutManager.currentSlotDeadline() - 250;
  46. const matcherMessage = `Timeout ${timeout}ms exceeded while waiting on the predicate`;
  47. const testMessage = `Test timeout of ${this.timeout}ms exceeded`;
  48. return {
  49. deadline: Math.min(testDeadline, matcherDeadline),
  50. timeoutMessage: testDeadline < matcherDeadline ? testMessage : matcherMessage
  51. };
  52. }
  53. static _defaultDeadlineForMatcher(timeout) {
  54. return {
  55. deadline: timeout ? (0, _utils.monotonicTime)() + timeout : 0,
  56. timeoutMessage: `Timeout ${timeout}ms exceeded while waiting on the predicate`
  57. };
  58. }
  59. constructor(configInternal, projectInternal, workerParams, test, retry, onStepBegin, onStepEnd, onAttach) {
  60. this._onStepBegin = void 0;
  61. this._onStepEnd = void 0;
  62. this._onAttach = void 0;
  63. this._test = void 0;
  64. this._timeoutManager = void 0;
  65. this._startTime = void 0;
  66. this._startWallTime = void 0;
  67. this._hasHardError = false;
  68. this._tracing = new _testTracing.TestTracing();
  69. this._didTimeout = false;
  70. this._wasInterrupted = false;
  71. this._lastStepId = 0;
  72. this._projectInternal = void 0;
  73. this._configInternal = void 0;
  74. this._steps = [];
  75. this._beforeHooksStep = void 0;
  76. this._afterHooksStep = void 0;
  77. this._onDidFinishTestFunction = void 0;
  78. // ------------ TestInfo fields ------------
  79. this.testId = void 0;
  80. this.repeatEachIndex = void 0;
  81. this.retry = void 0;
  82. this.workerIndex = void 0;
  83. this.parallelIndex = void 0;
  84. this.project = void 0;
  85. this.config = void 0;
  86. this.title = void 0;
  87. this.titlePath = void 0;
  88. this.file = void 0;
  89. this.line = void 0;
  90. this.column = void 0;
  91. this.fn = void 0;
  92. this.expectedStatus = void 0;
  93. this.duration = 0;
  94. this.annotations = [];
  95. this.attachments = [];
  96. this.status = 'passed';
  97. this.stdout = [];
  98. this.stderr = [];
  99. this.snapshotSuffix = '';
  100. this.outputDir = void 0;
  101. this.snapshotDir = void 0;
  102. this.errors = [];
  103. this._attachmentsPush = void 0;
  104. this._test = test;
  105. this.testId = test.id;
  106. this._onStepBegin = onStepBegin;
  107. this._onStepEnd = onStepEnd;
  108. this._onAttach = onAttach;
  109. this._startTime = (0, _utils.monotonicTime)();
  110. this._startWallTime = Date.now();
  111. this.repeatEachIndex = workerParams.repeatEachIndex;
  112. this.retry = retry;
  113. this.workerIndex = workerParams.workerIndex;
  114. this.parallelIndex = workerParams.parallelIndex;
  115. this._projectInternal = projectInternal;
  116. this.project = projectInternal.project;
  117. this._configInternal = configInternal;
  118. this.config = configInternal.config;
  119. this.title = test.title;
  120. this.titlePath = test.titlePath();
  121. this.file = test.location.file;
  122. this.line = test.location.line;
  123. this.column = test.location.column;
  124. this.fn = test.fn;
  125. this.expectedStatus = test.expectedStatus;
  126. this._timeoutManager = new _timeoutManager.TimeoutManager(this.project.timeout);
  127. this.outputDir = (() => {
  128. const relativeTestFilePath = _path.default.relative(this.project.testDir, test._requireFile.replace(/\.(spec|test)\.(js|ts|mjs)$/, ''));
  129. const sanitizedRelativePath = relativeTestFilePath.replace(process.platform === 'win32' ? new RegExp('\\\\', 'g') : new RegExp('/', 'g'), '-');
  130. const fullTitleWithoutSpec = test.titlePath().slice(1).join(' ');
  131. let testOutputDir = (0, _util.trimLongString)(sanitizedRelativePath + '-' + (0, _utils.sanitizeForFilePath)(fullTitleWithoutSpec));
  132. if (projectInternal.id) testOutputDir += '-' + (0, _utils.sanitizeForFilePath)(projectInternal.id);
  133. if (this.retry) testOutputDir += '-retry' + this.retry;
  134. if (this.repeatEachIndex) testOutputDir += '-repeat' + this.repeatEachIndex;
  135. return _path.default.join(this.project.outputDir, testOutputDir);
  136. })();
  137. this.snapshotDir = (() => {
  138. const relativeTestFilePath = _path.default.relative(this.project.testDir, test._requireFile);
  139. return _path.default.join(this.project.snapshotDir, relativeTestFilePath + '-snapshots');
  140. })();
  141. this._attachmentsPush = this.attachments.push.bind(this.attachments);
  142. this.attachments.push = (...attachments) => {
  143. for (const a of attachments) this._attach(a.name, a);
  144. return this.attachments.length;
  145. };
  146. }
  147. _modifier(type, modifierArgs) {
  148. if (typeof modifierArgs[1] === 'function') {
  149. throw new Error(['It looks like you are calling test.skip() inside the test and pass a callback.', 'Pass a condition instead and optional description instead:', `test('my test', async ({ page, isMobile }) => {`, ` test.skip(isMobile, 'This test is not applicable on mobile');`, `});`].join('\n'));
  150. }
  151. if (modifierArgs.length >= 1 && !modifierArgs[0]) return;
  152. const description = modifierArgs[1];
  153. this.annotations.push({
  154. type,
  155. description
  156. });
  157. if (type === 'slow') {
  158. this._timeoutManager.slow();
  159. } else if (type === 'skip' || type === 'fixme') {
  160. this.expectedStatus = 'skipped';
  161. throw new SkipError('Test is skipped: ' + (description || ''));
  162. } else if (type === 'fail') {
  163. if (this.expectedStatus !== 'skipped') this.expectedStatus = 'failed';
  164. }
  165. }
  166. async _runWithTimeout(cb) {
  167. const timeoutError = await this._timeoutManager.runWithTimeout(cb);
  168. // When interrupting, we arrive here with a timeoutError, but we should not
  169. // consider it a timeout.
  170. if (!this._wasInterrupted && timeoutError && !this._didTimeout) {
  171. this._didTimeout = true;
  172. this.errors.push(timeoutError);
  173. // Do not overwrite existing failure upon hook/teardown timeout.
  174. if (this.status === 'passed' || this.status === 'skipped') this.status = 'timedOut';
  175. }
  176. this.duration = this._timeoutManager.defaultSlotTimings().elapsed | 0;
  177. }
  178. async _runAndFailOnError(fn, skips) {
  179. try {
  180. await fn();
  181. } catch (error) {
  182. if (skips === 'allowSkips' && error instanceof SkipError) {
  183. if (this.status === 'passed') this.status = 'skipped';
  184. } else {
  185. this._failWithError(error, true /* isHardError */);
  186. return error;
  187. }
  188. }
  189. }
  190. _addStep(data, parentStep) {
  191. var _parentStep, _parentStep2;
  192. const stepId = `${data.category}@${++this._lastStepId}`;
  193. const rawStack = (0, _utils.captureRawStack)();
  194. if (!parentStep) parentStep = _utils.zones.zoneData('stepZone', rawStack) || undefined;
  195. // For out-of-stack calls, locate the enclosing step.
  196. let isLaxParent = false;
  197. if (!parentStep && data.laxParent) {
  198. const visit = step => {
  199. // Do not nest chains of route.continue.
  200. const shouldNest = step.title !== data.title;
  201. if (!step.endWallTime && shouldNest) parentStep = step;
  202. step.steps.forEach(visit);
  203. };
  204. this._steps.forEach(visit);
  205. isLaxParent = !!parentStep;
  206. }
  207. const filteredStack = (0, _util.filteredStackTrace)(rawStack);
  208. data.boxedStack = (_parentStep = parentStep) === null || _parentStep === void 0 ? void 0 : _parentStep.boxedStack;
  209. if (!data.boxedStack && data.box) {
  210. data.boxedStack = filteredStack.slice(1);
  211. data.location = data.location || data.boxedStack[0];
  212. }
  213. data.location = data.location || filteredStack[0];
  214. const step = {
  215. stepId,
  216. ...data,
  217. laxParent: isLaxParent,
  218. steps: [],
  219. complete: result => {
  220. if (step.endWallTime) return;
  221. step.endWallTime = Date.now();
  222. if (result.error) {
  223. if (!result.error[stepSymbol]) result.error[stepSymbol] = step;
  224. const error = (0, _util.serializeError)(result.error);
  225. if (data.boxedStack) error.stack = `${error.message}\n${(0, _utils.stringifyStackFrames)(data.boxedStack).join('\n')}`;
  226. step.error = error;
  227. }
  228. if (!step.error) {
  229. // Soft errors inside try/catch will make the test fail.
  230. // In order to locate the failing step, we are marking all the parent
  231. // steps as failing unconditionally.
  232. for (const childStep of step.steps) {
  233. if (childStep.error && childStep.infectParentStepsWithError) {
  234. step.error = childStep.error;
  235. step.infectParentStepsWithError = true;
  236. break;
  237. }
  238. }
  239. }
  240. const payload = {
  241. testId: this._test.id,
  242. stepId,
  243. wallTime: step.endWallTime,
  244. error: step.error
  245. };
  246. this._onStepEnd(payload);
  247. const errorForTrace = step.error ? {
  248. name: '',
  249. message: step.error.message || '',
  250. stack: step.error.stack
  251. } : undefined;
  252. this._tracing.appendAfterActionForStep(stepId, errorForTrace, result.attachments);
  253. if (step.isSoft && result.error) this._failWithError(result.error, false /* isHardError */);
  254. }
  255. };
  256. const parentStepList = parentStep ? parentStep.steps : this._steps;
  257. parentStepList.push(step);
  258. const payload = {
  259. testId: this._test.id,
  260. stepId,
  261. parentStepId: parentStep ? parentStep.stepId : undefined,
  262. title: data.title,
  263. category: data.category,
  264. wallTime: data.wallTime,
  265. location: data.location
  266. };
  267. this._onStepBegin(payload);
  268. this._tracing.appendBeforeActionForStep(stepId, (_parentStep2 = parentStep) === null || _parentStep2 === void 0 ? void 0 : _parentStep2.stepId, data.apiName || data.title, data.params, data.wallTime, data.location ? [data.location] : []);
  269. return step;
  270. }
  271. _interrupt() {
  272. // Mark as interrupted so we can ignore TimeoutError thrown by interrupt() call.
  273. this._wasInterrupted = true;
  274. this._timeoutManager.interrupt();
  275. // Do not overwrite existing failure (for example, unhandled rejection) with "interrupted".
  276. if (this.status === 'passed') this.status = 'interrupted';
  277. }
  278. _failWithError(error, isHardError) {
  279. // Do not overwrite any previous hard errors.
  280. // Some (but not all) scenarios include:
  281. // - expect() that fails after uncaught exception.
  282. // - fail after the timeout, e.g. due to fixture teardown.
  283. if (isHardError && this._hasHardError) return;
  284. if (isHardError) this._hasHardError = true;
  285. if (this.status === 'passed' || this.status === 'skipped') this.status = 'failed';
  286. const serialized = (0, _util.serializeError)(error);
  287. const step = error[stepSymbol];
  288. if (step && step.boxedStack) serialized.stack = `${error.name}: ${error.message}\n${(0, _utils.stringifyStackFrames)(step.boxedStack).join('\n')}`;
  289. this.errors.push(serialized);
  290. }
  291. async _runAsStepWithRunnable(stepInfo, cb) {
  292. return await this._timeoutManager.withRunnable({
  293. type: stepInfo.runnableType,
  294. slot: stepInfo.runnableSlot,
  295. location: stepInfo.location
  296. }, async () => {
  297. return await this._runAsStep(stepInfo, cb);
  298. });
  299. }
  300. async _runAsStep(stepInfo, cb) {
  301. const step = this._addStep({
  302. wallTime: Date.now(),
  303. ...stepInfo
  304. });
  305. return await _utils.zones.run('stepZone', step, async () => {
  306. try {
  307. const result = await cb(step);
  308. step.complete({});
  309. return result;
  310. } catch (e) {
  311. step.complete({
  312. error: e instanceof SkipError ? undefined : e
  313. });
  314. throw e;
  315. }
  316. });
  317. }
  318. _isFailure() {
  319. return this.status !== 'skipped' && this.status !== this.expectedStatus;
  320. }
  321. // ------------ TestInfo methods ------------
  322. async attach(name, options = {}) {
  323. this._attach(name, await (0, _util.normalizeAndSaveAttachment)(this.outputPath(), name, options));
  324. }
  325. _attach(name, attachment) {
  326. var _attachment$body;
  327. const step = this._addStep({
  328. title: `attach "${name}"`,
  329. category: 'attach',
  330. wallTime: Date.now(),
  331. laxParent: true
  332. });
  333. this._attachmentsPush(attachment);
  334. this._onAttach({
  335. testId: this._test.id,
  336. name: attachment.name,
  337. contentType: attachment.contentType,
  338. path: attachment.path,
  339. body: (_attachment$body = attachment.body) === null || _attachment$body === void 0 ? void 0 : _attachment$body.toString('base64')
  340. });
  341. step.complete({
  342. attachments: [attachment]
  343. });
  344. }
  345. outputPath(...pathSegments) {
  346. const outputPath = this._getOutputPath(...pathSegments);
  347. _fs.default.mkdirSync(this.outputDir, {
  348. recursive: true
  349. });
  350. return outputPath;
  351. }
  352. _getOutputPath(...pathSegments) {
  353. const joinedPath = _path.default.join(...pathSegments);
  354. const outputPath = (0, _util.getContainedPath)(this.outputDir, joinedPath);
  355. if (outputPath) return outputPath;
  356. throw new Error(`The outputPath is not allowed outside of the parent directory. Please fix the defined path.\n\n\toutputPath: ${joinedPath}`);
  357. }
  358. _fsSanitizedTestName() {
  359. const fullTitleWithoutSpec = this.titlePath.slice(1).join(' ');
  360. return (0, _utils.sanitizeForFilePath)((0, _util.trimLongString)(fullTitleWithoutSpec));
  361. }
  362. snapshotPath(...pathSegments) {
  363. const subPath = _path.default.join(...pathSegments);
  364. const parsedSubPath = _path.default.parse(subPath);
  365. const relativeTestFilePath = _path.default.relative(this.project.testDir, this._test._requireFile);
  366. const parsedRelativeTestFilePath = _path.default.parse(relativeTestFilePath);
  367. const projectNamePathSegment = (0, _utils.sanitizeForFilePath)(this.project.name);
  368. const snapshotPath = (this._projectInternal.snapshotPathTemplate || '').replace(/\{(.)?testDir\}/g, '$1' + this.project.testDir).replace(/\{(.)?snapshotDir\}/g, '$1' + this.project.snapshotDir).replace(/\{(.)?snapshotSuffix\}/g, this.snapshotSuffix ? '$1' + this.snapshotSuffix : '').replace(/\{(.)?testFileDir\}/g, '$1' + parsedRelativeTestFilePath.dir).replace(/\{(.)?platform\}/g, '$1' + process.platform).replace(/\{(.)?projectName\}/g, projectNamePathSegment ? '$1' + projectNamePathSegment : '').replace(/\{(.)?testName\}/g, '$1' + this._fsSanitizedTestName()).replace(/\{(.)?testFileName\}/g, '$1' + parsedRelativeTestFilePath.base).replace(/\{(.)?testFilePath\}/g, '$1' + relativeTestFilePath).replace(/\{(.)?arg\}/g, '$1' + _path.default.join(parsedSubPath.dir, parsedSubPath.name)).replace(/\{(.)?ext\}/g, parsedSubPath.ext ? '$1' + parsedSubPath.ext : '');
  369. return _path.default.normalize(_path.default.resolve(this._configInternal.configDir, snapshotPath));
  370. }
  371. skip(...args) {
  372. this._modifier('skip', args);
  373. }
  374. fixme(...args) {
  375. this._modifier('fixme', args);
  376. }
  377. fail(...args) {
  378. this._modifier('fail', args);
  379. }
  380. slow(...args) {
  381. this._modifier('slow', args);
  382. }
  383. setTimeout(timeout) {
  384. this._timeoutManager.setTimeout(timeout);
  385. }
  386. }
  387. exports.TestInfoImpl = TestInfoImpl;
  388. class SkipError extends Error {}
  389. const stepSymbol = Symbol('step');