question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support browser contexts to launch different sessions

See original GitHub issue

Support browser contexts (Target.createBrowserContext) so to avoid launching multiple instances if one wants a pristine session. See proposal https://github.com/GoogleChrome/puppeteer/issues/66 and original discussion at https://github.com/cyrus-and/chrome-remote-interface/issues/118.

A viable user scenario might be testing several users logged in simultaneously into the service. We might expose browser contexts as a string literal option to the browser.newPage:

browser.newPage(); // creates a new page in a default browser context
browser.newPage({ context: 'default' }); // same as previous call
browser.newPage({ context: 'another-context' }); // creates a page in another browser context

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:197
  • Comments:79 (17 by maintainers)

github_iconTop GitHub Comments

40reactions
barbolocommented, Nov 7, 2017

Ok, I got this working. Thanks for the tip, @ks07 .

const puppeteer = require('puppeteer');
const Page = require('puppeteer/lib/Page');

async function newPageWithNewContext(browser) {
  const {browserContextId} = await browser._connection.send('Target.createBrowserContext');
  const {targetId} = await browser._connection.send('Target.createTarget', {url: 'about:blank', browserContextId});
  const client = await browser._connection.createSession(targetId);
  const page = await Page.create(client, browser._ignoreHTTPSErrors, browser._screenshotTaskQueue);
  page.browserContextId = browserContextId;
  return page;
}

async function closePage(browser, page) {
  if (page.browserContextId != undefined) {
    await browser._connection.send('Target.disposeBrowserContext', {browserContextId: page.browserContextId});
  }
  await page.close();
}

(async () => {
  const browser = await puppeteer.launch();
  const page = await newPageWithNewContext(browser);
  await page.goto('https://example.com');
  console.log(await page.cookies());

  await closePage(browser, page);
  await browser.close();
})();

UPDATE (2017-11-04): I’ve updated the example with a suggestion on how to close the page and its context.

12reactions
SirNeuralcommented, Jan 14, 2018

Any updates to this issue now that pptr has hit v1.0.0? Supporting page contexts for isolations of sessions without having to make separate browser instances seems like a feature that I’d really like to see implemented!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is a BrowserContext in the Node library Puppeteer?
BrowserContexts provide a way to operate multiple independent browser sessions. When a browser is launched, it has a single BrowserContext used ...
Read more >
BrowserContext | Playwright - CukeTest
BrowserContexts provide a way to operate multiple independent browser sessions. If a page opens another page, e.g. with a window.open call, the popup...
Read more >
Observations running 2 million headless sessions
We're excited to announce that we've recently just crossed over 2 million sessions served! That's millions of screenshots generated, ...
Read more >
Multi-page scenarios | Playwright
Playwright can create multiple browser contexts within a single scenario. This is useful when you want to test for multi-user functionality, like chat....
Read more >
Creating a launch entry - IBM
In the Launch in Context application, click New Launch Entry. ... console for an operational management product that you want to open in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found