Best JavaScript code snippet using playwright-internal
Scheduler-test.internal.js
Source:Scheduler-test.internal.js  
...44      timeoutID = -1;45      endOfFrame = currentTime + frameSize;46      try {47        isFlushing = true;48        _flushWork(false);49      } finally {50        isFlushing = false;51        endOfFrame = -1;52        if (hasMicrotask) {53          onTimeout();54        }55      }56      const yields = yieldedValues;57      yieldedValues = [];58      return yields;59    };60    advanceTime = ms => {61      currentTime += ms;62      jest.advanceTimersByTime(ms);63    };64    doWork = (label, timeCost) => {65      if (typeof timeCost !== 'number') {66        throw new Error('Second arg must be a number.');67      }68      advanceTime(timeCost);69      yieldValue(label);70    };71    yieldedValues = [];72    yieldValue = value => {73      yieldedValues.push(value);74    };75    clearYieldedValues = () => {76      const yields = yieldedValues;77      yieldedValues = [];78      return yields;79    };80    function onTimeout() {81      if (_flushWork === null) {82        return;83      }84      if (isFlushing) {85        hasMicrotask = true;86      } else {87        try {88          isFlushing = true;89          _flushWork(true);90        } finally {91          hasMicrotask = false;92          isFlushing = false;93        }94      }95    }96    function requestHostCallback(fw, absoluteTimeout) {97      if (_flushWork !== null) {98        throw new Error('Work is already scheduled.');99      }100      _flushWork = fw;101      timeoutID = setTimeout(onTimeout, absoluteTimeout - currentTime);102    }103    function cancelHostCallback() {104      if (_flushWork === null) {105        throw new Error('No work is scheduled.');106      }107      _flushWork = null;108      clearTimeout(timeoutID);109    }110    function getTimeRemaining() {111      return endOfFrame;112    }113    // Override host implementation114    delete global.performance;115    global.Date.now = () => {116      return currentTime;117    };118    window._schedMock = [119      requestHostCallback,120      cancelHostCallback,121      getTimeRemaining,122    ];123    const Schedule = require('scheduler');124    runWithPriority = Schedule.unstable_runWithPriority;125    ImmediatePriority = Schedule.unstable_ImmediatePriority;126    UserBlockingPriority = Schedule.unstable_UserBlockingPriority;127    NormalPriority = Schedule.unstable_NormalPriority;128    scheduleCallback = Schedule.unstable_scheduleCallback;129    cancelCallback = Schedule.unstable_cancelCallback;130    wrapCallback = Schedule.unstable_wrapCallback;131    getCurrentPriorityLevel = Schedule.unstable_getCurrentPriorityLevel;132  });133  it('flushes work incrementally', () => {134    scheduleCallback(() => doWork('A', 100));135    scheduleCallback(() => doWork('B', 200));136    scheduleCallback(() => doWork('C', 300));137    scheduleCallback(() => doWork('D', 400));138    expect(flushWork(300)).toEqual(['A', 'B']);139    expect(flushWork(300)).toEqual(['C']);140    expect(flushWork(400)).toEqual(['D']);141  });142  it('flushes work until framesize reached', () => {143    scheduleCallback(() => doWork('A1_100', 100));144    scheduleCallback(() => doWork('A2_200', 200));145    scheduleCallback(() => doWork('B1_100', 100));146    scheduleCallback(() => doWork('B2_200', 200));147    scheduleCallback(() => doWork('C1_300', 300));148    scheduleCallback(() => doWork('C2_300', 300));149    scheduleCallback(() => doWork('D_3000', 3000));150    scheduleCallback(() => doWork('E1_300', 300));151    scheduleCallback(() => doWork('E2_200', 200));152    scheduleCallback(() => doWork('F1_200', 200));153    scheduleCallback(() => doWork('F2_200', 200));154    scheduleCallback(() => doWork('F3_300', 300));155    scheduleCallback(() => doWork('F4_500', 500));156    scheduleCallback(() => doWork('F5_200', 200));157    scheduleCallback(() => doWork('F6_20', 20));158    expect(Date.now()).toEqual(0);159    // No time left after A1_100 and A2_200 are run160    expect(flushWork(300)).toEqual(['A1_100', 'A2_200']);161    expect(Date.now()).toEqual(300);162    // B2_200 is started as there is still time left after B1_100163    expect(flushWork(101)).toEqual(['B1_100', 'B2_200']);164    expect(Date.now()).toEqual(600);165    // C1_300 is started as there is even a little frame time166    expect(flushWork(1)).toEqual(['C1_300']);167    expect(Date.now()).toEqual(900);168    // C2_300 is started even though there is no frame time169    expect(flushWork(0)).toEqual(['C2_300']);170    expect(Date.now()).toEqual(1200);171    // D_3000 is very slow, but won't affect next flushes (if no172    // timeouts happen)173    expect(flushWork(100)).toEqual(['D_3000']);174    expect(Date.now()).toEqual(4200);175    expect(flushWork(400)).toEqual(['E1_300', 'E2_200']);176    expect(Date.now()).toEqual(4700);177    // Default timeout is 5000, so during F2_200, work will timeout and are done178    // in reverse, including F2_200179    expect(flushWork(1000)).toEqual([180      'F1_200',181      'F2_200',182      'F3_300',183      'F4_500',184      'F5_200',185      'F6_20',186    ]);187    expect(Date.now()).toEqual(6120);188  });189  it('cancels work', () => {190    scheduleCallback(() => doWork('A', 100));191    const callbackHandleB = scheduleCallback(() => doWork('B', 200));192    scheduleCallback(() => doWork('C', 300));193    cancelCallback(callbackHandleB);194    expect(flushWork()).toEqual([195      'A',196      // B should have been cancelled197      'C',198    ]);199  });200  it('executes the highest priority callbacks first', () => {201    scheduleCallback(() => doWork('A', 100));202    scheduleCallback(() => doWork('B', 100));203    // Yield before B is flushed204    expect(flushWork(100)).toEqual(['A']);205    runWithPriority(UserBlockingPriority, () => {206      scheduleCallback(() => doWork('C', 100));207      scheduleCallback(() => doWork('D', 100));208    });209    // C and D should come first, because they are higher priority210    expect(flushWork()).toEqual(['C', 'D', 'B']);211  });212  it('expires work', () => {213    scheduleCallback(() => doWork('A', 100));214    runWithPriority(UserBlockingPriority, () => {215      scheduleCallback(() => doWork('B', 100));216    });217    scheduleCallback(() => doWork('C', 100));218    runWithPriority(UserBlockingPriority, () => {219      scheduleCallback(() => doWork('D', 100));220    });221    // Advance time, but not by enough to expire any work222    advanceTime(249);223    expect(clearYieldedValues()).toEqual([]);224    // Advance by just a bit more to expire the high pri callbacks225    advanceTime(1);226    expect(clearYieldedValues()).toEqual(['B', 'D']);227    // Expire the rest228    advanceTime(10000);229    expect(clearYieldedValues()).toEqual(['A', 'C']);230  });231  it('has a default expiration of ~5 seconds', () => {232    scheduleCallback(() => doWork('A', 100));233    advanceTime(4999);234    expect(clearYieldedValues()).toEqual([]);235    advanceTime(1);236    expect(clearYieldedValues()).toEqual(['A']);237  });238  it('continues working on same task after yielding', () => {239    scheduleCallback(() => doWork('A', 100));240    scheduleCallback(() => doWork('B', 100));241    const tasks = [['C1', 100], ['C2', 100], ['C3', 100]];242    const C = deadline => {243      while (tasks.length > 0) {244        doWork(...tasks.shift());245        if (246          tasks.length > 0 &&247          !deadline.didTimeout &&248          deadline.timeRemaining() <= 0249        ) {250          yieldValue('Yield!');251          return C;252        }253      }254    };255    scheduleCallback(C);256    scheduleCallback(() => doWork('D', 100));257    scheduleCallback(() => doWork('E', 100));258    expect(flushWork(300)).toEqual(['A', 'B', 'C1', 'Yield!']);259    expect(flushWork()).toEqual(['C2', 'C3', 'D', 'E']);260  });261  it('continuation callbacks inherit the expiration of the previous callback', () => {262    const tasks = [['A', 125], ['B', 124], ['C', 100], ['D', 100]];263    const work = deadline => {264      while (tasks.length > 0) {265        doWork(...tasks.shift());266        if (267          tasks.length > 0 &&268          !deadline.didTimeout &&269          deadline.timeRemaining() <= 0270        ) {271          yieldValue('Yield!');272          return work;273        }274      }275    };276    // Schedule a high priority callback277    runWithPriority(UserBlockingPriority, () => scheduleCallback(work));278    // Flush until just before the expiration time279    expect(flushWork(249)).toEqual(['A', 'B', 'Yield!']);280    // Advance time by just a bit more. This should expire all the remaining work.281    advanceTime(1);282    expect(clearYieldedValues()).toEqual(['C', 'D']);283  });284  it('nested callbacks inherit the priority of the currently executing callback', () => {285    runWithPriority(UserBlockingPriority, () => {286      scheduleCallback(() => {287        doWork('Parent callback', 100);288        scheduleCallback(() => {289          doWork('Nested callback', 100);290        });291      });292    });293    expect(flushWork(100)).toEqual(['Parent callback']);294    // The nested callback has user-blocking priority, so it should295    // expire quickly.296    advanceTime(250 + 100);297    expect(clearYieldedValues()).toEqual(['Nested callback']);298  });299  it('continuations are interrupted by higher priority work', () => {300    const tasks = [['A', 100], ['B', 100], ['C', 100], ['D', 100]];301    const work = deadline => {302      while (tasks.length > 0) {303        doWork(...tasks.shift());304        if (305          tasks.length > 0 &&306          !deadline.didTimeout &&307          deadline.timeRemaining() <= 0308        ) {309          yieldValue('Yield!');310          return work;311        }312      }313    };314    scheduleCallback(work);315    expect(flushWork(100)).toEqual(['A', 'Yield!']);316    runWithPriority(UserBlockingPriority, () => {317      scheduleCallback(() => doWork('High pri', 100));318    });319    expect(flushWork()).toEqual(['High pri', 'B', 'C', 'D']);320  });321  it(322    'continutations are interrupted by higher priority work scheduled ' +323      'inside an executing callback',324    () => {325      const tasks = [['A', 100], ['B', 100], ['C', 100], ['D', 100]];326      const work = deadline => {327        while (tasks.length > 0) {328          const task = tasks.shift();329          doWork(...task);330          if (task[0] === 'B') {331            // Schedule high pri work from inside another callback332            yieldValue('Schedule high pri');333            runWithPriority(UserBlockingPriority, () =>334              scheduleCallback(() => doWork('High pri', 100)),335            );336          }337          if (338            tasks.length > 0 &&339            !deadline.didTimeout &&340            deadline.timeRemaining() <= 0341          ) {342            yieldValue('Yield!');343            return work;344          }345        }346      };347      scheduleCallback(work);348      expect(flushWork()).toEqual([349        'A',350        'B',351        'Schedule high pri',352        // Even though there's time left in the frame, the low pri callback353        // should yield to the high pri callback354        'Yield!',355        'High pri',356        // Continue low pri work357        'C',358        'D',359      ]);360    },361  );362  it('immediate callbacks fire at the end of outermost event', () => {363    runWithPriority(ImmediatePriority, () => {364      scheduleCallback(() => yieldValue('A'));365      scheduleCallback(() => yieldValue('B'));366      // Nested event367      runWithPriority(ImmediatePriority, () => {368        scheduleCallback(() => yieldValue('C'));369        // Nothing should have fired yet370        expect(clearYieldedValues()).toEqual([]);371      });372      // Nothing should have fired yet373      expect(clearYieldedValues()).toEqual([]);374    });375    // The callbacks were called at the end of the outer event376    expect(clearYieldedValues()).toEqual(['A', 'B', 'C']);377  });378  it('wrapped callbacks have same signature as original callback', () => {379    const wrappedCallback = wrapCallback((...args) => ({args}));380    expect(wrappedCallback('a', 'b')).toEqual({args: ['a', 'b']});381  });382  it('wrapped callbacks inherit the current priority', () => {383    const wrappedCallback = wrapCallback(() => {384      scheduleCallback(() => {385        doWork('Normal', 100);386      });387    });388    const wrappedInteractiveCallback = runWithPriority(389      UserBlockingPriority,390      () =>391        wrapCallback(() => {392          scheduleCallback(() => {393            doWork('User-blocking', 100);394          });395        }),396    );397    // This should schedule a normal callback398    wrappedCallback();399    // This should schedule an user-blocking callback400    wrappedInteractiveCallback();401    advanceTime(249);402    expect(clearYieldedValues()).toEqual([]);403    advanceTime(1);404    expect(clearYieldedValues()).toEqual(['User-blocking']);405    advanceTime(10000);406    expect(clearYieldedValues()).toEqual(['Normal']);407  });408  it('wrapped callbacks inherit the current priority even when nested', () => {409    const wrappedCallback = wrapCallback(() => {410      scheduleCallback(() => {411        doWork('Normal', 100);412      });413    });414    const wrappedInteractiveCallback = runWithPriority(415      UserBlockingPriority,416      () =>417        wrapCallback(() => {418          scheduleCallback(() => {419            doWork('User-blocking', 100);420          });421        }),422    );423    runWithPriority(UserBlockingPriority, () => {424      // This should schedule a normal callback425      wrappedCallback();426      // This should schedule an user-blocking callback427      wrappedInteractiveCallback();428    });429    advanceTime(249);430    expect(clearYieldedValues()).toEqual([]);431    advanceTime(1);432    expect(clearYieldedValues()).toEqual(['User-blocking']);433    advanceTime(10000);434    expect(clearYieldedValues()).toEqual(['Normal']);435  });436  it('immediate callbacks fire at the end of callback', () => {437    const immediateCallback = runWithPriority(ImmediatePriority, () =>438      wrapCallback(() => {439        scheduleCallback(() => yieldValue('callback'));440      }),441    );442    immediateCallback();443    // The callback was called at the end of the outer event444    expect(clearYieldedValues()).toEqual(['callback']);445  });446  it("immediate callbacks fire even if there's an error", () => {447    expect(() => {448      runWithPriority(ImmediatePriority, () => {449        scheduleCallback(() => {450          yieldValue('A');451          throw new Error('Oops A');452        });453        scheduleCallback(() => {454          yieldValue('B');455        });456        scheduleCallback(() => {457          yieldValue('C');458          throw new Error('Oops C');459        });460      });461    }).toThrow('Oops A');462    expect(clearYieldedValues()).toEqual(['A']);463    // B and C flush in a subsequent event. That way, the second error is not464    // swallowed.465    expect(() => flushWork(0)).toThrow('Oops C');466    expect(clearYieldedValues()).toEqual(['B', 'C']);467  });468  it('exposes the current priority level', () => {469    yieldValue(getCurrentPriorityLevel());470    runWithPriority(ImmediatePriority, () => {471      yieldValue(getCurrentPriorityLevel());472      runWithPriority(NormalPriority, () => {473        yieldValue(getCurrentPriorityLevel());474        runWithPriority(UserBlockingPriority, () => {475          yieldValue(getCurrentPriorityLevel());476        });477      });478      yieldValue(getCurrentPriorityLevel());479    });...Scheduler-test.js
Source:Scheduler-test.js  
...47      timeoutID = -1;48      endOfFrame = currentTime + frameSize;49      try {50        isFlushing = true;51        _flushWork(false);52      } finally {53        isFlushing = false;54        endOfFrame = -1;55        if (hasMicrotask) {56          onTimeout();57        }58      }59      const yields = yieldedValues;60      yieldedValues = [];61      return yields;62    };63    advanceTime = ms => {64      currentTime += ms;65      jest.advanceTimersByTime(ms);66    };67    doWork = (label, timeCost) => {68      if (typeof timeCost !== 'number') {69        throw new Error('Second arg must be a number.');70      }71      advanceTime(timeCost);72      yieldValue(label);73    };74    yieldedValues = [];75    yieldValue = value => {76      yieldedValues.push(value);77    };78    clearYieldedValues = () => {79      const yields = yieldedValues;80      yieldedValues = [];81      return yields;82    };83    function onTimeout() {84      if (_flushWork === null) {85        return;86      }87      if (isFlushing) {88        hasMicrotask = true;89      } else {90        try {91          isFlushing = true;92          _flushWork(true);93        } finally {94          hasMicrotask = false;95          isFlushing = false;96        }97      }98    }99    function requestHostCallback(fw, absoluteTimeout) {100      if (_flushWork !== null) {101        throw new Error('Work is already scheduled.');102      }103      _flushWork = fw;104      timeoutID = setTimeout(onTimeout, absoluteTimeout - currentTime);105    }106    function cancelHostCallback() {107      if (_flushWork === null) {108        throw new Error('No work is scheduled.');109      }110      _flushWork = null;111      clearTimeout(timeoutID);112    }113    function shouldYieldToHost() {114      return endOfFrame <= currentTime;115    }116    function getCurrentTime() {117      return currentTime;118    }119    // Override host implementation120    delete global.performance;121    global.Date.now = () => {122      return currentTime;123    };124    window._schedMock = [125      requestHostCallback,126      cancelHostCallback,127      shouldYieldToHost,128      getCurrentTime,129    ];130    const Schedule = require('scheduler');131    runWithPriority = Schedule.unstable_runWithPriority;132    ImmediatePriority = Schedule.unstable_ImmediatePriority;133    UserBlockingPriority = Schedule.unstable_UserBlockingPriority;134    NormalPriority = Schedule.unstable_NormalPriority;135    scheduleCallback = Schedule.unstable_scheduleCallback;136    cancelCallback = Schedule.unstable_cancelCallback;137    wrapCallback = Schedule.unstable_wrapCallback;138    getCurrentPriorityLevel = Schedule.unstable_getCurrentPriorityLevel;139    shouldYield = Schedule.unstable_shouldYield;140  });141  it('flushes work incrementally', () => {142    scheduleCallback(() => doWork('A', 100));143    scheduleCallback(() => doWork('B', 200));144    scheduleCallback(() => doWork('C', 300));145    scheduleCallback(() => doWork('D', 400));146    expect(flushWork(300)).toEqual(['A', 'B']);147    expect(flushWork(300)).toEqual(['C']);148    expect(flushWork(400)).toEqual(['D']);149  });150  it('flushes work until framesize reached', () => {151    scheduleCallback(() => doWork('A1_100', 100));152    scheduleCallback(() => doWork('A2_200', 200));153    scheduleCallback(() => doWork('B1_100', 100));154    scheduleCallback(() => doWork('B2_200', 200));155    scheduleCallback(() => doWork('C1_300', 300));156    scheduleCallback(() => doWork('C2_300', 300));157    scheduleCallback(() => doWork('D_3000', 3000));158    scheduleCallback(() => doWork('E1_300', 300));159    scheduleCallback(() => doWork('E2_200', 200));160    scheduleCallback(() => doWork('F1_200', 200));161    scheduleCallback(() => doWork('F2_200', 200));162    scheduleCallback(() => doWork('F3_300', 300));163    scheduleCallback(() => doWork('F4_500', 500));164    scheduleCallback(() => doWork('F5_200', 200));165    scheduleCallback(() => doWork('F6_20', 20));166    expect(Date.now()).toEqual(0);167    // No time left after A1_100 and A2_200 are run168    expect(flushWork(300)).toEqual(['A1_100', 'A2_200']);169    expect(Date.now()).toEqual(300);170    // B2_200 is started as there is still time left after B1_100171    expect(flushWork(101)).toEqual(['B1_100', 'B2_200']);172    expect(Date.now()).toEqual(600);173    // C1_300 is started as there is even a little frame time174    expect(flushWork(1)).toEqual(['C1_300']);175    expect(Date.now()).toEqual(900);176    // C2_300 is started even though there is no frame time177    expect(flushWork(0)).toEqual(['C2_300']);178    expect(Date.now()).toEqual(1200);179    // D_3000 is very slow, but won't affect next flushes (if no180    // timeouts happen)181    expect(flushWork(100)).toEqual(['D_3000']);182    expect(Date.now()).toEqual(4200);183    expect(flushWork(400)).toEqual(['E1_300', 'E2_200']);184    expect(Date.now()).toEqual(4700);185    // Default timeout is 5000, so during F2_200, work will timeout and are done186    // in reverse, including F2_200187    expect(flushWork(1000)).toEqual([188      'F1_200',189      'F2_200',190      'F3_300',191      'F4_500',192      'F5_200',193      'F6_20',194    ]);195    expect(Date.now()).toEqual(6120);196  });197  it('cancels work', () => {198    scheduleCallback(() => doWork('A', 100));199    const callbackHandleB = scheduleCallback(() => doWork('B', 200));200    scheduleCallback(() => doWork('C', 300));201    cancelCallback(callbackHandleB);202    expect(flushWork()).toEqual([203      'A',204      // B should have been cancelled205      'C',206    ]);207  });208  it('executes the highest priority callbacks first', () => {209    scheduleCallback(() => doWork('A', 100));210    scheduleCallback(() => doWork('B', 100));211    // Yield before B is flushed212    expect(flushWork(100)).toEqual(['A']);213    runWithPriority(UserBlockingPriority, () => {214      scheduleCallback(() => doWork('C', 100));215      scheduleCallback(() => doWork('D', 100));216    });217    // C and D should come first, because they are higher priority218    expect(flushWork()).toEqual(['C', 'D', 'B']);219  });220  it('expires work', () => {221    scheduleCallback(() => doWork('A', 100));222    runWithPriority(UserBlockingPriority, () => {223      scheduleCallback(() => doWork('B', 100));224    });225    scheduleCallback(() => doWork('C', 100));226    runWithPriority(UserBlockingPriority, () => {227      scheduleCallback(() => doWork('D', 100));228    });229    // Advance time, but not by enough to expire any work230    advanceTime(249);231    expect(clearYieldedValues()).toEqual([]);232    // Advance by just a bit more to expire the high pri callbacks233    advanceTime(1);234    expect(clearYieldedValues()).toEqual(['B', 'D']);235    // Expire the rest236    advanceTime(10000);237    expect(clearYieldedValues()).toEqual(['A', 'C']);238  });239  it('has a default expiration of ~5 seconds', () => {240    scheduleCallback(() => doWork('A', 100));241    advanceTime(4999);242    expect(clearYieldedValues()).toEqual([]);243    advanceTime(1);244    expect(clearYieldedValues()).toEqual(['A']);245  });246  it('continues working on same task after yielding', () => {247    scheduleCallback(() => doWork('A', 100));248    scheduleCallback(() => doWork('B', 100));249    const tasks = [['C1', 100], ['C2', 100], ['C3', 100]];250    const C = () => {251      while (tasks.length > 0) {252        doWork(...tasks.shift());253        if (shouldYield()) {254          yieldValue('Yield!');255          return C;256        }257      }258    };259    scheduleCallback(C);260    scheduleCallback(() => doWork('D', 100));261    scheduleCallback(() => doWork('E', 100));262    expect(flushWork(300)).toEqual(['A', 'B', 'C1', 'Yield!']);263    expect(flushWork()).toEqual(['C2', 'C3', 'D', 'E']);264  });265  it('continuation callbacks inherit the expiration of the previous callback', () => {266    const tasks = [['A', 125], ['B', 124], ['C', 100], ['D', 100]];267    const work = () => {268      while (tasks.length > 0) {269        doWork(...tasks.shift());270        if (shouldYield()) {271          yieldValue('Yield!');272          return work;273        }274      }275    };276    // Schedule a high priority callback277    runWithPriority(UserBlockingPriority, () => scheduleCallback(work));278    // Flush until just before the expiration time279    expect(flushWork(249)).toEqual(['A', 'B', 'Yield!']);280    // Advance time by just a bit more. This should expire all the remaining work.281    advanceTime(1);282    expect(clearYieldedValues()).toEqual(['C', 'D']);283  });284  it('nested callbacks inherit the priority of the currently executing callback', () => {285    runWithPriority(UserBlockingPriority, () => {286      scheduleCallback(() => {287        doWork('Parent callback', 100);288        scheduleCallback(() => {289          doWork('Nested callback', 100);290        });291      });292    });293    expect(flushWork(100)).toEqual(['Parent callback']);294    // The nested callback has user-blocking priority, so it should295    // expire quickly.296    advanceTime(250 + 100);297    expect(clearYieldedValues()).toEqual(['Nested callback']);298  });299  it('continuations are interrupted by higher priority work', () => {300    const tasks = [['A', 100], ['B', 100], ['C', 100], ['D', 100]];301    const work = () => {302      while (tasks.length > 0) {303        doWork(...tasks.shift());304        if (tasks.length > 0 && shouldYield()) {305          yieldValue('Yield!');306          return work;307        }308      }309    };310    scheduleCallback(work);311    expect(flushWork(100)).toEqual(['A', 'Yield!']);312    runWithPriority(UserBlockingPriority, () => {313      scheduleCallback(() => doWork('High pri', 100));314    });315    expect(flushWork()).toEqual(['High pri', 'B', 'C', 'D']);316  });317  it(318    'continutations are interrupted by higher priority work scheduled ' +319      'inside an executing callback',320    () => {321      const tasks = [['A', 100], ['B', 100], ['C', 100], ['D', 100]];322      const work = () => {323        while (tasks.length > 0) {324          const task = tasks.shift();325          doWork(...task);326          if (task[0] === 'B') {327            // Schedule high pri work from inside another callback328            yieldValue('Schedule high pri');329            runWithPriority(UserBlockingPriority, () =>330              scheduleCallback(() => doWork('High pri', 100)),331            );332          }333          if (tasks.length > 0 && shouldYield()) {334            yieldValue('Yield!');335            return work;336          }337        }338      };339      scheduleCallback(work);340      expect(flushWork()).toEqual([341        'A',342        'B',343        'Schedule high pri',344        // Even though there's time left in the frame, the low pri callback345        // should yield to the high pri callback346        'Yield!',347        'High pri',348        // Continue low pri work349        'C',350        'D',351      ]);352    },353  );354  it('immediate callbacks fire at the end of outermost event', () => {355    runWithPriority(ImmediatePriority, () => {356      scheduleCallback(() => yieldValue('A'));357      scheduleCallback(() => yieldValue('B'));358      // Nested event359      runWithPriority(ImmediatePriority, () => {360        scheduleCallback(() => yieldValue('C'));361        // Nothing should have fired yet362        expect(clearYieldedValues()).toEqual([]);363      });364      // Nothing should have fired yet365      expect(clearYieldedValues()).toEqual([]);366    });367    // The callbacks were called at the end of the outer event368    expect(clearYieldedValues()).toEqual(['A', 'B', 'C']);369  });370  it('wrapped callbacks have same signature as original callback', () => {371    const wrappedCallback = wrapCallback((...args) => ({args}));372    expect(wrappedCallback('a', 'b')).toEqual({args: ['a', 'b']});373  });374  it('wrapped callbacks inherit the current priority', () => {375    const wrappedCallback = wrapCallback(() => {376      scheduleCallback(() => {377        doWork('Normal', 100);378      });379    });380    const wrappedInteractiveCallback = runWithPriority(381      UserBlockingPriority,382      () =>383        wrapCallback(() => {384          scheduleCallback(() => {385            doWork('User-blocking', 100);386          });387        }),388    );389    // This should schedule a normal callback390    wrappedCallback();391    // This should schedule an user-blocking callback392    wrappedInteractiveCallback();393    advanceTime(249);394    expect(clearYieldedValues()).toEqual([]);395    advanceTime(1);396    expect(clearYieldedValues()).toEqual(['User-blocking']);397    advanceTime(10000);398    expect(clearYieldedValues()).toEqual(['Normal']);399  });400  it('wrapped callbacks inherit the current priority even when nested', () => {401    const wrappedCallback = wrapCallback(() => {402      scheduleCallback(() => {403        doWork('Normal', 100);404      });405    });406    const wrappedInteractiveCallback = runWithPriority(407      UserBlockingPriority,408      () =>409        wrapCallback(() => {410          scheduleCallback(() => {411            doWork('User-blocking', 100);412          });413        }),414    );415    runWithPriority(UserBlockingPriority, () => {416      // This should schedule a normal callback417      wrappedCallback();418      // This should schedule an user-blocking callback419      wrappedInteractiveCallback();420    });421    advanceTime(249);422    expect(clearYieldedValues()).toEqual([]);423    advanceTime(1);424    expect(clearYieldedValues()).toEqual(['User-blocking']);425    advanceTime(10000);426    expect(clearYieldedValues()).toEqual(['Normal']);427  });428  it('immediate callbacks fire at the end of callback', () => {429    const immediateCallback = runWithPriority(ImmediatePriority, () =>430      wrapCallback(() => {431        scheduleCallback(() => yieldValue('callback'));432      }),433    );434    immediateCallback();435    // The callback was called at the end of the outer event436    expect(clearYieldedValues()).toEqual(['callback']);437  });438  it("immediate callbacks fire even if there's an error", () => {439    expect(() => {440      runWithPriority(ImmediatePriority, () => {441        scheduleCallback(() => {442          yieldValue('A');443          throw new Error('Oops A');444        });445        scheduleCallback(() => {446          yieldValue('B');447        });448        scheduleCallback(() => {449          yieldValue('C');450          throw new Error('Oops C');451        });452      });453    }).toThrow('Oops A');454    expect(clearYieldedValues()).toEqual(['A']);455    // B and C flush in a subsequent event. That way, the second error is not456    // swallowed.457    expect(() => flushWork(0)).toThrow('Oops C');458    expect(clearYieldedValues()).toEqual(['B', 'C']);459  });460  it('exposes the current priority level', () => {461    yieldValue(getCurrentPriorityLevel());462    runWithPriority(ImmediatePriority, () => {463      yieldValue(getCurrentPriorityLevel());464      runWithPriority(NormalPriority, () => {465        yieldValue(getCurrentPriorityLevel());466        runWithPriority(UserBlockingPriority, () => {467          yieldValue(getCurrentPriorityLevel());468        });469      });470      yieldValue(getCurrentPriorityLevel());471    });...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.evaluate(() => {7    window.flushWork();8  });9  await page.screenshot({ path: 'screenshot.png' });10  await browser.close();11})();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.evaluate(() => {7    window.flushWork();8    window.flushWork(1000);9  });10  await browser.close();11})();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.screenshot({path: 'example.png'});7  await page.close();8  await context.close();9  await browser.close();10})();11    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)12    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)13    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)14    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)15    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)16    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)17    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)18    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)19    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)20    at CDPSession.send (C:\Users\test\playwright\playwright\lib\cdp.js:83:19)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  const element = await page.$('input[name="q"]');7  await element.type('Hello World');8  await page.flushWork();9  await browser.close();10})();11const {chromium} = require('playwright');12(async () => {13  const browser = await chromium.launch();14  const context = await browser.newContext();15  const page = await context.newPage();16  const element = await page.$('input[name="q"]');17  await element.type('Hello World');18  await page.flushWork();19  await browser.close();20})();21const {chromium} = require('playwright');22(async () => {23  const browser = await chromium.launch();24  const context = await browser.newContext();25  const page = await context.newPage();26  const element = await page.$('input[name="q"]');27  await element.type('Hello World');28  await page.flushWork();29  await browser.close();30})();31const {chromium} = require('playwright');32(async () => {33  const browser = await chromium.launch();34  const context = await browser.newContext();35  const page = await context.newPage();36  const element = await page.$('input[name="q"]');37  await element.type('Hello World');38  await page.flushWork();39  await browser.close();40})();41const {chromium} = require('playwright');42(async () => {43  const browser = await chromium.launch();44  const context = await browser.newContext();45  const page = await context.newPage();46  const element = await page.$('input[name="q"]');47  await element.type('Hello World');48  await page.flushWork();Using AI Code Generation
1const {chromium} = require("playwright");2(async () => {3  const browser = await chromium.launch({headless: false});4  const context = await browser.newContext();5  const page = await context.newPage();6  await page.waitForSelector("input[name=q]");7  await page.fill("input[name=q]", "playwright");8  await page.keyboard.press("Enter");9  await page.waitForSelector("text=Playwright");10  await page.click("text=Playwright");11  await page.waitForSelector("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API.");12  await page.click("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API.");13  await page.waitForSelector("text=GitHub - microsoft/playwright: Node library to automate Chromium, Firefox and WebKit with a single API");14  await page.click("text=GitHub - microsoft/playwright: Node library to automate Chromium, Firefox and WebKit with a single API");15  await page.waitForTimeout(5000);16  await page.screenshot({path: "screenshot.png"});17  await browser.close();18})();19const {chromium} = require("playwright");20(async () => {21  const browser = await chromium.launch({headless: false});22  const context = await browser.newContext();23  const page = await context.newPage();24  await page.waitForSelector("input[name=q]");25  await page.fill("input[name=q]", "playwright");26  await page.keyboard.press("Enter");27  await page.waitForSelector("text=Playwright");28  await page.click("text=Playwright");29  await page.waitForSelector("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API.");30  await page.click("text=Playwright is a Node library to automate Chromium, Firefox and WebKit with a single API.");31  await page.waitForSelector("text=GitHub - microsoft/playwright: Node library to automate Chromium, Firefox and WebKit with a single API");32  await page.click("text=GitHub - microsoft/playwright: Node library to automate Chromium, Firefox and WebKit with a single API");33  await page.waitForTimeout(5000);Using AI Code Generation
1const { flushWork } = require('playwright/lib/server/browserType');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  await flushWork(page);8  await browser.close();9})();10const { test, expect } = require('@playwright/test');11test('test', async ({ page }) => {12  await page.screenshot({ path: 'example.png' });13  expect(true).toBe(true);14});15const { test, expect } = require('@playwright/test');16test('test', async ({ page }) => {17  await page.screenshot({ path: 'example.png' });18  expect(true).toBe(true);19});20const { test, expect } = require('@playwright/test');21test('test', async ({ page }) => {22  await page.screenshot({ path: 'example.png' });23  expect(true).toBe(true);24});25const { test, expect } = require('@playwright/test');26test('test', async ({ page }) => {27  await page.screenshot({ path: 'example.png' });28  expect(true).toBe(true);29});30const { test, expect } = require('@playwright/test');31test('test', async ({ page }) => {32  await page.screenshot({ path: 'example.png' });33  expect(true).toBe(true);34});35const { test, expect } = require('@playwright/test');36test('test', async ({ page }) => {37  await page.screenshot({ path: 'example.png' });38  expect(true).toBe(true);39});40const { test, expect } = require('@playwright/test');41test('test', async ({ page }) => {42  await page.screenshot({ path: 'example.png' });Using AI Code Generation
1const playwright = require('playwright');2const { flushWork } = require('playwright/lib/server/supplements/recorder/recorderSupplement');3(async () => {4  const browser = await playwright.chromium.launch({ headless: false });5  const context = await browser.newContext();6  const page = await context.newPage();7  await page.click('input[name="q"]');8  await page.fill('input[name="q"]', 'Hello World!');9  await page.press('input[name="q"]', 'Enter');10  await flushWork(page);11  await browser.close();12})();Using AI Code Generation
1const { flushWork } = require('playwright/lib/server/frames');2const { flushWork } = require('playwright/lib/server/frames');3flushWork().then(() => console.log('done'));4const { flushWork } = require('playwright/lib/server/frames');5flushWork().then(() => console.log('done'));6Hi, I am trying to use the flushWork method of the playwright internal API to wait for the page to finish loading. I am using the playwright library directly (not the test runner) and I am trying to run this code:7const { flushWork } = require('playwright/lib/server/frames');8flushWork().then(() => console.log('done'));9Hi, I am trying to use the flushWork method of the playwright internal API to wait for the page to finish loading. I am using the playwright library directly (not the test runner) and I am trying to run this code:10const { flushWork } = require('playwright/lib/server/frames');11flushWork().then(() => console.log('done'));12Hi, I am trying to use the flushWork method of the playwright internal API to wait for the page to finish loading. I am using the playwright library directly (not the test runner) and I am trying to run this code:13const { flushWork } = require('playwright/lib/server/frames');14flushWork().then(() => console.log('done'));15I am getting an error saying that flushWork is not a function. I am using the latest version of playwright (1.11.1). I am running this code inUsing AI Code Generation
1const { flushWork } = require("playwright-core/lib/server/supplements/recorder/recorderApp");2flushWork();3const { flushWork } = require("playwright-core/lib/server/supplements/recorder/recorderApp");4flushWork();5const { flushWork } = require("playwright-core/lib/server/supplements/recorder/recorderApp");6flushWork();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!!
