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

normalizeDuration.test.js

Source: normalizeDuration.test.js Github

copy
1import normalizeDuration from './../normalizeDuration';
2
3describe('normalizeDuration', () => {
4    it('shows minutes if under 1 hour duration', () => {
5        expect(normalizeDuration(3500000)).toBe('58 min');
6    });
7
8    it('shows hours if over 1 hour duration', () => {
9        expect(normalizeDuration(3600000)).toBe('1 h');
10    });
11
12    it('shows hours if over 1 hour duration', () => {
13        expect(normalizeDuration(3800000)).toBe('1 h 3 min');
14    });
15});
16
Full Screen

ve.track.js

Source: ve.track.js Github

copy
1/*!
2 * VisualEditor tracking for Wikia
3 */
4
5/* global mw, require */
6
7require( ['wikia.tracker'], function ( tracker ) {
8	/* jshint -W003 */
9	var actions = tracker.ACTIONS,
10		// These are topics used by MediaWiki, consider them reserved. Each topic should be
11		// assigned to a function which will map the data associated with a topic to a format
12		// understood by Wikia.Tracker. Keep them alphabetized.
13		mwTopics = {
14			'mwtiming.behavior.lastTransactionTillSaveDialogOpen': function ( data ) {
15				return {
16					action: actions.OPEN,
17					label: 'dialog-save',
18					value: normalizeDuration( data.duration )
19				};
20			},
21			'mwtiming.behavior.saveDialogClose': function ( data ) {
22				return {
23					action: actions.CLOSE,
24					label: 'dialog-save',
25					value: normalizeDuration( data.duration )
26				};
27			},
28			'command.execute': function ( data ) {
29				return {
30					action: actions.KEYPRESS,
31					label: 'tool-' + nameToLabel( data.name )
32				};
33			},
34			'error.createdocumentfromhtml': function ( data ) {
35				return {
36					action: actions.ERROR,
37					label: 'createdocumentfromhtml-' + data.message
38				};
39			},
40			'mwtiming.performance.system.activation': function ( data ) {
41				return {
42					action: actions.IMPRESSION,
43					label: 'edit-page-ready',
44					value: normalizeDuration( data.duration )
45				};
46			},
47			'mwtiming.performance.user.reviewComplete': function ( data ) {
48				return {
49					action: actions.SUCCESS,
50					label: 'dialog-save-review-changes',
51					value: normalizeDuration( data.duration )
52				};
53			},
54			'mwtiming.performance.user.reviewError': function ( data ) {
55				return {
56					action: actions.ERROR,
57					label: 'dialog-save-review-changes',
58					value: normalizeDuration( data.duration )
59				};
60			},
61			'mwtiming.performance.user.saveComplete': function ( data ) {
62				return {
63					action: actions.SUCCESS,
64					label: 'publish',
65					value: normalizeDuration( data.duration )
66				};
67			},
68			'mwtiming.performance.user.saveError': function ( data, topics ) {
69				if ( !data.type ) {
70					data.type = topics[ topics.length - 1 ];
71				}
72				return {
73					action: actions.ERROR,
74					label: 'publish-' + data.type,
75					retries: data.retries,
76					value: normalizeDuration( data.duration )
77				};
78			},
79			tool: function ( data ) {
80				return {
81					action: actions.CLICK,
82					label: 'tool-' + nameToLabel( data.name ),
83					value: data.toolbar === 'surface' ? 1 : 0
84				};
85			}
86		},
87		// @see {@link nameToLabel} for more information
88		nameToLabelMap = {
89			meta: 'page-settings',
90			mwSave: 'save',
91			transclusion: 'template',
92			wikiaMediaInsert: 'media-insert',
93			wikiaSourceMode: 'source'
94		},
95		// A lot of the events sent in the 'wikia' topic are tracked generically (for example,
96		// all dialog 'open' and 'close' events). Sometimes this isn't desired because we want
97		// to track them manually and provide custom data. Adding those events to this array
98		// will allow this. Events should be listed alphabetically in the format: "label/action"
99		wikiaTopicBlacklist = [
100			'dialog-save/close',
101			'dialog-save/open'
102		],
103		rUppercase = /[A-Z]/g;
104
105	/**
106	 * Convert symbolic names to tracking labels, falling back to the symbolic name if there is
107	 * nothing else to map it to.
108	 *
109	 * @example
110	 * "meta" -> "page-settings"
111	 *
112	 * @method
113	 * @param {string} name The symbolic name
114	 * @returns {string} The converted label
115	 */
116	function nameToLabel( name ) {
117		return nameToLabelMap[name] || name;
118	}
119
120	/**
121	 * Normalize time durations, for example rounding up or down or limiting
122	 * the number of decimal spaces to keep.
123	 *
124	 * @method
125	 * @param {number} duration Time in milliseconds
126	 * @returns {number} Normalized time in milliseconds
127	 */
128	function normalizeDuration( duration ) {
129		return Math.round( duration );
130	}
131
132	/**
133	 * Editor tracking function.
134	 *
135	 * @method
136	 * @param {string} topic Event sub-category (like "tool", "button", etc.); this will be joined
137	 * with data.label to form the whole label for the event.
138	 * @param {Object} data The data to send to our internal tracking function.
139	 */
140	function track( topic, data ) {
141
142		var i, mwEvent, topics,
143			params = {
144				category: 'editor-ve',
145				trackingMethod: 'analytics'
146			};
147
148		// MW events
149		if ( topic !== 'wikia' ) {
150			// MW events are categorized in dot notation (like "performance.system.activation")
151			// This allows us to follow an entire topic ("performance") without having to track
152			// all the sub-topics separately.
153			topics = topic.split( '.' );
154			for ( i = 0; i < topics.length; i++ ) {
155				topic = ( i === 0 ? '' : topic + '.' ) + topics[i];
156				mwEvent = mwTopics[topic];
157				if ( mwEvent ) {
158					// We have found a match; exit the loop
159					break;
160				}
161			}
162			// Only track things we care about
163			if ( !mwEvent ) {
164				return;
165			}
166			data = $.isFunction( mwEvent ) ? mwEvent( data, topics ) : mwEvent;
167		}
168
169		// Normalize tracking labels
170		data.label = data.label.replace( rUppercase, upperToHyphenLower );
171
172		// Don't track blacklisted Wikia tracking calls.
173		if (
174			topic === 'wikia' &&
175			$.inArray( [ data.label, data.action ].join( '/' ), wikiaTopicBlacklist ) > -1
176		) {
177			return;
178		}
179
180		// Funnel tracking
181		handleFunnel( data );
182
183		// Send off to Wikia.Tracker
184		tracker.track( ve.extendObject( params, data ) );
185	}
186
187	/**
188	 * Track fake pageviews in GA for certain events
189	 * @method
190	 * @param {Object} data The tracking data
191	 */
192	function handleFunnel( data ) {
193		var funnelEvents = [
194			'edit-page-ready/impression',
195			'edit-page-ready-toolbartest/impression',
196			'button-publish/enable',
197			'button-cancel/click',
198			'button-publish/click',
199			'dialog-save-publish/click',
200			'publish/success'
201		],
202		funnelEvent = data.label + '/' + data.action;
203
204		if ( funnelEvents.indexOf( funnelEvent ) !== -1 ) {
205			window.guaTrackPageview( '/fake-visual-editor/' + funnelEvent, 've' );
206		}
207	}
208
209	/**
210	 * Convert a string from lazyCamelCase to lowercase-hyphen style.
211	 *
212	 * @method
213	 * @param {string} match The matched string to convert.
214	 * @returns {string} The converted string.
215	 */
216	function upperToHyphenLower( match ) {
217		return '-' + match.toLowerCase();
218	}
219
220	// Track time to init when accessing directly from the URI via ?veaction=edit
221	if ( mw.libs.ve.activateOnPageLoad ) {
222		mw.hook( 've.activationComplete' ).add( function () {
223			ve.track( 'wikia', {
224				action: actions.IMPRESSION,
225				label: 'edit-page-ready-from-page-load',
226				value: normalizeDuration( ve.now() - window.wgNow )
227			} );
228		} );
229	}
230
231	// Exports
232	ve.track.actions = actions;
233	ve.track.nameToLabel = nameToLabel;
234	ve.track.normalizeDuration = normalizeDuration;
235	ve.trackSubscribeAll( track );
236	/* jshint +W003 */
237} );
238
Full Screen

duration.js

Source: duration.js Github

copy
1let Helpers = require('./helpers');
2let normalizeDuration = new Helpers().normalizeDuration;
3let absRound = new Helpers().absRound;
4let absFloor = new Helpers().absFloor;
5/**
6 * Duration object constructor
7 * @param duration
8 * @class Duration
9 * @constructor
10 */
11class Duration {
12    constructor(key, value) {
13        let duration = {},
14            data = this._data = {},
15            milliseconds = 0,
16            normalizedUnit = normalizeDuration(key, value),
17            unit = normalizedUnit.unit;
18        duration[unit] = normalizedUnit.value;
19        milliseconds = duration.milliseconds || duration.millisecond || duration.ms || 0;
20
21        let years = duration.years || duration.year || duration.y || 0,
22            months = duration.months || duration.month || duration.M || 0,
23            weeks = duration.weeks || duration.w || duration.week || 0,
24            days = duration.days || duration.d || duration.day || 0,
25            hours = duration.hours || duration.hour || duration.h || 0,
26            minutes = duration.minutes || duration.minute || duration.m || 0,
27            seconds = duration.seconds || duration.second || duration.s || 0;
28        // representation for dateAddRemove
29        this._milliseconds = milliseconds + seconds * (1e3) + minutes * (6e4) + hours * (36e5);
30        // Because of dateAddRemove treats 24 hours as different from a
31        // day when working around DST, we need to store them separately
32        this._days = days + weeks * 7;
33        // It is impossible translate months into days without knowing
34        // which months you are are talking about, so we have to store
35        // it separately.
36        this._months = months + years * 12;
37        // The following code bubbles up values, see the tests for
38        // examples of what that means.
39        data.milliseconds = milliseconds % 1000;
40        seconds += absFloor((milliseconds / 1000));
41        data.seconds = seconds % 60;
42        minutes += absRound(seconds / 60);
43        data.minutes = minutes % 60;
44        hours += absRound(minutes / 60);
45        data.hours = hours % 24;
46        days += absRound(hours / 24);
47        days += weeks * 7;
48        data.days = days % 30;
49        months += absRound(days / 30);
50        data.months = months % 12;
51        years += absRound(months / 12);
52        data.years = years;
53        return this;
54    }
55
56    valueOf () {
57        return this._milliseconds + this._days * (864e5) + this._months * (2592e6);
58    }
59}
60
61module.exports = Duration;
62
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)