How to use isInternalFileName method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

stackTrace.js

Source: stackTrace.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.captureRawStack = captureRawStack;
7exports.captureStackTrace = captureStackTrace;
8exports.isInternalFileName = isInternalFileName;
9exports.rewriteErrorMessage = rewriteErrorMessage;
10exports.splitErrorMessage = splitErrorMessage;
11
12var _path = _interopRequireDefault(require("path"));
13
14var _stackUtils = _interopRequireDefault(require("stack-utils"));
15
16var _utils = require("./utils");
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20/**
21 * Copyright (c) Microsoft Corporation.
22 *
23 * Licensed under the Apache License, Version 2.0 (the "License");
24 * you may not use this file except in compliance with the License.
25 * You may obtain a copy of the License at
26 *
27 * http://www.apache.org/licenses/LICENSE-2.0
28 *
29 * Unless required by applicable law or agreed to in writing, software
30 * distributed under the License is distributed on an "AS IS" BASIS,
31 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
32 * See the License for the specific language governing permissions and
33 * limitations under the License.
34 */
35const stackUtils = new _stackUtils.default();
36
37function rewriteErrorMessage(e, newMessage) {
38  var _e$stack;
39
40  const lines = (((_e$stack = e.stack) === null || _e$stack === void 0 ? void 0 : _e$stack.split('\n')) || []).filter(l => l.startsWith('    at '));
41  e.message = newMessage;
42  const errorTitle = `${e.name}: ${e.message}`;
43  if (lines.length) e.stack = `${errorTitle}\n${lines.join('\n')}`;
44  return e;
45}
46
47const CORE_DIR = _path.default.resolve(__dirname, '..', '..');
48
49const CORE_LIB = _path.default.join(CORE_DIR, 'lib');
50
51const CORE_SRC = _path.default.join(CORE_DIR, 'src');
52
53const TEST_DIR_SRC = _path.default.resolve(CORE_DIR, '..', 'playwright-test');
54
55const TEST_DIR_LIB = _path.default.resolve(CORE_DIR, '..', '@playwright', 'test');
56
57const COVERAGE_PATH = _path.default.join(CORE_DIR, '..', '..', 'tests', 'config', 'coverage.js');
58
59const WS_LIB = _path.default.relative(process.cwd(), _path.default.dirname(require.resolve('ws')));
60
61function captureRawStack() {
62  const stackTraceLimit = Error.stackTraceLimit;
63  Error.stackTraceLimit = 30;
64  const error = new Error();
65  const stack = error.stack;
66  Error.stackTraceLimit = stackTraceLimit;
67  return stack;
68}
69
70function isInternalFileName(file, functionName) {
71  // Node 16+ has node:internal.
72  if (file.startsWith('internal') || file.startsWith('node:')) return true; // EventEmitter.emit has 'events.js' file.
73
74  if (file === 'events.js' && functionName !== null && functionName !== void 0 && functionName.endsWith('emit')) return true; // Node 12
75
76  if (file === '_stream_readable.js' || file === '_stream_writable.js') return true;
77  if (file.startsWith(WS_LIB)) return true;
78  return false;
79}
80
81function captureStackTrace(rawStack) {
82  const stack = rawStack || captureRawStack();
83  const isTesting = (0, _utils.isUnderTest)();
84  let parsedFrames = stack.split('\n').map(line => {
85    const frame = stackUtils.parseLine(line);
86    if (!frame || !frame.file) return null;
87    if (isInternalFileName(frame.file, frame.function)) return null; // Workaround for https://github.com/tapjs/stack-utils/issues/60
88
89    let fileName;
90    if (frame.file.startsWith('file://')) fileName = new URL(frame.file).pathname;else fileName = _path.default.resolve(process.cwd(), frame.file);
91    if (isTesting && fileName.includes(COVERAGE_PATH)) return null;
92    const inCore = fileName.startsWith(CORE_LIB) || fileName.startsWith(CORE_SRC);
93    const parsed = {
94      frame: {
95        file: fileName,
96        line: frame.line,
97        column: frame.column,
98        function: frame.function
99      },
100      frameText: line,
101      inCore
102    };
103    return parsed;
104  }).filter(Boolean);
105  let apiName = '';
106  const allFrames = parsedFrames; // expect matchers have the following stack structure:
107  // at Object.__PWTRAP__[expect.toHaveText] (...)
108  // at __EXTERNAL_MATCHER_TRAP__ (...)
109  // at Object.throwingMatcher [as toHaveText] (...)
110
111  const TRAP = '__PWTRAP__[';
112  const expectIndex = parsedFrames.findIndex(f => f.frameText.includes(TRAP));
113
114  if (expectIndex !== -1) {
115    const text = parsedFrames[expectIndex].frameText;
116    const aliasIndex = text.indexOf(TRAP);
117    apiName = text.substring(aliasIndex + TRAP.length, text.indexOf(']'));
118    parsedFrames = parsedFrames.slice(expectIndex + 3);
119  } else {
120    // Deepest transition between non-client code calling into client code
121    // is the api entry.
122    for (let i = 0; i < parsedFrames.length - 1; i++) {
123      if (parsedFrames[i].inCore && !parsedFrames[i + 1].inCore) {
124        const frame = parsedFrames[i].frame;
125        apiName = normalizeAPIName(frame.function);
126        parsedFrames = parsedFrames.slice(i + 1);
127        break;
128      }
129    }
130  }
131
132  function normalizeAPIName(name) {
133    if (!name) return '';
134    const match = name.match(/(API|JS|CDP|[A-Z])(.*)/);
135    if (!match) return name;
136    return match[1].toLowerCase() + match[2];
137  } // Hide all test runner and library frames in the user stack (event handlers produce them).
138
139
140  parsedFrames = parsedFrames.filter((f, i) => {
141    if (f.frame.file.startsWith(TEST_DIR_SRC) || f.frame.file.startsWith(TEST_DIR_LIB)) return false;
142    if (i && f.frame.file.startsWith(CORE_DIR)) return false;
143    return true;
144  });
145  return {
146    allFrames: allFrames.map(p => p.frame),
147    frames: parsedFrames.map(p => p.frame),
148    frameTexts: parsedFrames.map(p => p.frameText),
149    apiName
150  };
151}
152
153function splitErrorMessage(message) {
154  const separationIdx = message.indexOf(':');
155  return {
156    name: separationIdx !== -1 ? message.slice(0, separationIdx) : '',
157    message: separationIdx !== -1 && separationIdx + 2 <= message.length ? message.substring(separationIdx + 2) : message
158  };
159}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)