Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pending-xhr-puppeteer

jtassin10.1kMIT3.0.0TypeScript support: included

Small tool for wait that all xhr are finished in pupeteer

puppeteer, xhr, pending, wait, nodejs, e2e

readme

Pending XHR Puppeteer

npm version Build Status

| Introduction | Installation | Usage | Contribute |

Introduction

Pending XHR Puppeteer is a tool that detect when there is xhr requests not yet finished. You can use it to have a xhr requests count or to wait for all xhr requests to be finished.

Installation

To install with yarn :

yarn add pending-xhr-puppeteer -D

To install with npm :

npm install pending-xhr-puppeteer --save-dev

Usage

wait for all xhr requests to be finished

const puppeteer = require('puppeteer');
const { PendingXHR } = require('pending-xhr-puppeteer');

const browser = await puppeteer.launch({
  headless: true,
  args,
});

const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
// Here all xhr requests are not finished
await pendingXHR.waitForAllXhrFinished();
// Here all xhr requests are finished

Get the number of pending xhr

const puppeteer = require('puppeteer');
const { PendingXHR } = require('pending-xhr-puppeteer');

const browser = await puppeteer.launch({
  headless: true,
  args,
});

const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
console.log(pendingXHR.pendingXhrCount());
// Display the number of xhr pending

Usage with Promise.race

If you need to wait xhrs but not longer than a specific time, You can race pending-xhr-puppeteer and setTimeout in a Promise.race.

const puppeteer = require('puppeteer');
const { PendingXHR } = require('pending-xhr-puppeteer');

const browser = await puppeteer.launch({
  headless: true,
  args,
});

const page = await browser.newPage();
const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
// We will wait max 1 seconde for xhrs
await Promise.race([
  pendingXHR.waitForAllXhrFinished(),
  new Promise(resolve => {
    setTimeout(resolve, 1000);
  }),
]);
console.log(pendingXHR.pendingXhrCount());
// May or may not have pending xhrs

Wait for all xhr triggered by all the events of the page

You can use this lib to wait for xhr triggered by any event from the UI (click, typing, ...).

Exemple :

const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
await page.click('.my-selector'); // This action will trigger some xhr
// Here all xhr requests triggered by the click are not finished
await pendingXHR.waitForAllXhrFinished();
// Here all xhr requests triggered by the click are finished
// You can then perform an other xhr producer event
await page.click('.my-selector2'); // This action will trigger some xhr
// You can rewait them
await pendingXHR.waitForAllXhrFinished();

This mode is usefull to test SPA, you d'ont have to recreate a new instance at each time. The request listeners will be deleted when you leave the page.

Wait for all xhr triggered by an event of the page

with waitOnceForAllXhrFinished you can wait until all the xhr are finished and them remove the listeners. This is usefull when waitForAllXhrFinished has a leaking behaviour for you.

Exemple :

const pendingXHR = new PendingXHR(page);
await page.goto(`http://page-with-xhr`);
await page.click('.my-selector'); // This action will trigger some xhr
// Here all xhr requests triggered by the click are not finished
await pendingXHR.waitOnceForAllXhrFinished();
// Here all xhr requests triggered by the click are finished
// All pendingXHR listeners are remove here too

Contribute

git clone https://github.com/jtassin/pending-xhr-puppeteer.git
cd pending-xhr-puppeteer
yarn
yarn test

Merge requests and issues are welcome.

changelog

2.3.4 (2025-04-05)

2.3.3 (2020-12-13)

2.3.2 (2020-06-16)

Bug Fixes

  • expose only interesting files (a083778)

2.3.1 (2020-06-16)

Bug Fixes

  • fix exposition of waitOnceForAllXhrFinished (b26cbf1)

2.3.0 (2020-06-16)

Features

  • expose removeListeners function (8877578)

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

v2.0.0

30 March 2019

  • Typescript #21
  • doc: add example for an event triggered xhr #11
  • Update dependency eslint-plugin-jest to v22.4.1 #14
  • Update dependency puppeteer to v1.13.0 #17
  • Update dependency jest to v24 #20
  • Update dependency eslint-config-prettier to v3.6.0 #13
  • Update dependency eslint to v5.15.3 #9
  • Pin dependencies #8
  • Add renovate.json #7
  • chore: releases with release-it 90a3ea3
  • chore(typescript): switching codebase to ts 8e66474
  • chore: add generated changelog a6b8638

v1.0.17

5 January 2019

  • add example with Promise.race #4
  • release 1.0.17 0585d7b

v1.0.16

5 January 2019

  • fixes #1 - handling multiple xhrs #3
  • release 1.0.16 437a7a2

v1.0.15

2 January 2019

v1.0.12

27 May 2018

v1.0.11

16 May 2018

v1.0.10

16 May 2018

  • back to pending-puppeteer-xhr 4a808dc

v1.0.9

16 May 2018

  • pending-xhr-puppeteer is working on npm :tada: 84d5f5f
  • adding a new package name because of npm (pending-xhr-puppeteer will be still up to date) 18c371b

v1.0.8

14 May 2018

v1.0.7

14 May 2018

v1.0.6

11 May 2018

  • still not visible on npmjs :/ f9a78c3

v1.0.5

11 May 2018

  • packahe not visible on npmjs b950cb3

v1.0.4

11 May 2018

v1.0.3

11 May 2018

v1.0.2

10 May 2018

v1.0.1

10 May 2018