How to use errorWithLocation 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.

utils.js

Source: utils.js Github

copy
1// @flow
2import { type Position } from "./tokenizer";
3
4export function positionToString( pos: Position ) {
5  return `${pos.line}:${pos.column}`;
6}
7
8class ErrorWithLocation extends Error {
9
10  +originalMessage: string;
11  +loc: Position;
12
13  constructor( message: string, loc: Position ) {
14    super( `${message} (at ${positionToString( loc )})` );
15    this.originalMessage = message;
16    this.loc = loc;
17  }
18
19}
20
21export function error( message: string, loc: Position ): ErrorWithLocation {
22  return new ErrorWithLocation( message, loc );
23}
24
Full Screen

testType.js

Source: testType.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.rootTestType = exports.TestTypeImpl = exports.DeclaredFixtures = void 0;
7
8var _expect = require("./expect");
9
10var _globals = require("./globals");
11
12var _test = require("./test");
13
14var _transform = require("./transform");
15
16var _util = require("./util");
17
18/**
19 * Copyright (c) Microsoft Corporation.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 */
33const countByFile = new Map();
34
35class DeclaredFixtures {
36  constructor() {
37    this.testType = void 0;
38    this.location = void 0;
39  }
40
41}
42
43exports.DeclaredFixtures = DeclaredFixtures;
44
45class TestTypeImpl {
46  constructor(fixtures) {
47    this.fixtures = void 0;
48    this.test = void 0;
49    this.fixtures = fixtures;
50    const test = (0, _transform.wrapFunctionWithLocation)(this._createTest.bind(this, 'default'));
51    test.expect = _expect.expect;
52    test.only = (0, _transform.wrapFunctionWithLocation)(this._createTest.bind(this, 'only'));
53    test.describe = (0, _transform.wrapFunctionWithLocation)(this._describe.bind(this, 'default'));
54    test.describe.only = (0, _transform.wrapFunctionWithLocation)(this._describe.bind(this, 'only'));
55    test.describe.serial = (0, _transform.wrapFunctionWithLocation)(this._describe.bind(this, 'serial'));
56    test.describe.serial.only = (0, _transform.wrapFunctionWithLocation)(this._describe.bind(this, 'serial.only'));
57    test.beforeEach = (0, _transform.wrapFunctionWithLocation)(this._hook.bind(this, 'beforeEach'));
58    test.afterEach = (0, _transform.wrapFunctionWithLocation)(this._hook.bind(this, 'afterEach'));
59    test.beforeAll = (0, _transform.wrapFunctionWithLocation)(this._hook.bind(this, 'beforeAll'));
60    test.afterAll = (0, _transform.wrapFunctionWithLocation)(this._hook.bind(this, 'afterAll'));
61    test.skip = (0, _transform.wrapFunctionWithLocation)(this._modifier.bind(this, 'skip'));
62    test.fixme = (0, _transform.wrapFunctionWithLocation)(this._modifier.bind(this, 'fixme'));
63    test.fail = (0, _transform.wrapFunctionWithLocation)(this._modifier.bind(this, 'fail'));
64    test.slow = (0, _transform.wrapFunctionWithLocation)(this._modifier.bind(this, 'slow'));
65    test.setTimeout = (0, _transform.wrapFunctionWithLocation)(this._setTimeout.bind(this));
66    test.step = (0, _transform.wrapFunctionWithLocation)(this._step.bind(this));
67    test.use = (0, _transform.wrapFunctionWithLocation)(this._use.bind(this));
68    test.extend = (0, _transform.wrapFunctionWithLocation)(this._extend.bind(this));
69    test.declare = (0, _transform.wrapFunctionWithLocation)(this._declare.bind(this));
70    this.test = test;
71  }
72
73  _createTest(type, location, title, fn) {
74    throwIfRunningInsideJest();
75    const suite = (0, _globals.currentlyLoadingFileSuite)();
76    if (!suite) throw (0, _util.errorWithLocation)(location, `test() can only be called in a test file`);
77    const ordinalInFile = countByFile.get(suite._requireFile) || 0;
78    countByFile.set(suite._requireFile, ordinalInFile + 1);
79    const test = new _test.TestCase('test', title, fn, ordinalInFile, this, location);
80    test._requireFile = suite._requireFile;
81
82    suite._addTest(test);
83
84    if (type === 'only') test._only = true;
85    if (type === 'skip') test.expectedStatus = 'skipped';
86  }
87
88  _describe(type, location, title, fn) {
89    throwIfRunningInsideJest();
90    const suite = (0, _globals.currentlyLoadingFileSuite)();
91    if (!suite) throw (0, _util.errorWithLocation)(location, `describe() can only be called in a test file`);
92
93    if (typeof title === 'function') {
94      throw (0, _util.errorWithLocation)(location, ['It looks like you are calling describe() without the title. Pass the title as a first argument:', `test.describe('my test group', () => {`, `  // Declare tests here`, `});`].join('\n'));
95    }
96
97    const child = new _test.Suite(title);
98    child._requireFile = suite._requireFile;
99    child._isDescribe = true;
100    child.location = location;
101
102    suite._addSuite(child);
103
104    if (type === 'only' || type === 'serial.only') child._only = true;
105    if (type === 'serial' || type === 'serial.only') child._serial = true;
106    (0, _globals.setCurrentlyLoadingFileSuite)(child);
107    fn();
108    (0, _globals.setCurrentlyLoadingFileSuite)(suite);
109  }
110
111  _hook(name, location, fn) {
112    const suite = (0, _globals.currentlyLoadingFileSuite)();
113    if (!suite) throw (0, _util.errorWithLocation)(location, `${name} hook can only be called in a test file`);
114
115    if (name === 'beforeAll' || name === 'afterAll') {
116      const hook = new _test.TestCase(name, name, fn, 0, this, location);
117      hook._requireFile = suite._requireFile;
118
119      suite._addAllHook(hook);
120    } else {
121      suite._eachHooks.push({
122        type: name,
123        fn,
124        location
125      });
126    }
127  }
128
129  _modifier(type, location, ...modifierArgs) {
130    const suite = (0, _globals.currentlyLoadingFileSuite)();
131
132    if (suite) {
133      if (typeof modifierArgs[0] === 'string' && typeof modifierArgs[1] === 'function') {
134        // Support for test.skip('title', () => {})
135        this._createTest('skip', location, modifierArgs[0], modifierArgs[1]);
136
137        return;
138      }
139
140      if (typeof modifierArgs[0] === 'function') {
141        suite._modifiers.push({
142          type,
143          fn: modifierArgs[0],
144          location,
145          description: modifierArgs[1]
146        });
147      } else {
148        if (modifierArgs.length >= 1 && !modifierArgs[0]) return;
149        const description = modifierArgs[1];
150
151        suite._annotations.push({
152          type,
153          description
154        });
155      }
156
157      return;
158    }
159
160    const testInfo = (0, _globals.currentTestInfo)();
161    if (!testInfo) throw (0, _util.errorWithLocation)(location, `test.${type}() can only be called inside test, describe block or fixture`);
162    if (typeof modifierArgs[0] === 'function') throw (0, _util.errorWithLocation)(location, `test.${type}() with a function can only be called inside describe block`);
163    testInfo[type](...modifierArgs);
164  }
165
166  _setTimeout(location, timeout) {
167    const suite = (0, _globals.currentlyLoadingFileSuite)();
168
169    if (suite) {
170      suite._timeout = timeout;
171      return;
172    }
173
174    const testInfo = (0, _globals.currentTestInfo)();
175    if (!testInfo) throw (0, _util.errorWithLocation)(location, `test.setTimeout() can only be called from a test`);
176    testInfo.setTimeout(timeout);
177  }
178
179  _use(location, fixtures) {
180    const suite = (0, _globals.currentlyLoadingFileSuite)();
181    if (!suite) throw (0, _util.errorWithLocation)(location, `test.use() can only be called in a test file`);
182
183    suite._use.push({
184      fixtures,
185      location
186    });
187  }
188
189  async _step(location, title, body) {
190    const testInfo = (0, _globals.currentTestInfo)();
191    if (!testInfo) throw (0, _util.errorWithLocation)(location, `test.step() can only be called from a test`);
192
193    const complete = testInfo._addStep('test.step', title);
194
195    try {
196      await body();
197      complete();
198    } catch (e) {
199      complete((0, _util.serializeError)(e));
200      throw e;
201    }
202  }
203
204  _extend(location, fixtures) {
205    const fixturesWithLocation = {
206      fixtures,
207      location
208    };
209    return new TestTypeImpl([...this.fixtures, fixturesWithLocation]).test;
210  }
211
212  _declare(location) {
213    const declared = new DeclaredFixtures();
214    declared.location = location;
215    const child = new TestTypeImpl([...this.fixtures, declared]);
216    declared.testType = child;
217    return child.test;
218  }
219
220}
221
222exports.TestTypeImpl = TestTypeImpl;
223
224function throwIfRunningInsideJest() {
225  if (process.env.JEST_WORKER_ID) {
226    throw new Error(`Playwright Test needs to be invoked via 'npx playwright test' and excluded from Jest test runs.\n` + `Creating one directory for Playwright tests and one for Jest is the recommended way of doing it.\n` + `See https://playwright.dev/docs/intro/ for more information about Playwright Test.`);
227  }
228}
229
230const rootTestType = new TestTypeImpl([]);
231exports.rootTestType = rootTestType;
Full Screen

printer.js

Source: printer.js Github

copy
1import chalk from "chalk";
2import { getFileContent } from "./parser";
3import { codeFrameColumns } from "@babel/code-frame";
4import type { HegelError } from "@hegel/core";
5import type { ExtendedSyntaxError } from "@babel/parser";
6
7export type ErrorWithLocation = HegelError | ExtendedSyntaxError;
8
9export function printSingleError(
10  error: ErrorWithLocation,
11  fileContent: string
12) {
13  const loc =
14    "start" in error.loc
15      ? error.loc
16      : {
17          start: error.loc,
18          end: Object.assign(error.loc, { column: error.loc.column + 1 }),
19        };
20  const line = chalk.dim.underline(`${error.source}:${String(loc.start.line)}`);
21  const codeFrame = codeFrameColumns(fileContent, loc, {
22    highlightCode: true,
23    message: error.message.replace(/\([\d:]+\)/gi, ""),
24  });
25  return `${line}\n${codeFrame}`;
26}
27
28export async function getErrorsPrint(errors: Array<ErrorWithLocation>) {
29  let result = "";
30  for (const error of errors) {
31    if (error === undefined || error.source === "") {
32      continue;
33    }
34    const content = await getFileContent(error.source);
35    result = `${result}${printSingleError(error, content)}\n\n`;
36  }
37  return result;
38}
39
40export function getVerdictPrint(errors) {
41  return errors.length > 0
42    ? `Found ${String(errors.length)} error${errors.length > 1 ? "s" : ""}`
43    : "No errors!";
44}
45
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)