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

hooks.js

Source: hooks.js Github

copy
1'use strict';
2
3const _ = require('lodash');
4const Utils = require('./utils');
5const Promise = require('./promise');
6const debug = Utils.getLogger().debugContext('hooks');
7
8const hookTypes = {
9  beforeValidate: {params: 2},
10  afterValidate: {params: 2},
11  validationFailed: {params: 3},
12  beforeCreate: {params: 2},
13  afterCreate: {params: 2},
14  beforeDestroy: {params: 2},
15  afterDestroy: {params: 2},
16  beforeRestore: {params: 2},
17  afterRestore: {params: 2},
18  beforeUpdate: {params: 2},
19  afterUpdate: {params: 2},
20  beforeSave: {params: 2, proxies: ['beforeUpdate', 'beforeCreate']},
21  afterSave: {params: 2, proxies: ['afterUpdate', 'afterCreate']},
22  beforeUpsert: {params: 2},
23  afterUpsert: {params: 2},
24  beforeBulkCreate: {params: 2},
25  afterBulkCreate: {params: 2},
26  beforeBulkDestroy: {params: 1},
27  afterBulkDestroy: {params: 1},
28  beforeBulkRestore: {params: 1},
29  afterBulkRestore: {params: 1},
30  beforeBulkUpdate: {params: 1},
31  afterBulkUpdate: {params: 1},
32  beforeFind: {params: 1},
33  beforeFindAfterExpandIncludeAll: {params: 1},
34  beforeFindAfterOptions: {params: 1},
35  afterFind: {params: 2},
36  beforeCount: {params: 1},
37  beforeDefine: {params: 2, sync: true},
38  afterDefine: {params: 1, sync: true},
39  beforeInit: {params: 2, sync: true},
40  afterInit: {params: 1, sync: true},
41  beforeConnect: {params: 1},
42  afterConnect: {params: 2},
43  beforeSync: {params: 1},
44  afterSync: {params: 1},
45  beforeBulkSync: {params: 1},
46  afterBulkSync: {params: 1}
47};
48exports.hooks = hookTypes;
49
50const hookAliases = {
51  beforeDelete: 'beforeDestroy',
52  afterDelete: 'afterDestroy',
53  beforeBulkDelete: 'beforeBulkDestroy',
54  afterBulkDelete: 'afterBulkDestroy',
55  beforeConnection: 'beforeConnect'
56};
57exports.hookAliases = hookAliases;
58
59/**
60 * get array of current hook and its proxied hooks combined
61 * @private
62 */
63const getProxiedHooks = hookType =>
64  hookTypes[hookType].proxies
65    ? hookTypes[hookType].proxies.concat(hookType)
66    : [hookType]
67;
68
69function getHooks(hookType) {
70  return (this.options.hooks || {})[hookType] || [];
71};
72
73const Hooks = {
74  /**
75   * Process user supplied hooks definition
76   *
77   * @param {Object} hooks
78   *
79   * @private
80   * @memberOf Sequelize
81   * @memberOf Sequelize.Model
82   */
83  _setupHooks(hooks) {
84    this.options.hooks = {};
85    _.map(hooks || {}, (hooksArray, hookName) => {
86      if (!_.isArray(hooksArray)) hooksArray = [hooksArray];
87      hooksArray.forEach(hookFn => this.addHook(hookName, hookFn));
88    });
89  },
90
91  runHooks(hooks) {
92    if (!hooks) throw new Error('runHooks requires at least 1 argument');
93
94    const hookArgs = Utils.sliceArgs(arguments, 1);
95    let hookType;
96
97    if (typeof hooks === 'string') {
98      hookType = hooks;
99      hooks = getHooks.call(this, hookType);
100
101      if (this.sequelize) {
102        hooks = hooks.concat(getHooks.call(this.sequelize, hookType));
103      }
104    }
105
106    if (!Array.isArray(hooks)) {
107      hooks = [hooks];
108    }
109
110    // synchronous hooks
111    if (hookTypes[hookType] && hookTypes[hookType].sync) {
112      for (let hook of hooks) {
113        if (typeof hook === 'object') {
114          hook = hook.fn;
115        }
116
117        debug(`running hook(sync) ${hookType}`);
118        hook.apply(this, hookArgs);
119      }
120      return;
121    }
122
123    // asynchronous hooks (default)
124    return Promise.each(hooks, hook => {
125      if (typeof hook === 'object') {
126        hook = hook.fn;
127      }
128
129      debug(`running hook ${hookType}`);
130      return Promise.resolve(hook.apply(this, hookArgs));
131    }).return();
132  },
133
134  hook() {
135    Utils.deprecate('hook() method has been deprecated, please use addHook() method instead');
136    return Hooks.addHook.apply(this, arguments);
137  },
138
139  /**
140   * Add a hook to the model
141   *
142   * @param {String}    hookType
143   * @param {String}    [name]    Provide a name for the hook function. It can be used to remove the hook later or to order hooks based on some sort of priority system in the future.
144   * @param {Function}  fn        The hook function
145   *
146   * @memberOf Sequelize
147   * @memberOf Sequelize.Model
148   */
149  addHook(hookType, name, fn) {
150    if (typeof name === 'function') {
151      fn = name;
152      name = null;
153    }
154
155    debug(`adding hook ${hookType}`);
156    const originalHookType = hookType;
157    hookType = hookAliases[hookType] || hookType;
158
159    if (hookAliases[originalHookType]) {
160      Utils.deprecate(`${originalHookType} hook has been deprecated, please use ${hookType} hook instead`);
161    }
162
163    // check for proxies, add them too
164    hookType = getProxiedHooks(hookType);
165
166    _.each(hookType, type => {
167      this.options.hooks[type] = getHooks.call(this, type);
168      this.options.hooks[type].push(name ? {name, fn} : fn);
169    });
170
171    return this;
172  },
173
174  /**
175   * Remove hook from the model
176   *
177   * @param {String} hookType
178   * @param {String|Function} name
179   *
180   * @memberOf Sequelize
181   * @memberOf Sequelize.Model
182   */
183  removeHook(hookType, name) {
184    hookType = hookAliases[hookType] || hookType;
185    const isReference = typeof name === 'function' ? true : false;
186
187    if (!this.hasHook(hookType)) {
188      return this;
189    }
190
191    Utils.debug(`removing hook ${hookType}`);
192
193    // check for proxies, add them too
194    hookType = getProxiedHooks(hookType);
195
196    for (const type of hookType) {
197      this.options.hooks[type] = this.options.hooks[type].filter(hook => {
198        if (isReference && typeof hook === 'function') {
199          return hook !== name; // check if same method
200        } else if (!isReference && typeof hook === 'object') {
201          return hook.name !== name;
202        }
203        return true;
204      });
205    }
206
207    return this;
208  },
209
210  /**
211   * Check whether the mode has any hooks of this type
212   *
213   * @param {String}  hookType
214   *
215   * @alias hasHooks
216   * @memberOf Sequelize
217   * @memberOf Sequelize.Model
218   */
219  hasHook(hookType) {
220    return this.options.hooks[hookType] && !!this.options.hooks[hookType].length;
221  }
222};
223Hooks.hasHooks = Hooks.hasHook;
224
225
226function applyTo(target) {
227  _.mixin(target, Hooks);
228
229  const allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
230  for (const hook of allHooks) {
231    target[hook] = function(name, callback) {
232      return this.addHook(hook, name, callback);
233    };
234  }
235}
236exports.applyTo = applyTo;
237
238/**
239 * A hook that is run before validation
240 * @param {String}   name
241 * @param {Function} fn   A callback function that is called with instance, options
242 * @name beforeValidate
243 * @memberOf Sequelize.Model
244 */
245
246/**
247 * A hook that is run after validation
248 * @param {String}   name
249 * @param {Function} fn   A callback function that is called with instance, options
250 * @name afterValidate
251 * @memberOf Sequelize.Model
252 */
253
254/**
255 * A hook that is run when validation fails
256 * @param {String}   name
257 * @param {Function} fn   A callback function that is called with instance, options, error. Error is the
258 * SequelizeValidationError. If the callback throws an error, it will replace the original validation error.
259 * @name validationFailed
260 * @memberOf Sequelize.Model
261 */
262
263/**
264 * A hook that is run before creating a single instance
265 * @param {String}   name
266 * @param {Function} fn   A callback function that is called with attributes, options
267 * @name beforeCreate
268 * @memberOf Sequelize.Model
269 */
270
271/**
272 * A hook that is run after creating a single instance
273 * @param {String}   name
274 * @param {Function} fn   A callback function that is called with attributes, options
275 * @name afterCreate
276 * @memberOf Sequelize.Model
277 */
278
279/**
280 * A hook that is run before creating or updating a single instance, It proxies `beforeCreate` and `beforeUpdate`
281 * @param {String}   name
282 * @param {Function} fn   A callback function that is called with attributes, options
283 * @name beforeSave
284 * @memberOf Sequelize.Model
285 */
286
287/**
288 * A hook that is run before upserting
289 * @param {String}   name
290 * @param {Function} fn   A callback function that is called with attributes, options
291 * @name beforeUpsert
292 * @memberOf Sequelize.Model
293 */
294
295/**
296 * A hook that is run after upserting
297 * @param {String}   name
298 * @param {Function} fn   A callback function that is called with attributes, options
299 * @name afterUpsert
300 * @memberOf Sequelize.Model
301 */
302
303/**
304  * A hook that is run after creating or updating a single instance, It proxies `afterCreate` and `afterUpdate`
305  * @param {String}   name
306  * @param {Function} fn   A callback function that is called with attributes, options
307  * @name afterSave
308  * @memberOf Sequelize.Model
309  */
310
311/**
312 * A hook that is run before destroying a single instance
313 * @param {String}   name
314 * @param {Function} fn   A callback function that is called with instance, options
315 *
316 * @name beforeDestroy
317 * @alias beforeDelete
318 * @memberOf Sequelize.Model
319 */
320
321/**
322 * A hook that is run after destroying a single instance
323 * @param {String}   name
324 * @param {Function} fn   A callback function that is called with instance, options
325 *
326 * @name afterDestroy
327 * @alias afterDelete
328 * @memberOf Sequelize.Model
329 */
330
331/**
332 * A hook that is run before restoring a single instance
333 * @param {String}   name
334 * @param {Function} fn   A callback function that is called with instance, options
335 *
336 * @name beforeRestore
337 * @memberOf Sequelize.Model
338 */
339
340/**
341 * A hook that is run after restoring a single instance
342 * @param {String}   name
343 * @param {Function} fn   A callback function that is called with instance, options
344 *
345 * @name afterRestore
346 * @memberOf Sequelize.Model
347 */
348
349/**
350 * A hook that is run before updating a single instance
351 * @param {String}   name
352 * @param {Function} fn   A callback function that is called with instance, options
353 * @name beforeUpdate
354 * @memberOf Sequelize.Model
355 */
356
357/**
358 * A hook that is run after updating a single instance
359 * @param {String}   name
360 * @param {Function} fn   A callback function that is called with instance, options
361 * @name afterUpdate
362 * @memberOf Sequelize.Model
363 */
364
365/**
366 * A hook that is run before creating instances in bulk
367 * @param {String}   name
368 * @param {Function} fn   A callback function that is called with instances, options
369 * @name beforeBulkCreate
370 * @memberOf Sequelize.Model
371 */
372
373/**
374 * A hook that is run after creating instances in bulk
375 * @param {String}   name
376 * @param {Function} fn   A callback function that is called with instances, options
377 * @name afterBulkCreate
378 * @memberOf Sequelize.Model
379 */
380
381/**
382 * A hook that is run before destroying instances in bulk
383 * @param {String}   name
384 * @param {Function} fn   A callback function that is called with options
385 *
386 * @name beforeBulkDestroy
387 * @alias beforeBulkDelete
388 * @memberOf Sequelize.Model
389 */
390
391/**
392 * A hook that is run after destroying instances in bulk
393 * @param {String}   name
394 * @param {Function} fn   A callback function that is called with options
395 *
396 * @name afterBulkDestroy
397 * @alias afterBulkDelete
398 * @memberOf Sequelize.Model
399 */
400
401/**
402 * A hook that is run before restoring instances in bulk
403 * @param {String}   name
404 * @param {Function} fn   A callback function that is called with options
405 *
406 * @name beforeBulkRestore
407 * @memberOf Sequelize.Model
408 */
409
410/**
411 * A hook that is run after restoring instances in bulk
412 * @param {String}   name
413 * @param {Function} fn   A callback function that is called with options
414 *
415 * @name afterBulkRestore
416 * @memberOf Sequelize.Model
417 */
418
419/**
420 * A hook that is run before updating instances in bulk
421 * @param {String}   name
422 * @param {Function} fn   A callback function that is called with options
423 * @name beforeBulkUpdate
424 * @memberOf Sequelize.Model
425 */
426
427/**
428 * A hook that is run after updating instances in bulk
429 * @param {String}   name
430 * @param {Function} fn   A callback function that is called with options
431 * @name afterBulkUpdate
432 * @memberOf Sequelize.Model
433 */
434
435/**
436 * A hook that is run before a find (select) query
437 * @param {String}   name
438 * @param {Function} fn   A callback function that is called with options
439 * @name beforeFind
440 * @memberOf Sequelize.Model
441 */
442
443/**
444 * A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
445 * @param {String}   name
446 * @param {Function} fn   A callback function that is called with options
447 * @name beforeFindAfterExpandIncludeAll
448 * @memberOf Sequelize.Model
449 */
450
451/**
452 * A hook that is run before a find (select) query, after all option parsing is complete
453 * @param {String}   name
454 * @param {Function} fn   A callback function that is called with options
455 * @name beforeFindAfterOptions
456 * @memberOf Sequelize.Model
457 */
458
459/**
460 * A hook that is run after a find (select) query
461 * @param {String}   name
462 * @param {Function} fn   A callback function that is called with instance(s), options
463 * @name afterFind
464 * @memberOf Sequelize.Model
465 */
466
467/**
468 * A hook that is run before a count query
469 * @param {String}   name
470 * @param {Function} fn   A callback function that is called with options
471 * @name beforeCount
472 * @memberOf Sequelize.Model
473 */
474
475/**
476 * A hook that is run before a define call
477 * @param {String}   name
478 * @param {Function} fn   A callback function that is called with attributes, options
479 * @name beforeDefine
480 * @memberOf Sequelize
481 */
482
483/**
484 * A hook that is run after a define call
485 * @param {String}   name
486 * @param {Function} fn   A callback function that is called with factory
487 * @name afterDefine
488 * @memberOf Sequelize
489 */
490
491/**
492 * A hook that is run before Sequelize() call
493 * @param {String}   name
494 * @param {Function} fn   A callback function that is called with config, options
495 * @name beforeInit
496 * @memberOf Sequelize
497 */
498
499/**
500 * A hook that is run after Sequelize() call
501 * @param {String}   name
502 * @param {Function} fn   A callback function that is called with sequelize
503 * @name afterInit
504 * @memberOf Sequelize
505 */
506
507/**
508 * A hook that is run before a connection is created
509 * @param {String}   name
510 * @param {Function} fn   A callback function that is called with config passed to connection
511 * @name beforeConnect
512 * @memberOf Sequelize
513 */
514
515/**
516 * A hook that is run after a connection is created
517 * @param {String}   name
518 * @param {Function} fn   A callback function that is called with the connection object and thye config passed to connection
519 * @name afterConnect
520 * @memberOf Sequelize
521 */
522
523/**
524 * A hook that is run before Model.sync call
525 * @param {String}   name
526 * @param {Function} fn   A callback function that is called with options passed to Model.sync
527 * @name beforeSync
528 * @memberOf Sequelize
529 */
530
531/**
532 * A hook that is run after Model.sync call
533 * @param {String}   name
534 * @param {Function} fn   A callback function that is called with options passed to Model.sync
535 * @name afterSync
536 * @memberOf Sequelize
537 */
538
539/**
540  * A hook that is run before sequelize.sync call
541  * @param {String}   name
542  * @param {Function} fn   A callback function that is called with options passed to sequelize.sync
543  * @name beforeBulkSync
544  * @memberOf Sequelize
545  */
546
547/**
548  * A hook that is run after sequelize.sync call
549  * @param {String}   name
550  * @param {Function} fn   A callback function that is called with options passed to sequelize.sync
551  * @name afterBulkSync
552  * @memberOf Sequelize
553  */
554
Full Screen

hook.js

Source: hook.js Github

copy
1const Hook = {
2    hooks: {action: {}, filter: {}},
3    addAction: function (action, callable, priority, tag) {
4        this.addHook('action', action, callable, priority, tag);
5        return this;
6    },
7    addFilter: function (action, callable, priority, tag) {
8        this.addHook('filter', action, callable, priority, tag);
9        return this;
10    },
11    doAction: function (action) {
12        this.doHook('action', action, arguments);
13        return this;
14    },
15    applyFilters: function (action) {
16        return this.doHook('filter', action, arguments);
17    },
18    removeAction: function (action, tag) {
19        this.removeHook('action', action, tag);
20        return this;
21    },
22    removeFilter: function (action, priority, tag) {
23        this.removeHook('filter', action, priority, tag);
24        return this;
25    },
26    addHook: function (hookType, action, callable, priority, tag) {
27        if (undefined === this.hooks[hookType][action]) {
28            this.hooks[hookType][action] = [];
29        }
30        var hooks = this.hooks[hookType][action];
31        if (undefined === tag) {
32            tag = action + '_' + hooks.length;
33        }
34        this.hooks[hookType][action].push({tag: tag, callable: callable, priority: priority});
35        return this;
36    },
37    doHook: function (hookType, action, args) {
38
39        // splice args from object into array and remove first index which is the hook name
40        args = Array.prototype.slice.call(args, 1);
41
42        if (undefined !== this.hooks[hookType][action]) {
43            var hooks = this.hooks[hookType][action], hook;
44            //sort by priority
45            hooks.sort(function (a, b) {
46                return a["priority"] - b["priority"];
47            });
48            for (var i = 0; i < hooks.length; i++) {
49                hook = hooks[i].callable;
50                if (typeof hook !== 'function')
51                    hook = window[hook];
52                if ('action' === hookType) {
53                    hook.apply(null, args);
54                } else {
55                    args[0] = hook.apply(null, args);
56                }
57            }
58        }
59        if ('filter' === hookType) {
60            return args[0];
61        }
62        return this;
63    },
64    removeHook: function (hookType, action, priority, tag) {
65        if (undefined !== this.hooks[hookType][action]) {
66            var hooks = this.hooks[hookType][action];
67            for (var i = hooks.length - 1; i >= 0; i--) {
68                if ((undefined === tag || tag === hooks[i].tag) && (undefined === priority || priority === hooks[i].priority)) {
69                    hooks.splice(i, 1);
70                }
71            }
72        }
73        return this;
74    }
75};
76
77export default Hook;
Full Screen

gforms_hooks.js

Source: gforms_hooks.js Github

copy
1
2//----------------------------------------------------------
3//------ JAVASCRIPT HOOK FUNCTIONS FOR GRAVITY FORMS -------
4//----------------------------------------------------------
5
6if ( ! gform ) {
7	document.addEventListener( 'gform_main_scripts_loaded', function() { gform.scriptsLoaded = true; } );
8	window.addEventListener( 'DOMContentLoaded', function() { gform.domLoaded = true; } );
9
10	var gform = {
11		domLoaded: false,
12		scriptsLoaded: false,
13		initializeOnLoaded: function( fn ) {
14			if ( gform.domLoaded && gform.scriptsLoaded ) {
15				fn();
16			} else if( ! gform.domLoaded && gform.scriptsLoaded ) {
17				window.addEventListener( 'DOMContentLoaded', fn );
18			} else {
19				document.addEventListener( 'gform_main_scripts_loaded', fn );
20			}
21		},
22		hooks: { action: {}, filter: {} },
23		addAction: function( action, callable, priority, tag ) {
24			gform.addHook( 'action', action, callable, priority, tag );
25		},
26		addFilter: function( action, callable, priority, tag ) {
27			gform.addHook( 'filter', action, callable, priority, tag );
28		},
29		doAction: function( action ) {
30			gform.doHook( 'action', action, arguments );
31		},
32		applyFilters: function( action ) {
33			return gform.doHook( 'filter', action, arguments );
34		},
35		removeAction: function( action, tag ) {
36			gform.removeHook( 'action', action, tag );
37		},
38		removeFilter: function( action, priority, tag ) {
39			gform.removeHook( 'filter', action, priority, tag );
40		},
41		addHook: function( hookType, action, callable, priority, tag ) {
42			if ( undefined == gform.hooks[hookType][action] ) {
43				gform.hooks[hookType][action] = [];
44			}
45			var hooks = gform.hooks[hookType][action];
46			if ( undefined == tag ) {
47				tag = action + '_' + hooks.length;
48			}
49			if( priority == undefined ){
50				priority = 10;
51			}
52
53			gform.hooks[hookType][action].push( { tag:tag, callable:callable, priority:priority } );
54		},
55		doHook: function( hookType, action, args ) {
56
57			// splice args from object into array and remove first index which is the hook name
58			args = Array.prototype.slice.call(args, 1);
59
60			if ( undefined != gform.hooks[hookType][action] ) {
61				var hooks = gform.hooks[hookType][action], hook;
62				//sort by priority
63				hooks.sort(function(a,b){return a["priority"]-b["priority"]});
64
65				hooks.forEach( function( hookItem ) {
66					hook = hookItem.callable;
67
68					if(typeof hook != 'function')
69						hook = window[hook];
70					if ( 'action' == hookType ) {
71						hook.apply(null, args);
72					} else {
73						args[0] = hook.apply(null, args);
74					}
75				} );
76			}
77			if ( 'filter'==hookType ) {
78				return args[0];
79			}
80		},
81		removeHook: function( hookType, action, priority, tag ) {
82			if ( undefined != gform.hooks[hookType][action] ) {
83				var hooks = gform.hooks[hookType][action];
84				hooks = hooks.filter( function(hook, index, arr) {
85					var removeHook = (undefined==tag||tag==hook.tag) && (undefined==priority||priority==hook.priority);
86					return !removeHook;
87				} );
88				gform.hooks[hookType][action] = hooks;
89			}
90		}
91	};
92}
93
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)