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

pause.js

Source: pause.js Github

copy
1/**
2 * Tracks when users pause the video.
3 *
4 * Example Usage:
5 * player.on('tracking:pause', (e, data) => console.log(data))
6 *
7 * Data Attributes:
8 * => pauseCount:       Total number of Pause events triggered
9 *
10 * @function PauseTracking
11 * @param    {Object} [config={}]
12 *           An object of config left to the plugin author to define.
13 */
14
15const PauseTracking = function(config) {
16  const player = this;
17  let pauseCount = 0;
18  let timer = null;
19  let locked = false;
20  const reset = function(e) {
21    if (timer) {
22      clearTimeout(timer);
23    }
24    pauseCount = 0;
25    locked = false;
26  };
27
28  player.on('dispose', reset);
29  player.on('loadstart', reset);
30  player.on('ended', reset);
31  player.on('pause', function() {
32    if (player.scrubbing() || locked) {
33      return;
34    }
35
36    timer = setTimeout(function() {
37      pauseCount++;
38      player.trigger('tracking:pause', {pauseCount});
39    }, 300);
40  });
41};
42
43export default PauseTracking;
44
Full Screen

effect.js

Source: effect.js Github

copy
1const { isArray, isObject, isMap, isSet } = require('./utils')
2
3let shouldTrack = true
4let activeEffect
5const effectStack = []
6const trackStack = []
7const targetMap = new WeakMap()
8
9const ITERATE_KEY = Symbol('')
10const MAP_KEY_ITERATE_KEY = Symbol('')
11
12const pauseTracking = () => {
13  trackStack.push(shouldTrack)
14  shouldTrack = false
15}
16
17const enableTracking = () => {
18  trackStack.push(shouldTrack)
19  shouldTrack = true
20}
21
22const resetTracking = () => {
23  const last = trackStack.pop()
24  shouldTrack = last === undefined ? true : last
25}
26
27const effect = (fn, options = {}) => {
28  const effectIns = function reactiveEffect() {
29    if (!effectIns.active) {
30      return fn()
31    }
32    if (!effectStack.includes(effectIns)) {
33      clearEffect(effectIns)
34      try {
35        enableTracking()
36        effectStack.push(effectIns)
37        activeEffect = effectIns
38        return fn()
39      } catch (e) {
40        console.log(e)
41      } finally {
42        effectStack.pop()
43        resetTracking()
44        activeEffect = effectStack[effectStack.length - 1]
45      }
46    }
47  }
48  effectIns.active = true
49  effectIns.deps = []
50  effectIns.options = options
51  return effectIns
52}
53
54const clearEffect = (effect) => {
55  const { deps } = effect
56  if (deps.length) {
57    for (let i = 0; i < deps.length; i++) {
58      deps[i].delete(effect)
59    }
60    deps.length = 0
61  }
62}
63
64const stopEffect = (effect) => {
65  if (effect.active) {
66    clearEffect(effect)
67    if (effect.options.onStop) {
68      effect.options.onStop()
69    }
70    effect.active = false
71  }
72}
73
74const track = (target, key) => {
75  if (!shouldTrack || activeEffect === undefined) {
76    return
77  }
78  let depsMap = targetMap.get(target)
79  if (!depsMap) {
80    targetMap.set(target, (depsMap = new Map()))
81  }
82
83  let dep = depsMap.get(key)
84  if (!dep) {
85    depsMap.set(key, (dep = new Set()))
86  }
87
88  if (!dep.has(activeEffect)) {
89    dep.add(activeEffect)
90    activeEffect.deps.push(dep)
91  }
92}
93
94const trigger = (target, type, key, newValue, oldValue) => {
95  const depsMap = targetMap.get(target)
96  if (!depsMap) {
97    return
98  }
99
100  const effects = new Set()
101
102  const add = (dep = []) => dep.forEach((effect) => effects.add(effect))
103
104  if (type == 'clear') {
105    depsMap.forEach(add)
106  } else if (key === 'length' && isArray(target)) {
107    depsMap.forEach((dep, key) => {
108      if (key === 'length' || key >= newValue) {
109        add(dep)
110      }
111    })
112  } else {
113    if (key) {
114      add(depsMap.get(key))
115    }
116
117    switch (type) {
118      case 'add':
119        if (!isArray(target)) {
120          //触发自身
121          add(depsMap.get(ITERATE_KEY))
122          if (isMap(target)) {
123            add(depsMap.get(MAP_KEY_ITERATE_KEY))
124          }
125        } else if (isIntegerKey(key)) {
126          // new index added to array -> length changes
127          add(depsMap.get('length'))
128        }
129        break
130      case 'delete':
131        if (!isArray(target)) {
132          add(depsMap.get(ITERATE_KEY))
133          if (isMap(target)) {
134            add(depsMap.get(MAP_KEY_ITERATE_KEY))
135          }
136        }
137        break
138      case 'set':
139        if (isMap(target)) {
140          add(depsMap.get(ITERATE_KEY))
141        }
142        break
143    }
144  }
145
146  effects.forEach((effect) => {
147    if (effect !== activeEffect) {
148      if (effect.options.scheduler) {
149        effect.options.scheduler(effect)
150      } else {
151        effect()
152      }
153    }
154  })
155}
156
157exports.effect = effect
158exports.track = track
159exports.trigger = trigger
160exports.pauseTracking = pauseTracking
161exports.enableTracking = enableTracking
162exports.ITERATE_KEY = ITERATE_KEY
163
Full Screen

power-manager.js

Source: power-manager.js Github

copy
1const { powerMonitor, powerSaveBlocker } = require('electron');
2const tracker = require('../base/task-tracker');
3const osIntegration = require('../base/os-integration');
4const Logger = require('../utils/log');
5
6const log = new Logger('PowerManager');
7
8class PowerManager {
9
10  constructor() {
11
12    this._suspendDetected = false;
13    this._powerSaveBlockedId = -1;
14
15    powerMonitor.on('suspend', () => {
16
17      log.debug('System going to sleep.');
18      this.pauseTracking();
19
20    });
21    powerMonitor.on('resume', () => {
22
23      log.debug('System resumed from sleep state.');
24      this.resumeTracking();
25
26    });
27
28    powerMonitor.on('lock-screen', () => {
29
30      log.debug('System locked.');
31      this.pauseTracking();
32
33    });
34    powerMonitor.on('unlock-screen', () => {
35
36      log.debug('System unlocked.');
37      this.resumeTracking();
38
39    });
40
41    powerMonitor.on('shutdown', () => osIntegration.gracefullExit());
42
43    tracker.on('started', () => {
44
45      this._powerSaveBlockedId = powerSaveBlocker.start('prevent-display-sleep');
46      if (powerSaveBlocker.isStarted(this._powerSaveBlockedId))
47        log.debug('Prevent display sleep while tracking!');
48      else
49        log.warning('Can\'t setup Power Save Blocker!');
50
51    });
52    tracker.on('stopped', () => {
53
54      if (this._powerSaveBlockedId > -1 && powerSaveBlocker.isStarted(this._powerSaveBlockedId)) {
55
56        log.debug('Now display can sleep!');
57        powerSaveBlocker.stop(this._powerSaveBlockedId);
58
59      }
60
61    });
62
63    log.debug('Loaded');
64
65  }
66
67  pauseTracking() {
68
69    if (tracker.active && !this._suspendDetected) {
70
71      this._suspendDetected = true;
72      tracker.pauseTicker();
73      log.debug('Tracker paused.');
74
75    }
76
77  }
78
79  resumeTracking() {
80
81    if (this._suspendDetected) {
82
83      this._suspendDetected = false;
84      tracker.resumeTicker();
85      log.debug('Tracker resumed.');
86
87    }
88
89  }
90
91}
92
93module.exports = new PowerManager();
94
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)