Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bbchriscesar/9d92b2cabef8854525e6e27687bd84c0 to your computer and use it in GitHub Desktop.
Save bbchriscesar/9d92b2cabef8854525e6e27687bd84c0 to your computer and use it in GitHub Desktop.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
// Enable network request interception
await context.route('**/*', (route, request) => {
// Intercept the network request
console.log(`Intercepted: ${request.url()}`);
// Abort the request
route.abort();
});
const page = await context.newPage();
// Navigate to a website that requires authentication
await page.goto('https://example.com');
// Perform login and authentication steps here
// ...
// Persist the authentication state
await context.persistStorage('auth.json');
// Close the browser and context
await browser.close();
// Re-launch the browser and context
const browser2 = await chromium.launch();
const context2 = await browser2.newContext();
// Restore the previous authentication state
await context2.clearStorageState();
await context2.addCookies(await context.cookies());
await context2.storageState({ path: 'auth.json' });
const page2 = await context2.newPage();
// Enable network request interception
await context2.route('**/*', (route, request) => {
// Intercept the network request
console.log(`Intercepted: ${request.url()}`);
// Abort the request
route.abort();
});
// Continue interacting with the website while reusing the authentication state
await page2.goto('https://example.com/profile');
// Close the browser and context
await browser2.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment