Best JavaScript code snippet using playwright-internal
recorderSupplement.js
Source:recorderSupplement.js
1"use strict";2Object.defineProperty(exports, "__esModule", {3 value: true4});5exports.RecorderSupplement = void 0;6var fs = _interopRequireWildcard(require("fs"));7var _codeGenerator = require("./recorder/codeGenerator");8var _utils = require("./recorder/utils");9var _page = require("../page");10var _frames = require("../frames");11var _browserContext = require("../browserContext");12var _java = require("./recorder/java");13var _javascript = require("./recorder/javascript");14var _csharp = require("./recorder/csharp");15var _python = require("./recorder/python");16var recorderSource = _interopRequireWildcard(require("../../generated/recorderSource"));17var consoleApiSource = _interopRequireWildcard(require("../../generated/consoleApiSource"));18var _recorderApp = require("./recorder/recorderApp");19var _utils2 = require("../../utils/utils");20var _recorderUtils = require("./recorder/recorderUtils");21var _debugger = require("./debugger");22var _events = require("events");23function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }24function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }25/**26 * Copyright (c) Microsoft Corporation.27 *28 * Licensed under the Apache License, Version 2.0 (the "License");29 * you may not use this file except in compliance with the License.30 * You may obtain a copy of the License at31 *32 * http://www.apache.org/licenses/LICENSE-2.033 *34 * Unless required by applicable law or agreed to in writing, software35 * distributed under the License is distributed on an "AS IS" BASIS,36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.37 * See the License for the specific language governing permissions and38 * limitations under the License.39 */40const symbol = Symbol('RecorderSupplement');41class RecorderSupplement {42 static showInspector(context) {43 RecorderSupplement.show(context, {}).catch(() => {});44 }45 static show(context, params = {}) {46 let recorderPromise = context[symbol];47 if (!recorderPromise) {48 const recorder = new RecorderSupplement(context, params);49 recorderPromise = recorder.install().then(() => recorder);50 context[symbol] = recorderPromise;51 }52 return recorderPromise;53 }54 constructor(context, params) {55 this._context = void 0;56 this._mode = void 0;57 this._highlightedSelector = '';58 this._recorderApp = null;59 this._currentCallsMetadata = new Map();60 this._recorderSources = [];61 this._userSources = new Map();62 this._allMetadatas = new Map();63 this._debugger = void 0;64 this._contextRecorder = void 0;65 this._mode = params.startRecording ? 'recording' : 'none';66 this._contextRecorder = new ContextRecorder(context, params);67 this._context = context;68 this._debugger = _debugger.Debugger.lookup(context);69 context.instrumentation.addListener(this, context);70 }71 async install() {72 const recorderApp = await _recorderApp.RecorderApp.open(this._context._browser.options.sdkLanguage, !!this._context._browser.options.headful);73 this._recorderApp = recorderApp;74 recorderApp.once('close', () => {75 this._debugger.resume(false);76 this._recorderApp = null;77 });78 recorderApp.on('event', data => {79 if (data.event === 'setMode') {80 this._setMode(data.params.mode);81 this._refreshOverlay();82 return;83 }84 if (data.event === 'selectorUpdated') {85 this._highlightedSelector = data.params.selector;86 this._refreshOverlay();87 return;88 }89 if (data.event === 'step') {90 this._debugger.resume(true);91 return;92 }93 if (data.event === 'resume') {94 this._debugger.resume(false);95 return;96 }97 if (data.event === 'pause') {98 this._debugger.pauseOnNextStatement();99 return;100 }101 if (data.event === 'clear') {102 this._contextRecorder.clearScript();103 return;104 }105 });106 await Promise.all([recorderApp.setMode(this._mode), recorderApp.setPaused(this._debugger.isPaused()), this._pushAllSources()]);107 this._context.once(_browserContext.BrowserContext.Events.Close, () => {108 this._contextRecorder.dispose();109 recorderApp.close().catch(() => {});110 });111 this._contextRecorder.on(ContextRecorder.Events.Change, data => {112 var _this$_recorderApp;113 this._recorderSources = data.sources;114 this._pushAllSources();115 (_this$_recorderApp = this._recorderApp) === null || _this$_recorderApp === void 0 ? void 0 : _this$_recorderApp.setFile(data.primaryFileName);116 });117 await this._context.exposeBinding('_playwrightRecorderState', false, source => {118 let actionSelector = this._highlightedSelector;119 let actionPoint;120 for (const [metadata, sdkObject] of this._currentCallsMetadata) {121 if (source.page === sdkObject.attribution.page) {122 actionPoint = metadata.point || actionPoint;123 actionSelector = actionSelector || metadata.params.selector;124 }125 }126 const uiState = {127 mode: this._mode,128 actionPoint,129 actionSelector130 };131 return uiState;132 });133 await this._context.exposeBinding('_playwrightRecorderSetSelector', false, async (_, selector) => {134 var _this$_recorderApp2, _this$_recorderApp3;135 this._setMode('none');136 await ((_this$_recorderApp2 = this._recorderApp) === null || _this$_recorderApp2 === void 0 ? void 0 : _this$_recorderApp2.setSelector(selector, true));137 await ((_this$_recorderApp3 = this._recorderApp) === null || _this$_recorderApp3 === void 0 ? void 0 : _this$_recorderApp3.bringToFront());138 });139 await this._context.exposeBinding('_playwrightResume', false, () => {140 this._debugger.resume(false);141 });142 await this._context.extendInjectedScript(consoleApiSource.source);143 await this._contextRecorder.install();144 if (this._debugger.isPaused()) this._pausedStateChanged();145 this._debugger.on(_debugger.Debugger.Events.PausedStateChanged, () => this._pausedStateChanged());146 this._context.recorderAppForTest = recorderApp;147 }148 _pausedStateChanged() {149 var _this$_recorderApp4;150 // If we are called upon page.pause, we don't have metadatas, populate them.151 for (const {152 metadata,153 sdkObject154 } of this._debugger.pausedDetails()) {155 if (!this._currentCallsMetadata.has(metadata)) this.onBeforeCall(sdkObject, metadata);156 }157 (_this$_recorderApp4 = this._recorderApp) === null || _this$_recorderApp4 === void 0 ? void 0 : _this$_recorderApp4.setPaused(this._debugger.isPaused());158 this._updateUserSources();159 this.updateCallLog([...this._currentCallsMetadata.keys()]);160 }161 _setMode(mode) {162 var _this$_recorderApp5;163 this._mode = mode;164 (_this$_recorderApp5 = this._recorderApp) === null || _this$_recorderApp5 === void 0 ? void 0 : _this$_recorderApp5.setMode(this._mode);165 this._contextRecorder.setEnabled(this._mode === 'recording');166 this._debugger.setMuted(this._mode === 'recording');167 if (this._mode !== 'none') this._context.pages()[0].bringToFront().catch(() => {});168 }169 _refreshOverlay() {170 for (const page of this._context.pages()) page.mainFrame().evaluateExpression('window._playwrightRefreshOverlay()', false, undefined, 'main').catch(() => {});171 }172 async onBeforeCall(sdkObject, metadata) {173 if (this._mode === 'recording') return;174 this._currentCallsMetadata.set(metadata, sdkObject);175 this._allMetadatas.set(metadata.id, metadata);176 this._updateUserSources();177 this.updateCallLog([metadata]);178 if (metadata.params && metadata.params.selector) {179 var _this$_recorderApp6;180 this._highlightedSelector = metadata.params.selector;181 (_this$_recorderApp6 = this._recorderApp) === null || _this$_recorderApp6 === void 0 ? void 0 : _this$_recorderApp6.setSelector(this._highlightedSelector).catch(() => {});182 }183 }184 async onAfterCall(sdkObject, metadata) {185 if (this._mode === 'recording') return;186 if (!metadata.error) this._currentCallsMetadata.delete(metadata);187 this._updateUserSources();188 this.updateCallLog([metadata]);189 }190 _updateUserSources() {191 var _this$_recorderApp7;192 // Remove old decorations.193 for (const source of this._userSources.values()) {194 source.highlight = [];195 source.revealLine = undefined;196 } // Apply new decorations.197 let fileToSelect = undefined;198 for (const metadata of this._currentCallsMetadata.keys()) {199 if (!metadata.stack || !metadata.stack[0]) continue;200 const {201 file,202 line203 } = metadata.stack[0];204 let source = this._userSources.get(file);205 if (!source) {206 source = {207 file,208 text: this._readSource(file),209 highlight: [],210 language: languageForFile(file)211 };212 this._userSources.set(file, source);213 }214 if (line) {215 const paused = this._debugger.isPaused(metadata);216 source.highlight.push({217 line,218 type: metadata.error ? 'error' : paused ? 'paused' : 'running'219 });220 source.revealLine = line;221 fileToSelect = source.file;222 }223 }224 this._pushAllSources();225 if (fileToSelect) (_this$_recorderApp7 = this._recorderApp) === null || _this$_recorderApp7 === void 0 ? void 0 : _this$_recorderApp7.setFile(fileToSelect);226 }227 _pushAllSources() {228 var _this$_recorderApp8;229 (_this$_recorderApp8 = this._recorderApp) === null || _this$_recorderApp8 === void 0 ? void 0 : _this$_recorderApp8.setSources([...this._recorderSources, ...this._userSources.values()]);230 }231 async onBeforeInputAction(sdkObject, metadata) {}232 async onCallLog(sdkObject, metadata, logName, message) {233 this.updateCallLog([metadata]);234 }235 updateCallLog(metadatas) {236 var _this$_recorderApp9;237 if (this._mode === 'recording') return;238 const logs = [];239 for (const metadata of metadatas) {240 if (!metadata.method || metadata.internal) continue;241 let status = 'done';242 if (this._currentCallsMetadata.has(metadata)) status = 'in-progress';243 if (this._debugger.isPaused(metadata)) status = 'paused';244 logs.push((0, _recorderUtils.metadataToCallLog)(metadata, status));245 }246 (_this$_recorderApp9 = this._recorderApp) === null || _this$_recorderApp9 === void 0 ? void 0 : _this$_recorderApp9.updateCallLogs(logs);247 }248 _readSource(fileName) {249 try {250 return fs.readFileSync(fileName, 'utf-8');251 } catch (e) {252 return '// No source available';253 }254 }255}256exports.RecorderSupplement = RecorderSupplement;257class ContextRecorder extends _events.EventEmitter {258 constructor(context, params) {259 super();260 this._generator = void 0;261 this._pageAliases = new Map();262 this._lastPopupOrdinal = 0;263 this._lastDialogOrdinal = 0;264 this._lastDownloadOrdinal = 0;265 this._timers = new Set();266 this._context = void 0;267 this._params = void 0;268 this._recorderSources = void 0;269 this._context = context;270 this._params = params;271 const language = params.language || context._browser.options.sdkLanguage;272 const languages = new Set([new _java.JavaLanguageGenerator(), new _javascript.JavaScriptLanguageGenerator(false), new _javascript.JavaScriptLanguageGenerator(true), new _python.PythonLanguageGenerator(false), new _python.PythonLanguageGenerator(true), new _csharp.CSharpLanguageGenerator()]);273 const primaryLanguage = [...languages].find(l => l.id === language);274 if (!primaryLanguage) throw new Error(`\n===============================\nUnsupported language: '${language}'\n===============================\n`);275 languages.delete(primaryLanguage);276 const orderedLanguages = [primaryLanguage, ...languages];277 this._recorderSources = [];278 const generator = new _codeGenerator.CodeGenerator(context._browser.options.name, !!params.startRecording, params.launchOptions || {}, params.contextOptions || {}, params.device, params.saveStorage);279 let text = '';280 generator.on('change', () => {281 this._recorderSources = [];282 for (const languageGenerator of orderedLanguages) {283 const source = {284 file: languageGenerator.fileName,285 text: generator.generateText(languageGenerator),286 language: languageGenerator.highlighter,287 highlight: []288 };289 source.revealLine = source.text.split('\n').length - 1;290 this._recorderSources.push(source);291 if (languageGenerator === orderedLanguages[0]) text = source.text;292 }293 this.emit(ContextRecorder.Events.Change, {294 sources: this._recorderSources,295 primaryFileName: primaryLanguage.fileName296 });297 });298 if (params.outputFile) {299 context.on(_browserContext.BrowserContext.Events.BeforeClose, () => {300 fs.writeFileSync(params.outputFile, text);301 text = '';302 });303 process.on('exit', () => {304 if (text) fs.writeFileSync(params.outputFile, text);305 });306 }307 this._generator = generator;308 }309 async install() {310 this._context.on(_browserContext.BrowserContext.Events.Page, page => this._onPage(page));311 for (const page of this._context.pages()) this._onPage(page); // Input actions that potentially lead to navigation are intercepted on the page and are312 // performed by the Playwright.313 await this._context.exposeBinding('_playwrightRecorderPerformAction', false, (source, action) => this._performAction(source.frame, action)); // Other non-essential actions are simply being recorded.314 await this._context.exposeBinding('_playwrightRecorderRecordAction', false, (source, action) => this._recordAction(source.frame, action));315 await this._context.extendInjectedScript(recorderSource.source, {316 isUnderTest: (0, _utils2.isUnderTest)()317 });318 }319 setEnabled(enabled) {320 this._generator.setEnabled(enabled);321 }322 dispose() {323 for (const timer of this._timers) clearTimeout(timer);324 this._timers.clear();325 }326 async _onPage(page) {327 // First page is called page, others are called popup1, popup2, etc.328 const frame = page.mainFrame();329 page.on('close', () => {330 this._pageAliases.delete(page);331 this._generator.addAction({332 pageAlias,333 ...(0, _utils.describeFrame)(page.mainFrame()),334 committed: true,335 action: {336 name: 'closePage',337 signals: []338 }339 });340 });341 frame.on(_frames.Frame.Events.Navigation, () => this._onFrameNavigated(frame, page));342 page.on(_page.Page.Events.Download, () => this._onDownload(page));343 page.on(_page.Page.Events.Dialog, () => this._onDialog(page));344 const suffix = this._pageAliases.size ? String(++this._lastPopupOrdinal) : '';345 const pageAlias = 'page' + suffix;346 this._pageAliases.set(page, pageAlias);347 if (page.opener()) {348 this._onPopup(page.opener(), page);349 } else {350 this._generator.addAction({351 pageAlias,352 ...(0, _utils.describeFrame)(page.mainFrame()),353 committed: true,354 action: {355 name: 'openPage',356 url: page.mainFrame().url(),357 signals: []358 }359 });360 }361 }362 clearScript() {363 this._generator.restart();364 if (!!this._params.startRecording) {365 for (const page of this._context.pages()) this._onFrameNavigated(page.mainFrame(), page);366 }367 }368 async _performAction(frame, action) {369 // Commit last action so that no further signals are added to it.370 this._generator.commitLastAction();371 const page = frame._page;372 const actionInContext = {373 pageAlias: this._pageAliases.get(page),374 ...(0, _utils.describeFrame)(frame),375 action376 };377 const perform = async (action, params, cb) => {378 const callMetadata = {379 id: `call@${(0, _utils2.createGuid)()}`,380 apiName: 'frame.' + action,381 objectId: frame.guid,382 pageId: frame._page.guid,383 frameId: frame.guid,384 wallTime: Date.now(),385 startTime: (0, _utils2.monotonicTime)(),386 endTime: 0,387 type: 'Frame',388 method: action,389 params,390 log: [],391 snapshots: []392 };393 this._generator.willPerformAction(actionInContext);394 try {395 await frame.instrumentation.onBeforeCall(frame, callMetadata);396 await cb(callMetadata);397 } catch (e) {398 callMetadata.endTime = (0, _utils2.monotonicTime)();399 await frame.instrumentation.onAfterCall(frame, callMetadata);400 this._generator.performedActionFailed(actionInContext);401 return;402 }403 callMetadata.endTime = (0, _utils2.monotonicTime)();404 await frame.instrumentation.onAfterCall(frame, callMetadata);405 const timer = setTimeout(() => {406 // Commit the action after 5 seconds so that no further signals are added to it.407 actionInContext.committed = true;408 this._timers.delete(timer);409 }, 5000);410 this._generator.didPerformAction(actionInContext);411 this._timers.add(timer);412 };413 const kActionTimeout = 5000;414 if (action.name === 'click') {415 const {416 options417 } = (0, _utils.toClickOptions)(action);418 await perform('click', {419 selector: action.selector420 }, callMetadata => frame.click(callMetadata, action.selector, { ...options,421 timeout: kActionTimeout422 }));423 }424 if (action.name === 'press') {425 const modifiers = (0, _utils.toModifiers)(action.modifiers);426 const shortcut = [...modifiers, action.key].join('+');427 await perform('press', {428 selector: action.selector,429 key: shortcut430 }, callMetadata => frame.press(callMetadata, action.selector, shortcut, {431 timeout: kActionTimeout432 }));433 }434 if (action.name === 'check') await perform('check', {435 selector: action.selector436 }, callMetadata => frame.check(callMetadata, action.selector, {437 timeout: kActionTimeout438 }));439 if (action.name === 'uncheck') await perform('uncheck', {440 selector: action.selector441 }, callMetadata => frame.uncheck(callMetadata, action.selector, {442 timeout: kActionTimeout443 }));444 if (action.name === 'select') {445 const values = action.options.map(value => ({446 value447 }));448 await perform('selectOption', {449 selector: action.selector,450 values451 }, callMetadata => frame.selectOption(callMetadata, action.selector, [], values, {452 timeout: kActionTimeout453 }));454 }455 }456 async _recordAction(frame, action) {457 // Commit last action so that no further signals are added to it.458 this._generator.commitLastAction();459 this._generator.addAction({460 pageAlias: this._pageAliases.get(frame._page),461 ...(0, _utils.describeFrame)(frame),462 action463 });464 }465 _onFrameNavigated(frame, page) {466 const pageAlias = this._pageAliases.get(page);467 this._generator.signal(pageAlias, frame, {468 name: 'navigation',469 url: frame.url()470 });471 }472 _onPopup(page, popup) {473 const pageAlias = this._pageAliases.get(page);474 const popupAlias = this._pageAliases.get(popup);475 this._generator.signal(pageAlias, page.mainFrame(), {476 name: 'popup',477 popupAlias478 });479 }480 _onDownload(page) {481 const pageAlias = this._pageAliases.get(page);482 this._generator.signal(pageAlias, page.mainFrame(), {483 name: 'download',484 downloadAlias: String(++this._lastDownloadOrdinal)485 });486 }487 _onDialog(page) {488 const pageAlias = this._pageAliases.get(page);489 this._generator.signal(pageAlias, page.mainFrame(), {490 name: 'dialog',491 dialogAlias: String(++this._lastDialogOrdinal)492 });493 }494}495ContextRecorder.Events = {496 Change: 'change'497};498function languageForFile(file) {499 if (file.endsWith('.py')) return 'python';500 if (file.endsWith('.java')) return 'java';501 if (file.endsWith('.cs')) return 'csharp';502 return 'javascript';...
recorderUtils.js
Source:recorderUtils.js
...17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.18 * See the License for the specific language governing permissions and19 * limitations under the License.20 */21function metadataToCallLog(metadata, status) {22 var _metadata$params, _metadata$params2;23 let title = metadata.apiName || metadata.method;24 if (metadata.method === 'waitForEventInfo') title += `(${metadata.params.info.event})`;25 title = title.replace('object.expect', 'expect');26 if (metadata.error) status = 'error';27 const params = {28 url: (_metadata$params = metadata.params) === null || _metadata$params === void 0 ? void 0 : _metadata$params.url,29 selector: (_metadata$params2 = metadata.params) === null || _metadata$params2 === void 0 ? void 0 : _metadata$params2.selector30 };31 let duration = metadata.endTime ? metadata.endTime - metadata.startTime : undefined;32 if (typeof duration === 'number' && metadata.pauseStartTime && metadata.pauseEndTime) {33 duration -= metadata.pauseEndTime - metadata.pauseStartTime;34 duration = Math.max(duration, 0);35 }...
Using AI Code Generation
1const { metadataToCallLog } = require('@playwright/test/lib/utils/stackTrace');2const metadata = { title: 'test', file: 'test.js', line: 1, column: 1 };3console.log(metadataToCallLog(metadata));4const { metadataToCallLog } = require('@playwright/test/lib/utils/stackTrace');5const metadata = { title: 'test', file: 'test.js', line: 1, column: 1 };6console.log(metadataToCallLog(metadata, 'MyTest'));7const { PlaywrightTestReporter } = require('@logzio/playwright-test-reporter');8module.exports = {9 use: {10 reporter: new PlaywrightTestReporter(),11 },12};13const { test, expect } = require('@playwright/test');14const { PlaywrightTestReporter } = require('@logzio/playwright-test-reporter');15test.use(new PlaywrightTestReporter());16test('MyTest', async ({ page }) => {17 const title = page.locator('text=Playwright');18 await expect(title).toBeVisible();19});20 ✓ passed (1.1s)
Using AI Code Generation
1const { metadataToCallLog } = require('playwright/lib/server/trace/recorder');2const metadata = {3 params: {4 }5};6const callLog = metadataToCallLog(metadata);7console.log(callLog);8{9 params: { foo: 'bar' },10}
Using AI Code Generation
1const { metadataToCallLog } = require('@playwright/test/lib/trace/recorder/api');2const metadata = {3 headers: {4 },5};6const callLog = metadataToCallLog(metadata);7console.log(callLog);
Using AI Code Generation
1const { metadataToCallLog } = require('playwright/lib/utils/stackTrace');2const metadata = {3 test: {4 },5 project: {6 },7 worker: {8 workerInfo: { title: 'MyProject' }9 },10 config: {11 }12};13const callLog = metadataToCallLog(metadata);14console.log(callLog);
Using AI Code Generation
1const { metadataToCallLog } = require('@playwright/test/lib/server/trace/recorder/recorderApp');2const metadata = {3};4const callLog = metadataToCallLog(metadata);5console.log(callLog);6{7}8import { PlaywrightTestConfig } from '@playwright/test';9const config: PlaywrightTestConfig = {10};11export default config;12import { test, expect } from '@playwright/test';13test('My Test', async ({ page }) => {14 const title = page.locator('text=Playwright');15 await expect(title).toBeVisible();16});17const { test } = require('@playwright/test');18const config = require('./
Using AI Code Generation
1const { metadataToCallLog } = require('playwright/lib/server/chromium/crNetworkManager');2const metadata = {3 'requestHeaders': {4 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'5 },6 'responseHeaders': {7 'content-type': 'text/html; charset=utf-8'8 },9};10const callLog = metadataToCallLog(metadata);11console.log(callLog);
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!