UNPKG

web-snaps

Version:

Browser automation with automatic snapshotting.

36 lines (35 loc) 1.13 kB
import { awaitAllPromisesInObject, randomString } from '@augment-vir/common'; import { initBrowser } from './init-browser.js'; /** * Setup a browser instance for use with the web-snaps package. * * @category Internal */ export async function setupBrowser({ context: rawContext, userDataDirPath, options, }) { const storeKey = [ 'data-store', randomString(32), ].join('-'); const { browserResult, context } = await awaitAllPromisesInObject({ browserResult: initBrowser({ userDataDirPath, storeKey, options }), context: rawContext, }); return { storeKey, ...browserResult, context }; } /** * Run a callback with a browser instance. The browser will be cleanly closed when the callback is * finished (or fails). * * @category Internal */ export async function withBrowserContext(params, /** Calls this callback and then automatically closes the browser afterwards. */ callback) { const browserParams = await setupBrowser(params); try { return await callback(browserParams); } finally { await browserParams.browserContext.close(); } }