web-snaps
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

web-snaps

A simple library for browser automation with snapshot saving after each stop of the automation script. Closed Shadow DOM piercing is also enabled.

Uses Playwright under the hood.

Install

npm i web-snaps

Usage

Here's an example script to step you through the primary way this package is meant to be used:

import {defineSnapSuite} from 'web-snaps';

/** # 1. Define Context (if any) and Output (if any) types. */

type MyContext = {
    credentialsClient: {
        getCredentials(key: string): Promise<Record<string, string>>;
    };
};

type MyOutput = {
    parsedData: Record<string, string>;
};

/** # 2. Define a SnapSuite. */

const {defineWebFlow, runWebFlows} = defineSnapSuite<MyContext, MyOutput>(
    'my/dir/to/save/snapshots',
);

/** # 3. Define your WebFlows. */

const myWebFlows = [
    defineWebFlow({
        flowKey: 'my-flow-1',
        startUrl: 'https://example.com',
        phases: [
            {
                name: 'initial load',
                async run({page}) {
                    await page.getByText('example domain').waitFor({state: 'visible'});
                },
            },
            {
                name: 'iana site',
                /** Optionally sanitize the snapshot before it is saved. */
                sanitizeSnapshot(params) {
                    return '';
                },
                async run({page, context}) {
                    await page.getByText('more information...').click();
                    await page.getByText('example domains').first().waitFor({state: 'visible'});
                    return {
                        output: {
                            parsedData: {},
                        },
                    };
                },
            },
        ],
    }),
    // ...etc.
] as const;

/** # 4. Run your WebFlows. */

await runWebFlows({
    /** Pass in your actual context here. */
    context: {} as MyContext,
    webFlows: myWebFlows,
    /** The directory of your browser's user data. This is required. */
    userDataDirPath: '',
    /** Pass in extra options here. */
    options: {},
});

Package Sidebar

Install

npm i web-snaps

Weekly Downloads

501

Version

1.3.1

License

(MIT or CC0 1.0)

Unpacked Size

58 kB

Total Files

30

Last publish

Collaborators

  • electrovir