Best JavaScript code snippet using playwright-internal
mixin.spec.js
Source:mixin.spec.js
...36 it('will deliver the data emitted with the event to a listener', function () {37 testObj1.addEventListener('mainEvent', function (data) {38 expect(data).toEqual(7);39 });40 testObj1.emitEvent('mainEvent', 7);41 });42 it('will pass the listener an empty object if no data is sent with the event emission', function () {43 testObj1.addEventListener('mainEvent', function (data) {44 expect(data).toEqual({});45 });46 testObj1.emitEvent('mainEvent');47 });48 it('will pass the listener an empty object if null data is sent with the event emission', function () {49 testObj1.addEventListener('mainEvent', function (data) {50 expect(data).toEqual({});51 });52 testObj1.emitEvent('mainEvent', null);53 });54 it('will pass the listener the emitted data even if the emitted data is falsy (but not null or undefined)', function () {55 var called = 0;56 testObj1.addEventListener('mainEvent', function (data) {57 called++;58 if (called === 1) {59 expect(data).toEqual(0);60 } else if (called === 2) {61 expect(data).toEqual(false);62 } else if (called === 3) {63 expect(data).toEqual('');64 }65 });66 testObj1.emitEvent('mainEvent', 0);67 testObj1.emitEvent('mainEvent', false);68 testObj1.emitEvent('mainEvent', '');69 // Just a sanity check that the listener was called and worked on all 3 pieces of data70 expect(called).toEqual(3);71 });72 it('won\'t trigger a listener callback until the event is emitted', function () {73 var called = 0;74 testObj1.emitEvent('mainEvent');75 testObj1.addEventListener('mainEvent', function () { called++; });76 expect(called).toEqual(0);77 });78 it('will throw if invalid options are passed in when adding a listener', function () {79 var unsupportedOptionsProvided = ['badoption1', 'badoption2'];80 expect(function () {81 testObj1.addEventListener('mainEvent', function () {}, { badoption1: true, badoption2: {} });82 }).toThrow(new Error('Unsupported options ' + unsupportedOptionsProvided + ' when adding event listener'));83 });84 it('can add a listener that is immediately triggered with the most recent event emission', function () {85 var called = 0;86 testObj1.emitEvent('mainEvent');87 testObj1.addEventListener('mainEvent', function () { called++; }, { replayLast: true });88 expect(called).toEqual(1);89 });90 it('won\'t try to replay the last emit on the added listener if the event hasn\'t been emitted yet', function () {91 var called = 0;92 testObj1.addEventListener('mainEvent', function () { called++; }, { replayLast: true });93 expect(called).toEqual(0);94 });95 it('automatically removes a listener if removeOnDestructionOf option is set', inject(function ($rootScope) {96 var called = 0;97 var scope = $rootScope.$new();98 testObj1.addEventListener('mainEvent', function () { called++; }, { 'removeOnDestructionOf': scope });99 testObj1.emitEvent('mainEvent');100 expect(called).toEqual(1);101 scope.$broadcast('$destroy');102 // should have been removed103 testObj1.emitEvent('mainEvent');104 expect(called).toEqual(1);105 }));106 it('automatically removes a listener after it is fired if removeAfterFired option is set', inject(function ($rootScope) {107 var called = 0;108 testObj1.addEventListener('mainEvent', function () { called++; }, { 'removeAfterFired': true });109 testObj1.emitEvent('mainEvent');110 expect(called).toEqual(1);111 // should have been removed112 testObj1.emitEvent('mainEvent');113 expect(called).toEqual(1);114 }));115 it('causes no errors if multiple automatic removal options are used and triggered', inject(function ($rootScope) {116 var called = 0;117 var scope = $rootScope.$new();118 testObj1.addEventListener('mainEvent', function () { called++; }, {119 'removeAfterFired': true, 'removeOnDestructionOf': scope120 });121 testObj1.emitEvent('mainEvent');122 expect(called).toEqual(1);123 // should have been removed124 testObj1.emitEvent('mainEvent');125 expect(called).toEqual(1);126 scope.$broadcast('$destroy');127 // should still be removed, and shouldn't throw or cause errors128 testObj1.emitEvent('mainEvent');129 expect(called).toEqual(1);130 }));131 // Emitting132 it('can emit an event that was declared when mixing in', function () {133 testObj1.emitEvent('mainEvent');134 });135 it('calls the registered callback for an event when it\'s emitted', function () {136 var called = false;137 testObj1.addEventListener('mainEvent', function () { called = true; });138 testObj1.emitEvent('mainEvent');139 expect(called).toEqual(true);140 });141 it('allows method chaining for all public event methods', function () {142 var accumulator1 = 0;143 var accumulator2 = 0;144 var fun1 = function () { accumulator1++; };145 var fun2 = function () { accumulator2++; };146 // Chain-it!147 testObj1148 .addEventListener('mainEvent', fun1)149 .addEventListener('mainEvent', fun2)150 .emitEvent('mainEvent')151 .emitEvent('mainEvent')152 .removeEventListener('mainEvent', fun1)153 .removeEventListener('mainEvent', fun2)154 .emitEvent('mainEvent');155 expect(accumulator1).toEqual(2);156 expect(accumulator2).toEqual(2);157 });158 it('calls the registered callbacks once for each event emit', function () {159 var called1 = 0;160 var called2 = 0;161 testObj1.addEventListener('mainEvent', function () { called1++; });162 testObj1.addEventListener('mainEvent', function () { called2++; });163 testObj1.emitEvent('mainEvent');164 expect(called1).toEqual(1);165 expect(called2).toEqual(1);166 testObj1.emitEvent('mainEvent');167 expect(called1).toEqual(2);168 expect(called2).toEqual(2);169 });170 // Removing171 it('does not call an event listener callback after it\'s removed', function () {172 var called = 0;173 var fun = function () { called++; };174 testObj1.addEventListener('mainEvent', fun);175 testObj1.emitEvent('mainEvent');176 expect(called).toEqual(1);177 testObj1.removeEventListener('mainEvent', fun);178 testObj1.emitEvent('mainEvent');179 expect(called).toEqual(1);180 });181 // TODO: test auto-remove of listener on scope $destroy182 // ==================183 // = Error Handling =184 // ==================185 it('throws an exception if not provided an array of events', inject(function (mixin) {186 expect(function () {187 mixin.eventEmission({}, null);188 }).toThrow(new Error('eventNames must be an array of strings'));189 expect(function () {190 mixin.eventEmission({}, {'string': 'value'});191 }).toThrow(new Error('eventNames must be an array of strings'));192 }));...
emit-event.spec.js
Source:emit-event.spec.js
1'use strict';2const EmitEvent = require('./emit-event');3describe('emit event', function() {4 const testCtx = {5 AWS: AWS,6 namespace: 'test'7 };8 it('validates event object', function() {9 assert.throws(10 () => new EmitEvent(),11 /"event" is required/12 );13 assert.throws(14 () => new EmitEvent({ group: '' }),15 /"GroupName" is not allowed to be empty/16 );17 assert.throws(18 () => new EmitEvent({ group: 1 }),19 /"GroupName" must be a string/20 );21 assert.throws(22 () => new EmitEvent({ group: '1' }),23 /"GroupName" length must be at least 3/24 );25 assert.throws(26 () => new EmitEvent({ group: '123' }),27 /"aggregate" is required/28 );29 assert.throws(30 () => new EmitEvent({ group: '123', aggregate: '' }),31 /"aggregate" is not allowed to be empty/32 );33 assert.throws(34 () => new EmitEvent({ group: '123', aggregate: 1 }),35 /"aggregate" must be a string/36 );37 assert.throws(38 () => new EmitEvent({ group: '123', aggregate: '1', type: '1' }),39 /"seq" is required/40 );41 assert.throws(42 () => new EmitEvent(43 { group: '123', aggregate: '1', type: '1', seq: '' }44 ),45 /"seq" must be a number/46 );47 assert.throws(48 () => new EmitEvent(49 { group: '123', aggregate: '1', type: '1', seq: -1 }50 ),51 /"seq" must be greater than -1/52 );53 assert.throws(54 () => new EmitEvent(55 { group: '123', aggregate: '1', type: '1', seq: 1.2 }56 ),57 /"seq" must be an integer/58 );59 assert.throws(60 () => new EmitEvent({ group: '123', seq: 0, aggregate: '1' }),61 /"type" is required/62 );63 assert.throws(64 () => new EmitEvent({ group: '123', seq: 0, aggregate: '1', type: '' }),65 /"type" is not allowed to be empty/66 );67 assert.throws(68 () => new EmitEvent({ group: '123', seq: 0, aggregate: '1', type: 1 }),69 /"type" must be a string/70 );71 assert.throws(72 () => new EmitEvent(73 { group: '123', aggregate: '1', type: '1', seq: 0, foo: { r: 1 } }74 ),75 /"foo" is not allowed/76 );77 assert.throws(78 () => new EmitEvent(79 {80 group: '123', aggregate: '1', type: '1', seq: 0, foo: { r: 1 },81 timestamp: 12382 }83 ),84 /"timestamp" is not allowed/85 );86 });87 it('creates group if does not exist', function(done) {88 this.slow(2000); // slow because it waits for table creation89 const90 ddb = new AWS.DynamoDB(),91 tableName = new rbeta.types.TableName({92 namespace: testCtx.namespace,93 group: 'emitTest'94 }).toString();95 ddb.describeTable(96 { TableName: tableName },97 (err, data) => {98 assert.equal(err.code, 'ResourceNotFoundException');99 new EmitEvent({100 group: 'emitTest',101 aggregate: '1',102 type: '1',103 seq: 0,104 data: { r: 1 }105 })106 .run(testCtx)107 .then((data) => {108 ddb.describeTable(109 { TableName: tableName },110 (err, data) => {111 assert(data.Table.StreamSpecification.StreamEnabled);112 assert.equal(113 data.Table.StreamSpecification.StreamViewType,114 'NEW_IMAGE'115 );116 // check that tracking table is created117 ddb.describeTable(118 { TableName: tableName },119 (err, data) => {120 assert(data);121 done(err);122 }123 );124 }125 );126 })127 .catch(done);128 }129 );130 });131 it('does not allow emitting event of duplicated event', function(done) {132 new EmitEvent({133 group: 'emitTest',134 aggregate: 'test1',135 type: '1',136 seq: 0137 })138 .run(testCtx)139 .then(140 () => new EmitEvent({141 group: 'emitTest',142 aggregate: 'test1',143 type: '2',144 seq: 0145 }).run(testCtx)146 )147 .catch((err) => {148 assert.equal(err.code, 'ConditionalCheckFailedException');149 done();150 });151 });152 it('test emitting events', function(done) {153 new EmitEvent({154 group: 'emitTest',155 aggregate: 'test2',156 type: '1',157 seq: 0158 })159 .run(testCtx)160 .then(161 () => new EmitEvent({162 group: 'emitTest',163 aggregate: 'test2',164 type: '2',165 seq: 1166 }).run(testCtx)167 )168 .then(() => done()).catch(done);169 });...
test.js
Source:test.js
...7 var didPop;8 emitter.on( 'pop', function() {9 didPop = true;10 });11 emitter.emitEvent( 'pop' );12 assert.ok( didPop, 'event emitted' );13});14it( 'emitEvent should pass argument to listener', function() {15 var emitter = new EvEmitter();16 var result;17 function onPop( arg ) {18 result = arg;19 }20 emitter.on( 'pop', onPop );21 emitter.emitEvent( 'pop', [ 1 ] );22 assert.equal( result, 1, 'event emitted, arg passed' );23});24it( 'does not allow same listener to be added', function() {25 var emitter = new EvEmitter();26 var ticks = 0;27 function onPop() {28 ticks++;29 }30 emitter.on( 'pop', onPop );31 emitter.on( 'pop', onPop );32 var _onPop = onPop;33 emitter.on( 'pop', _onPop );34 emitter.emitEvent('pop');35 assert.equal( ticks, 1, '1 tick for same listener' );36});37it( 'should remove listener with .off()', function() {38 var emitter = new EvEmitter();39 var ticks = 0;40 function onPop() {41 ticks++;42 }43 emitter.on( 'pop', onPop );44 emitter.emitEvent('pop');45 emitter.off( 'pop', onPop );46 emitter.emitEvent('pop');47 assert.equal( ticks, 1, '.off() removed listener' );48 // reset49 var ary = [];50 ticks = 0;51 emitter.allOff();52 function onPopA() {53 ticks++;54 ary.push('a');55 if ( ticks == 2 ) {56 emitter.off( 'pop', onPopA );57 }58 }59 function onPopB() {60 ary.push('b');61 }62 emitter.on( 'pop', onPopA );63 emitter.on( 'pop', onPopB );64 emitter.emitEvent('pop'); // a,b65 emitter.emitEvent('pop'); // a,b - remove onPopA66 emitter.emitEvent('pop'); // b67 assert.equal( ary.join(','), 'a,b,a,b,b', '.off in listener does not interfer' );68});69it( 'should handle once()', function() {70 var emitter = new EvEmitter();71 var ary = [];72 emitter.on( 'pop', function() {73 ary.push('a');74 });75 emitter.once( 'pop', function() {76 ary.push('b');77 });78 emitter.on( 'pop', function() {79 ary.push('c');80 });81 emitter.emitEvent('pop');82 emitter.emitEvent('pop');83 assert.equal( ary.join(','), 'a,b,c,a,c', 'once listener triggered once' );84 // reset85 emitter.allOff();86 ary = [];87 // add two identical but not === listeners, only do one once88 emitter.on( 'pop', function() {89 ary.push('a');90 });91 emitter.once( 'pop', function() {92 ary.push('a');93 });94 emitter.emitEvent('pop');95 emitter.emitEvent('pop');96 assert.equal( ary.join(','), 'a,a,a', 'identical listeners do not interfere with once' );97});98it( 'does not infinite loop in once()', function() {99 var emitter = new EvEmitter();100 var ticks = 0;101 function onPop() {102 ticks++;103 if ( ticks < 4 ) {104 emitter.emitEvent('pop');105 }106 }107 emitter.once( 'pop', onPop );108 emitter.emitEvent('pop');109 assert.equal( ticks, 1, '1 tick with emitEvent in once' );110});111it( 'handles emitEvent with no listeners', function() {112 var emitter = new EvEmitter();113 assert.doesNotThrow( function() {114 emitter.emitEvent( 'pop', [ 1, 2, 3 ] );115 });116 function onPop() {}117 emitter.on( 'pop', onPop );118 emitter.off( 'pop', onPop );119 assert.doesNotThrow( function() {120 emitter.emitEvent( 'pop', [ 1, 2, 3 ] );121 });122 emitter.on( 'pop', onPop );123 emitter.emitEvent( 'pop', [ 1, 2, 3 ] );124 emitter.off( 'pop', onPop );125 assert.doesNotThrow( function() {126 emitter.emitEvent( 'pop', [ 1, 2, 3 ] );127 });128});129it( 'removes all listeners after allOff', function() {130 var emitter = new EvEmitter();131 var ary = [];132 emitter.on( 'pop', function() {133 ary.push('a');134 });135 emitter.on( 'pop', function() {136 ary.push('b');137 });138 emitter.once( 'pop', function() {139 ary.push('c');140 });141 emitter.emitEvent('pop');142 emitter.allOff();143 emitter.emitEvent('pop');144 assert.equal( ary.join(','), 'a,b,c', 'allOff removed listeners' );...
globecontrol.js
Source:globecontrol.js
...24 viewer.controls._mouseToPan(10, 10);25 viewer.controls.update();26 });27 it('mouse down', function (done) {28 renderer.domElement.emitEvent('mousedown', event).then(() => {29 renderer.domElement.emitEvent('mousemove', event);30 renderer.domElement.emitEvent('mouseup', event);31 done();32 });33 });34 it('dolly', function () {35 viewer.controls.dollyIn();36 viewer.controls.dollyOut();37 });38 it('mouse down + crtl', function (done) {39 event.keyCode = 17;40 renderer.domElement.emitEvent('keydown', event).then(() => {41 renderer.domElement.emitEvent('mousedown', event).then(() => {42 renderer.domElement.emitEvent('mousemove', event);43 renderer.domElement.emitEvent('mouseup', event);44 renderer.domElement.emitEvent('keyup', event);45 done();46 });47 });48 });49 it('mouse wheel', function (done) {50 renderer.domElement.emitEvent('mousewheel', event).then(() => {51 done();52 });53 });54 it('mouse dblclick', function (done) {55 renderer.domElement.emitEvent('dblclick', event).then(() => {56 done();57 });58 });59 event.touches = [1, 1];60 it('touch start', function (done) {61 renderer.domElement.emitEvent('touchstart', event).then(() => {62 done();63 });64 });65 it('touch move', function () {66 renderer.domElement.emitEvent('touchmove', event);67 });68 it('touch end', function () {69 renderer.domElement.emitEvent('touchend', event);70 });...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.emitEvent('myEvent', { foo: 'bar' });7 await browser.close();8})();9I am trying to use the emitEvent method of Playwright Internal API to emit custom events from the browser. I have tried the above code and I am getting the following error: (node:12744) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed. I am not sure if the emitEvent method is not working or I am using it in the wrong way. Can someone please help me with this?
Using AI Code Generation
1const { emitEvent } = require('playwright/lib/server/frames');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frame');4const { FrameManager } = require('playwright/lib/server/frameManager');5const { CDPSession } = require('playwright/lib/server/chromium/cdpsession');6const { Protocol } = require('playwright/lib/server/ch
Using AI Code Generation
1const { _electron: electron } = require('playwright');2const { app } = require('electron');3const { BrowserWindow } = require('electron/main');4(async () => {5 const browserWindow = new BrowserWindow();6 await browserWindow.webContents.executeJavaScript(`window.playwright = require('playwright')`);7 await browserWindow.webContents.executeJavaScript(`window.playwright._electron.emitEvent('my-event', {detail: 'my-detail'})`);8 await app.whenReady();9 await app.quit();10})();11const { test, expect } = require('@playwright/test');12test('test', async ({ page }) => {13 const [event] = await Promise.all([14 page.waitForEvent('my-event'),15 page.evaluate(() => window.playwright._electron.emitEvent('my-event', {detail: 'my-detail'})),16 ]);17 expect(event).toEqual({detail: 'my-detail'});18});19const { webkit } = require('playwright');20(async () => {21 const browser = await webkit.launch();22 const context = await browser.newContext();23 const page = await context.newPage();24 await page.click('text=Get Started');25 const text = await page.textContent('text=Get Started');26 console.log(text);27 await browser.close();28})();29const { webkit } = require('playwright');30(async () => {31 const browser = await webkit.launch();32 const context = await browser.newContext();
Using AI Code Generation
1const { emitEvent } = require('playwright/lib/internal/telemetry/recorder');2emitEvent({3 timestamp: Date.now(),4 data: {5 },6});7module.exports = {8 launchOptions: {9 },10 config: require('./jest-playwright.config'),11};
Using AI Code Generation
1const { emitEvent } = require('playwright-core/lib/helper');2const { Page } = require('playwright-core/lib/server/page');3const { Events } = require('playwright-core/lib/server/events');4class MyPage extends Page {5 constructor(context, pageOrError) {6 super(context, pageOrError);7 this._eventEmitter = new Events();8 }9 async test() {10 await this._eventEmitter.emit(Page.Events.Request, {11 });12 }13}14const myPage = new MyPage();15myPage.test().then(() => console.log('Done'));16const { emitEvent } = require('playwright-core/lib/helper');17const { Page } = require('playwright-core/lib/server/page');18const { Events } = require('playwright-core/lib/server/events');19class MyPage extends Page {20 constructor(context, pageOrError) {21 super(context, pageOrError);22 this._eventEmitter = new Events();23 }24 async test() {25 await this._eventEmitter.emit(Page.Events.Request, {26 });27 }28}29const myPage = new MyPage();30myPage.test().then(() => console.log('Done'));31const { emitEvent } = require('playwright-core/lib/helper');32const { Page } = require('playwright-core/lib/server/page');33const { Events } = require('playwright-core/lib/server/events');34class MyPage extends Page {35 constructor(context, pageOrError) {36 super(context, pageOrError);37 this._eventEmitter = new Events();38 }39 async test() {40 await this._eventEmitter.emit(Page.Events.Request, {41 });42 }43}44const myPage = new MyPage();45myPage.test().then(() => console.log('Done'));46const { emitEvent } = require('playwright-core/lib/helper');47const { Page } = require('playwright-core
Using AI Code Generation
1const { chromium } = require('playwright');2const { emitEvent } = require('playwright/lib/internal/telemetry/telemetry');3async function main(){4 const browser = await chromium.launch();5 const page = await browser.newPage();6 await emitEvent(page, 'MyEvent', { myCustomKey: 'myCustomValue' });7 await browser.close();8}9main();
Using AI Code Generation
1const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');2InternalEventEmitter.emitEvent('myEvent', 'hello, world');3InternalEventEmitter.emitEvent('myEvent', 'hello, world');4const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');5InternalEventEmitter.emitEvent('myEvent', 'hello, world');6const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');7InternalEventEmitter.emitEvent('myEvent', 'hello, world');8const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');9InternalEventEmitter.emitEvent('myEvent', 'hello, world');10const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');11InternalEventEmitter.emitEvent('myEvent', 'hello, world');12const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');13InternalEventEmitter.emitEvent('myEvent', 'hello, world');14const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');15InternalEventEmitter.emitEvent('myEvent', 'hello, world');16const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');17InternalEventEmitter.emitEvent('myEvent', 'hello, world');18const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');19InternalEventEmitter.emitEvent('myEvent', 'hello, world');20const { InternalEventEmitter } = require('@playwright/test/lib/utils/events');
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!!