Skip to main content
Version: 1.17

Browser Contexts

Browser context

A BrowserContext is an isolated incognito-alike session within a browser instance. Browser contexts are fast and cheap to create. We recommend running each test scenario in its own new Browser context, so that the browser state is isolated between the tests. If you are using Playwright Test, this happens out of the box for each test. Otherwise, you can create browser contexts manually:

browser = playwright.chromium.launch()
context = browser.new_context()
page = context.new_page()

Browser contexts can also be used to emulate multi-page scenarios involving mobile devices, permissions, locale and color scheme.

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
iphone_11 = p.devices['iPhone 11 Pro']
browser = p.webkit.launch(headless=False)
context = browser.new_context(
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
page = context.new_page()

Multiple contexts

Browser contexts are isolated environments on a single browser instance. Playwright can create multiple browser contexts within a single scenario. This is useful when you want to test for multi-user functionality, like chat.

from playwright.sync_api import sync_playwright

def run(playwright):
# create a chromium browser instance
chromium = playwright.chromium
browser = chromium.launch()

# create two isolated browser contexts
user_context = browser.new_context()
admin_context = browser.new_context()

# create pages and interact with contexts independently

with sync_playwright() as playwright:

API reference