Skip to content

tpkn/pptr-spoofer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Puppeteer Spoofer npm Package

Replace page requests in Puppeteer

Let's say you are testing some page. And that page loads tons of heavy libs from slow remote server each time. Tedious process, huh? With this module you can respond with content of that libraries, loaded from your local drive.

In case you have the CORS error, disable web security (at your own risk):

puppeteer.launch({ args: [ '--disable-web-security' ] });

Installation

npm install pptr-spoofer

API

PptrSpoofer(req, rules)

req

Type: Object
Page request object

rules

Type: Array

[
   {
      rule: '',
      body: '',
      status: 200,
      contentType: 'text/plane'
   }
]
  • rule String or RegExp of the request url that should be replaced
  • body String or Buffer of the response body content, Default: ''
  • status Number, Default: 200
  • contentType String, Default: 'text/plane'

Usage

const puppeteer = require('puppeteer');
const spoofer = require('pptr-spoofer');

let browser = await puppeteer.launch({ headless: 0, devtools: 1, args: ['--disable-web-security'] });
let pages = await browser.pages();
let page = pages[0];

await page.setRequestInterception(true);
await page.bringToFront();

page.on('request', req => {
   spoofer(req, [
      {
         rule: 'https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.8.2/pixi.js', 
         body: fs.readFileSync('./libs/pixi.js'),
         status: 200,
         contentType: 'text/plain'
      }
   ]);
});

await page.goto('http://localhost:12345/test', { waitUntil: 'networkidle2', timeout: 45000 });

About

Replace page requests in Puppeteer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published