Socket
Book a DemoInstallSign in
Socket

playwright-har

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

playwright-har

Generate HAR files from Playwright test execution

0.2.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

playwright-har

npm

playwright-har is capturing HAR files from browser network traffic and saves them to simplify debugging of failed tests.

Credits

This is a port of puppeteer-har that was adjusted to work with Playwright.

Install

npm i --save playwright-har

Usage

Quick start

import { chromium } from 'playwright'
import { PlaywrightHar } from 'playwright-har'

(async () => {
    const browser = await chromium.launch();
    const context = await browser.newContext();
    const page = await context.newPage();

    const playwrightHar = new PlaywrightHar(page);
    await playwrightHar.start();

    await page.goto('http://whatsmyuseragent.org/');
    // ... other actions ...

    await playwrightHar.stop('./example.har');
    await browser.close();
})();

Integration with jest-playwright preset

In CustomEnvironment.js :

const PlaywrightEnvironment = require('jest-playwright-preset/lib/PlaywrightEnvironment').default
const { PlaywrightHar } = require('playwright-har');

class CustomEnvironment extends PlaywrightEnvironment {
    
    constructor(config, context) {
        super(config, context);
        this.playwrightHar;
    }

    async handleTestEvent(event) {

        if (event.name == 'test_start') {
            if (this.global.browserName === 'chromium') {
                this.playwrightHar = new PlaywrightHar(this.global.page);
                await this.playwrightHar.start();
            }
        }

        if (event.name == 'test_done') {
            if (this.global.browserName === 'chromium') {
                const parentName = event.test.parent.name.replace(/\W/g, '-');
                const specName = event.test.name.replace(/\W/g, '-');
                await this.playwrightHar.stop(`./${parentName}_${specName}.har`);
            }
        }
    }
}

module.exports = CustomEnvironment;

This setup will create PlaywrightHar instance for each test statement in describe statement in spec file. Browser network traffic will be collected from this step execution and save it in .har file with name corresponding to describe name followed by test name.

Additional info

  • HAR files collection works only on chromium browser
  • stop() has an optional argument path - when specified, generated HAR file will be saved into provided path, otherwise it will be returned as an object

Keywords

playwright

FAQs

Package last updated on 13 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.