Best JavaScript code snippet using playwright-internal
DOMLegacyEventPluginSystem.js
Source:DOMLegacyEventPluginSystem.js
...287 *288 * @param {string} registrationName Name of listener (e.g. `onClick`).289 * @param {object} mountAt Container where to mount the listener290 */291export function legacyListenToEvent(292 registrationName: string,293 mountAt: Document | Element | Node,294): void {295 const listenerMap = getListenerMapForElement(mountAt);296 const dependencies = registrationNameDependencies[registrationName];297 for (let i = 0; i < dependencies.length; i++) {298 const dependency = dependencies[i];299 legacyListenToTopLevelEvent(dependency, mountAt, listenerMap);300 }301}302export function legacyListenToTopLevelEvent(303 topLevelType: DOMTopLevelEventType,304 mountAt: Document | Element | Node,305 listenerMap: Map<DOMTopLevelEventType | string, null | (any => void)>,...
ReactBrowserEventEmitter-test.internal.js
Source:ReactBrowserEventEmitter-test.internal.js
1/**2 * Copyright (c) Facebook, Inc. and its affiliates.3 *4 * This source code is licensed under the MIT license found in the5 * LICENSE file in the root directory of this source tree.6 *7 * @emails react-core8 */9'use strict';10let EventPluginGetListener;11let EventPluginRegistry;12let React;13let ReactDOM;14let ReactDOMComponentTree;15let listenToEvent;16let ReactDOMEventListener;17let ReactTestUtils;18let ReactFeatureFlags;19let idCallOrder;20const recordID = function(id) {21 idCallOrder.push(id);22};23const recordIDAndStopPropagation = function(id, event) {24 recordID(id);25 event.stopPropagation();26};27const recordIDAndReturnFalse = function(id, event) {28 recordID(id);29 return false;30};31const LISTENER = jest.fn();32const ON_CLICK_KEY = 'onClick';33const ON_CHANGE_KEY = 'onChange';34const ON_MOUSE_ENTER_KEY = 'onMouseEnter';35let GRANDPARENT;36let PARENT;37let CHILD;38let BUTTON;39let getListener;40let putListener;41let deleteAllListeners;42let container;43function registerSimpleTestHandler() {44 putListener(CHILD, ON_CLICK_KEY, LISTENER);45 const listener = getListener(CHILD, ON_CLICK_KEY);46 expect(listener).toEqual(LISTENER);47 return getListener(CHILD, ON_CLICK_KEY);48}49// We should probably remove this file at some point, it's just full of50// internal API usage.51describe('ReactBrowserEventEmitter', () => {52 beforeEach(() => {53 jest.resetModules();54 LISTENER.mockClear();55 ReactFeatureFlags = require('shared/ReactFeatureFlags');56 EventPluginGetListener = require('react-dom/src/events/getListener')57 .default;58 EventPluginRegistry = require('legacy-events/EventPluginRegistry');59 React = require('react');60 ReactDOM = require('react-dom');61 ReactDOMComponentTree = require('../client/ReactDOMComponentTree');62 if (ReactFeatureFlags.enableModernEventSystem) {63 listenToEvent = require('../events/DOMModernPluginEventSystem')64 .listenToEvent;65 } else {66 listenToEvent = require('../events/DOMLegacyEventPluginSystem')67 .legacyListenToEvent;68 }69 ReactDOMEventListener = require('../events/ReactDOMEventListener');70 ReactTestUtils = require('react-dom/test-utils');71 container = document.createElement('div');72 document.body.appendChild(container);73 let GRANDPARENT_PROPS = {};74 let PARENT_PROPS = {};75 let CHILD_PROPS = {};76 let BUTTON_PROPS = {};77 function Child(props) {78 return <div ref={c => (CHILD = c)} {...props} />;79 }80 class ChildWrapper extends React.PureComponent {81 render() {82 return <Child {...this.props} />;83 }84 }85 function renderTree() {86 ReactDOM.render(87 <div ref={c => (GRANDPARENT = c)} {...GRANDPARENT_PROPS}>88 <div ref={c => (PARENT = c)} {...PARENT_PROPS}>89 <ChildWrapper {...CHILD_PROPS} />90 <button disabled={true} ref={c => (BUTTON = c)} {...BUTTON_PROPS} />91 </div>92 </div>,93 container,94 );95 }96 renderTree();97 getListener = function(node, eventName) {98 const inst = ReactDOMComponentTree.getInstanceFromNode(node);99 return EventPluginGetListener(inst, eventName);100 };101 putListener = function(node, eventName, listener) {102 switch (node) {103 case CHILD:104 CHILD_PROPS[eventName] = listener;105 break;106 case PARENT:107 PARENT_PROPS[eventName] = listener;108 break;109 case GRANDPARENT:110 GRANDPARENT_PROPS[eventName] = listener;111 break;112 case BUTTON:113 BUTTON_PROPS[eventName] = listener;114 break;115 }116 // Rerender with new event listeners117 renderTree();118 };119 deleteAllListeners = function(node) {120 switch (node) {121 case CHILD:122 CHILD_PROPS = {};123 break;124 case PARENT:125 PARENT_PROPS = {};126 break;127 case GRANDPARENT:128 GRANDPARENT_PROPS = {};129 break;130 case BUTTON:131 BUTTON_PROPS = {};132 break;133 }134 renderTree();135 };136 idCallOrder = [];137 });138 afterEach(() => {139 document.body.removeChild(container);140 container = null;141 });142 it('should store a listener correctly', () => {143 registerSimpleTestHandler();144 const listener = getListener(CHILD, ON_CLICK_KEY);145 expect(listener).toBe(LISTENER);146 });147 it('should retrieve a listener correctly', () => {148 registerSimpleTestHandler();149 const listener = getListener(CHILD, ON_CLICK_KEY);150 expect(listener).toEqual(LISTENER);151 });152 it('should not retrieve listeners on a disabled interactive element', () => {153 putListener(BUTTON, ON_MOUSE_ENTER_KEY, recordID.bind(null, BUTTON));154 const listener = getListener(BUTTON, ON_MOUSE_ENTER_KEY);155 expect(listener).toBe(null);156 });157 it('should clear all handlers when asked to', () => {158 registerSimpleTestHandler();159 deleteAllListeners(CHILD);160 const listener = getListener(CHILD, ON_CLICK_KEY);161 expect(listener).toBe(undefined);162 });163 it('should invoke a simple handler registered on a node', () => {164 registerSimpleTestHandler();165 CHILD.click();166 expect(LISTENER).toHaveBeenCalledTimes(1);167 });168 it('should not invoke handlers if ReactDOMEventListener is disabled', () => {169 registerSimpleTestHandler();170 ReactDOMEventListener.setEnabled(false);171 CHILD.click();172 expect(LISTENER).toHaveBeenCalledTimes(0);173 ReactDOMEventListener.setEnabled(true);174 CHILD.click();175 expect(LISTENER).toHaveBeenCalledTimes(1);176 });177 it('should bubble simply', () => {178 putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));179 putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, PARENT));180 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));181 CHILD.click();182 expect(idCallOrder.length).toBe(3);183 expect(idCallOrder[0]).toBe(CHILD);184 expect(idCallOrder[1]).toBe(PARENT);185 expect(idCallOrder[2]).toBe(GRANDPARENT);186 });187 it('should bubble to the right handler after an update', () => {188 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, 'GRANDPARENT'));189 putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, 'PARENT'));190 putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, 'CHILD'));191 CHILD.click();192 expect(idCallOrder).toEqual(['CHILD', 'PARENT', 'GRANDPARENT']);193 idCallOrder = [];194 // Update just the grand parent without updating the child.195 putListener(196 GRANDPARENT,197 ON_CLICK_KEY,198 recordID.bind(null, 'UPDATED_GRANDPARENT'),199 );200 CHILD.click();201 expect(idCallOrder).toEqual(['CHILD', 'PARENT', 'UPDATED_GRANDPARENT']);202 });203 it('should continue bubbling if an error is thrown', () => {204 putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));205 putListener(PARENT, ON_CLICK_KEY, function() {206 recordID(PARENT);207 throw new Error('Handler interrupted');208 });209 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));210 expect(function() {211 ReactTestUtils.Simulate.click(CHILD);212 }).toThrow();213 expect(idCallOrder.length).toBe(3);214 expect(idCallOrder[0]).toBe(CHILD);215 expect(idCallOrder[1]).toBe(PARENT);216 expect(idCallOrder[2]).toBe(GRANDPARENT);217 });218 it('should set currentTarget', () => {219 putListener(CHILD, ON_CLICK_KEY, function(event) {220 recordID(CHILD);221 expect(event.currentTarget).toBe(CHILD);222 });223 putListener(PARENT, ON_CLICK_KEY, function(event) {224 recordID(PARENT);225 expect(event.currentTarget).toBe(PARENT);226 });227 putListener(GRANDPARENT, ON_CLICK_KEY, function(event) {228 recordID(GRANDPARENT);229 expect(event.currentTarget).toBe(GRANDPARENT);230 });231 CHILD.click();232 expect(idCallOrder.length).toBe(3);233 expect(idCallOrder[0]).toBe(CHILD);234 expect(idCallOrder[1]).toBe(PARENT);235 expect(idCallOrder[2]).toBe(GRANDPARENT);236 });237 it('should support stopPropagation()', () => {238 putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));239 putListener(240 PARENT,241 ON_CLICK_KEY,242 recordIDAndStopPropagation.bind(null, PARENT),243 );244 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));245 CHILD.click();246 expect(idCallOrder.length).toBe(2);247 expect(idCallOrder[0]).toBe(CHILD);248 expect(idCallOrder[1]).toBe(PARENT);249 });250 it('should support overriding .isPropagationStopped()', () => {251 // Ew. See D4504876.252 putListener(CHILD, ON_CLICK_KEY, recordID.bind(null, CHILD));253 putListener(PARENT, ON_CLICK_KEY, function(e) {254 recordID(PARENT, e);255 // This stops React bubbling but avoids touching the native event256 e.isPropagationStopped = () => true;257 });258 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));259 CHILD.click();260 expect(idCallOrder.length).toBe(2);261 expect(idCallOrder[0]).toBe(CHILD);262 expect(idCallOrder[1]).toBe(PARENT);263 });264 it('should stop after first dispatch if stopPropagation', () => {265 putListener(266 CHILD,267 ON_CLICK_KEY,268 recordIDAndStopPropagation.bind(null, CHILD),269 );270 putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, PARENT));271 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));272 CHILD.click();273 expect(idCallOrder.length).toBe(1);274 expect(idCallOrder[0]).toBe(CHILD);275 });276 it('should not stopPropagation if false is returned', () => {277 putListener(CHILD, ON_CLICK_KEY, recordIDAndReturnFalse.bind(null, CHILD));278 putListener(PARENT, ON_CLICK_KEY, recordID.bind(null, PARENT));279 putListener(GRANDPARENT, ON_CLICK_KEY, recordID.bind(null, GRANDPARENT));280 CHILD.click();281 expect(idCallOrder.length).toBe(3);282 expect(idCallOrder[0]).toBe(CHILD);283 expect(idCallOrder[1]).toBe(PARENT);284 expect(idCallOrder[2]).toBe(GRANDPARENT);285 });286 /**287 * The entire event registration state of the world should be "locked-in" at288 * the time the event occurs. This is to resolve many edge cases that come289 * about from a listener on a lower-in-DOM node causing structural changes at290 * places higher in the DOM. If this lower-in-DOM node causes new content to291 * be rendered at a place higher-in-DOM, we need to be careful not to invoke292 * these new listeners.293 */294 it('should invoke handlers that were removed while bubbling', () => {295 const handleParentClick = jest.fn();296 const handleChildClick = function(event) {297 deleteAllListeners(PARENT);298 };299 putListener(CHILD, ON_CLICK_KEY, handleChildClick);300 putListener(PARENT, ON_CLICK_KEY, handleParentClick);301 CHILD.click();302 expect(handleParentClick).toHaveBeenCalledTimes(1);303 });304 it('should not invoke newly inserted handlers while bubbling', () => {305 const handleParentClick = jest.fn();306 const handleChildClick = function(event) {307 putListener(PARENT, ON_CLICK_KEY, handleParentClick);308 };309 putListener(CHILD, ON_CLICK_KEY, handleChildClick);310 CHILD.click();311 expect(handleParentClick).toHaveBeenCalledTimes(0);312 });313 it('should have mouse enter simulated by test utils', () => {314 putListener(CHILD, ON_MOUSE_ENTER_KEY, recordID.bind(null, CHILD));315 ReactTestUtils.Simulate.mouseEnter(CHILD);316 expect(idCallOrder.length).toBe(1);317 expect(idCallOrder[0]).toBe(CHILD);318 });319 it('should listen to events only once', () => {320 spyOnDevAndProd(EventTarget.prototype, 'addEventListener');321 listenToEvent(ON_CLICK_KEY, document);322 listenToEvent(ON_CLICK_KEY, document);323 expect(EventTarget.prototype.addEventListener).toHaveBeenCalledTimes(1);324 });325 it('should work with event plugins without dependencies', () => {326 spyOnDevAndProd(EventTarget.prototype, 'addEventListener');327 listenToEvent(ON_CLICK_KEY, document);328 expect(EventTarget.prototype.addEventListener.calls.argsFor(0)[0]).toBe(329 'click',330 );331 });332 it('should work with event plugins with dependencies', () => {333 spyOnDevAndProd(EventTarget.prototype, 'addEventListener');334 listenToEvent(ON_CHANGE_KEY, document);335 const setEventListeners = [];336 const listenCalls = EventTarget.prototype.addEventListener.calls.allArgs();337 for (let i = 0; i < listenCalls.length; i++) {338 setEventListeners.push(listenCalls[i][1]);339 }340 const module = EventPluginRegistry.registrationNameModules[ON_CHANGE_KEY];341 const dependencies = module.eventTypes.change.dependencies;342 expect(setEventListeners.length).toEqual(dependencies.length);343 for (let i = 0; i < setEventListeners.length; i++) {344 expect(dependencies.indexOf(setEventListeners[i])).toBeTruthy();345 }346 });...
Using AI Code Generation
1const playwright = require('playwright');2(async () => {3 const browser = await playwright['chromium'].launch({headless: false});4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.legacyListenToEvent('request', (event) => {7 console.log(event.url());8 });9 await page.click('text=Get started');10})();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 const internalApi = page._delegate;6 internalApi.legacyListenToEvent('request', (request) => {7 console.log(request.url());8 });9 await page.click('text=Get started');10 await browser.close();11})();12const { chromium } = require('playwright');13(async () => {14 const browser = await chromium.launch();15 const page = await browser.newPage();16 await page.route('**', route => {17 console.log(route.request().url());18 route.continue();19 });20 await page.click('text=Get started');21 await browser.close();22})();
Using AI Code Generation
1import { legacyListenToEvent } from '@playwright/test/lib/legacyListenToEvent';2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 const [event] = await legacyListenToEvent(page, 'request');8 console.log('Request to: ' + event.url());9 await browser.close();10})();11import { PlaywrightTestConfig } from '@playwright/test';12const config: PlaywrightTestConfig = {13 use: {14 },15};16export default config;
Using AI Code Generation
1const { legacyListenToEvent } = require('playwright/lib/server/transport');2const { chromium } = require('playwright');3const browser = await chromium.launch();4const page = await browser.newPage();5legacyListenToEvent(page, 'request', request => {6 console.log('request', request);7});8await browser.close();9const { chromium } = require('playwright');10const browser = await chromium.launch();11const page = await browser.newPage();12page.on('request', request => {13 console.log('request', request);14});15await browser.close();16const { chromium } = require('playwright');17const browser = await chromium.launch();18const page = await browser.newPage();19page.on('request', request => {20 console.log('request', request);21});22await browser.close();23const { chromium } = require('playwright');24const browser = await chromium.launch();25const page = await browser.newPage();26page.on('request', request => {27 console.log('request', request);28});29await browser.close();30const { chromium } = require('playwright');31const browser = await chromium.launch();32const page = await browser.newPage();
Using AI Code Generation
1const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');2const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');3const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');4const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');5const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');6const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');7const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');8const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');9const { legacyListenToEvent } = require('@playwright/test/lib/server/transport');10const { legacyListenToEvent } = require('@playwright/test/lib/server/transport
Using AI Code Generation
1const playwright = require('playwright');2const { legacyListenToEvent } = require('playwright/lib/internal/transport');3const { chromium } = playwright;4const browser = await chromium.launch();5const context = await browser.newContext();6const page = await context.newPage();7await page.waitForSelector('text=Get started');8const element = await page.$('text=Get started');9const listener = legacyListenToEvent(page, 'request', (request) => {10 console.log(request.url());11});12await element.click();13await listener.dispose();14const playwright = require('playwright');15const { legacyListenToEvent } = require('playwright/lib/internal/transport');16const { chromium } = playwright;17const browser = await chromium.launch();18const context = await browser.newContext();19const page = await context.newPage();20await page.waitForSelector('text=Get started');21const element = await page.$('text=Get started');22const listener = legacyListenToEvent(page, 'request', (request) => {23 console.log(request.url());24});25await element.click();26await listener.dispose();
Using AI Code Generation
1const { legacyListenToEvent } = require('playwright/lib/server/playwright');2legacyListenToEvent('page', 'close', (event) => {3 console.log(event);4});5const { legacyListenToEvent } = require('playwright/lib/server/playwright');6legacyListenToEvent('page', 'close', (event) => {7 console.log(event);8});9const { legacyListenToEvent } = require('playwright/lib/server/playwright');10legacyListenToEvent('page', 'close', (event) => {11 console.log(event);12});13const { legacyListenToEvent } = require('playwright/lib/server/playwright');14legacyListenToEvent('page', 'close', (event) => {15 console.log(event);16});17const { legacyListenToEvent } = require('playwright/lib/server/playwright');18legacyListenToEvent('page', 'close', (event) => {19 console.log(event);20});21const { legacyListenToEvent } = require('playwright/lib/server/playwright');22legacyListenToEvent('page', 'close', (event) => {23 console.log(event);24});25const { legacyListenToEvent } = require('playwright/lib/server/playwright');26legacyListenToEvent('page', 'close', (event) => {27 console.log(event);28});29const { legacyListenToEvent } = require('playwright/lib/server/playwright');30legacyListenToEvent('page', 'close', (event) => {31 console.log(event);32});33const { legacyListenToEvent } = require('playwright/lib/server/playwright');34legacyListenToEvent('page', 'close', (event) =>
Using AI Code Generation
1const playwright = require('playwright');2const { legacyListenToEvent } = playwright;3legacyListenToEvent('request', (request) => {4 console.log('request received', request.url());5});6const playwright = require('playwright');7const { listenToEvent } = playwright;8let listener = listenToEvent('request', (request) => {9 console.log('request received', request.url());10});11listener.dispose();12const playwright = require('playwright');13const { listenToEvent } = playwright;14let listener = listenToEvent('request', (request) => {15 console.log('request received', request.url());16});17listener.dispose();18const playwright = require('playwright');19const { listenToEvent } = playwright;20let listener = listenToEvent('request', (request) => {21 console.log('request received', request.url());22});23listener.dispose();24const playwright = require('playwright');25const { listenToEvent } = playwright;26let listener = listenToEvent('request', (request) => {27 console.log('request received', request.url());28});29listener.dispose();30const playwright = require('playwright');31const { listenToEvent } = playwright;32let listener = listenToEvent('request', (request) => {33 console.log('request received', request.url());34});35listener.dispose();
Using AI Code Generation
1const { legacyListenToEvent } = require('playwright/lib/server/frames');2legacyListenToEvent( page, 'request', (request) => {3 console.log('Request URL: ' + request.url());4} );5const { legacyListenToEvent } = require('playwright/lib/server/frames');6legacyListenToEvent( page, 'request', (request) => {7 console.log('Request URL: ' + request.url());8} );
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!!