mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
244 lines
6.8 KiB
Markdown
244 lines
6.8 KiB
Markdown
![]() |
---
|
||
|
id: browser-contexts
|
||
|
title: "Browser Contexts"
|
||
|
---
|
||
|
|
||
|
<!-- TOC -->
|
||
|
|
||
|
## 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](./intro.md), this happens out of the
|
||
|
box for each test. Otherwise, you can create browser contexts manually:
|
||
|
|
||
|
```js
|
||
|
const browser = await chromium.launch();
|
||
|
const context = await browser.newContext();
|
||
|
const page = await context.newPage();
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
Browser browser = chromium.launch();
|
||
|
BrowserContext context = browser.newContext();
|
||
|
Page page = context.newPage();
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
browser = await playwright.chromium.launch()
|
||
|
context = await browser.new_context()
|
||
|
page = await context.new_page()
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
browser = playwright.chromium.launch()
|
||
|
context = browser.new_context()
|
||
|
page = context.new_page()
|
||
|
```
|
||
|
|
||
|
```csharp
|
||
|
await using var browser = playwright.Chromium.LaunchAsync();
|
||
|
var context = await browser.NewContextAsync();
|
||
|
var page = await context.NewPageAsync();
|
||
|
```
|
||
|
|
||
|
Browser contexts can also be used to emulate multi-page scenarios involving
|
||
|
mobile devices, permissions, locale and color scheme.
|
||
|
|
||
|
```js
|
||
|
const { devices } = require('playwright');
|
||
|
const iPhone = devices['iPhone 11 Pro'];
|
||
|
|
||
|
const context = await browser.newContext({
|
||
|
...iPhone,
|
||
|
permissions: ['geolocation'],
|
||
|
geolocation: { latitude: 52.52, longitude: 13.39},
|
||
|
colorScheme: 'dark',
|
||
|
locale: 'de-DE'
|
||
|
});
|
||
|
const page = await context.newPage();
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
// FIXME
|
||
|
import com.microsoft.playwright.*;
|
||
|
|
||
|
public class Example {
|
||
|
public static void main(String[] args) {
|
||
|
try (Playwright playwright = Playwright.create()) {
|
||
|
BrowserType devices = playwright.devices();
|
||
|
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
|
||
|
.setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1")
|
||
|
.setViewportSize(375, 812)
|
||
|
.setDeviceScaleFactor(3)
|
||
|
.setIsMobile(true)
|
||
|
.setHasTouch(true)
|
||
|
.setPermissions(Arrays.asList("geolocation"))
|
||
|
.setGeolocation(52.52, 13.39)
|
||
|
.setColorScheme(ColorScheme.DARK)
|
||
|
.setLocale("de-DE"));
|
||
|
Page page = context.newPage();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
import asyncio
|
||
|
from playwright.async_api import async_playwright
|
||
|
|
||
|
async def main():
|
||
|
async with async_playwright() as p:
|
||
|
iphone_11 = p.devices['iPhone 11 Pro']
|
||
|
browser = await p.chromium.launch()
|
||
|
context = await browser.new_context(
|
||
|
**iphone_11,
|
||
|
locale='de-DE',
|
||
|
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
|
||
|
permissions=['geolocation'],
|
||
|
color_scheme='dark',
|
||
|
)
|
||
|
page = await browser.new_page()
|
||
|
await browser.close()
|
||
|
|
||
|
asyncio.run(main())
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
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(
|
||
|
**iphone_11,
|
||
|
locale='de-DE',
|
||
|
geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 },
|
||
|
permissions=['geolocation']
|
||
|
)
|
||
|
page = context.new_page()
|
||
|
browser.close()
|
||
|
```
|
||
|
|
||
|
```csharp
|
||
|
using Microsoft.Playwright;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
class PlaywrightExample
|
||
|
{
|
||
|
public static async Task Main()
|
||
|
{
|
||
|
using var playwright = await Playwright.CreateAsync();
|
||
|
await using var browser = await playwright.Webkit.LaunchAsync();
|
||
|
var options = new BrowserNewContextOptions(playwright.Devices["iPhone 11 Pro"])
|
||
|
{
|
||
|
Geolocation = new() { Longitude = 12.492507f, Latitude = 41.889938f },
|
||
|
Permissions = new[] { "geolocation" },
|
||
|
Locale = "de-DE"
|
||
|
};
|
||
|
|
||
|
await using var context = await browser.NewContextAsync(options);
|
||
|
var page = await browser.NewPageAsync();
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Multiple contexts
|
||
|
|
||
|
[Browser contexts](./browser-contexts.md) 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.
|
||
|
|
||
|
```js
|
||
|
const { chromium } = require('playwright');
|
||
|
|
||
|
// Create a Chromium browser instance
|
||
|
const browser = await chromium.launch();
|
||
|
|
||
|
// Create two isolated browser contexts
|
||
|
const userContext = await browser.newContext();
|
||
|
const adminContext = await browser.newContext();
|
||
|
|
||
|
// Create pages and interact with contexts independently
|
||
|
```
|
||
|
|
||
|
```java
|
||
|
import com.microsoft.playwright.*;
|
||
|
|
||
|
public class Example {
|
||
|
public static void main(String[] args) {
|
||
|
try (Playwright playwright = Playwright.create()) {
|
||
|
BrowserType chromium = playwright.chromium();
|
||
|
// Create a Chromium browser instance
|
||
|
Browser browser = chromium.launch();
|
||
|
// Create two isolated browser contexts
|
||
|
BrowserContext userContext = browser.newContext();
|
||
|
BrowserContext adminContext = browser.newContext();
|
||
|
// Create pages and interact with contexts independently
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
```python async
|
||
|
import asyncio
|
||
|
from playwright.async_api import async_playwright
|
||
|
|
||
|
async def run(playwright):
|
||
|
# create a chromium browser instance
|
||
|
chromium = playwright.chromium
|
||
|
browser = await chromium.launch()
|
||
|
|
||
|
# create two isolated browser contexts
|
||
|
user_context = await browser.new_context()
|
||
|
admin_context = await browser.new_context()
|
||
|
|
||
|
# create pages and interact with contexts independently
|
||
|
|
||
|
async def main():
|
||
|
async with async_playwright() as playwright:
|
||
|
await run(playwright)
|
||
|
asyncio.run(main())
|
||
|
```
|
||
|
|
||
|
```python sync
|
||
|
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:
|
||
|
run(playwright)
|
||
|
```
|
||
|
|
||
|
```csharp
|
||
|
using Microsoft.Playwright;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
|
class Program
|
||
|
{
|
||
|
public static async Task Main()
|
||
|
{
|
||
|
using var playwright = await Playwright.CreateAsync();
|
||
|
// Create a Chromium browser instance
|
||
|
await using var browser = await playwright.Chromium.LaunchAsync();
|
||
|
await using var userContext = await browser.NewContextAsync();
|
||
|
await using var adminContext = await browser.NewContextAsync();
|
||
|
// Create pages and interact with contexts independently.
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### API reference
|
||
|
- [BrowserContext]
|
||
|
- [`method: Browser.newContext`]
|
||
|
- [`method: BrowserContext.addCookies`]
|