2021-01-01 15:17:27 -08:00
---
id: emulation
2021-01-16 14:37:13 -08:00
title: "Emulation"
2021-01-01 15:17:27 -08:00
---
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
With Playwright you can test your app on any browser as well as emulate a real device such as a mobile phone or tablet. Simply configure the devices you would like to emulate and Playwright will simulate the browser behavior such as `"userAgent"` , `"screenSize"` , `"viewport"` and if it `"hasTouch"` enabled. You can also emulate the `"geolocation"` , `"locale"` and `"timezone"` for all tests or for a specific test as well as set the `"permissions"` to show notifications or change the `"colorScheme"` .
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
## Devices
* langs: js, csharp, python
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
Playwright comes with a [registry of device parameters ](https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json ) using [`property: Playwright.devices` ] for selected desktop, tablet and mobile devices. It can be used to simulate browser behavior for a specific device such as user agent, screen size, viewport and if it has touch enabled. All tests will run with the specified device parameters.
2022-08-08 17:05:56 +02:00
2023-03-06 16:43:32 +01:00
```js tab=js-js
// playwright.config.ts/js
2023-01-12 13:12:02 -08:00
import { defineConfig, devices } from '@playwright/test '; // import devices
2020-12-30 18:04:51 -08:00
2023-01-12 13:12:02 -08:00
export default defineConfig({
2022-09-08 18:57:20 +02:00
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},
{
name: 'Mobile Safari',
use: {
2023-03-07 08:58:49 +01:00
...devices['iPhone 13'],
2022-09-08 18:57:20 +02:00
},
},
],
2023-01-12 13:12:02 -08:00
});
2022-09-08 18:57:20 +02:00
```
```js tab=js-library
2020-12-30 18:04:51 -08:00
const { chromium, devices } = require('playwright');
const browser = await chromium.launch();
2023-03-07 08:58:49 +01:00
const iphone13 = devices['iPhone 13'];
2020-12-30 18:04:51 -08:00
const context = await browser.newContext({
2023-03-07 08:58:49 +01:00
...iphone13,
2020-12-30 18:04:51 -08:00
});
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
import asyncio
2021-01-11 17:04:24 -08:00
from playwright.async_api import async_playwright
2021-01-08 17:39:33 +01:00
2021-01-14 15:01:39 -08:00
async def run(playwright):
2023-03-07 08:58:49 +01:00
iphone_13 = playwright.devices['iPhone 13']
2021-01-14 15:01:39 -08:00
browser = await playwright.webkit.launch(headless=False)
context = await browser.new_context(
2023-03-07 08:58:49 +01:00
**iphone_13,
2021-01-14 15:01:39 -08:00
)
2021-01-08 17:39:33 +01:00
2021-01-14 15:01:39 -08:00
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-11 17:04:24 -08:00
from playwright.sync_api import sync_playwright
2021-01-08 17:39:33 +01:00
2021-01-14 15:01:39 -08:00
def run(playwright):
2023-03-07 08:58:49 +01:00
iphone_13 = playwright.devices['iPhone 13']
2021-01-14 15:01:39 -08:00
browser = playwright.webkit.launch(headless=False)
2021-01-08 17:39:33 +01:00
context = browser.new_context(
2023-03-07 08:58:49 +01:00
**iphone_13,
2021-01-08 17:39:33 +01:00
)
2021-01-14 15:01:39 -08:00
with sync_playwright() as playwright:
run(playwright)
2021-01-08 17:39:33 +01:00
```
2021-05-20 04:53:12 +02:00
```csharp
using Microsoft.Playwright;
using System.Threading.Tasks;
2021-05-22 07:55:53 -07:00
class Program
2021-05-20 04:53:12 +02:00
{
2021-05-22 07:55:53 -07:00
public static async Task Main()
{
using var playwright = await Playwright.CreateAsync();
2022-07-13 11:50:18 +02:00
await using var browser = await playwright.Chromium.LaunchAsync(new()
2021-05-22 07:55:53 -07:00
{
Headless: False
});
2023-03-07 08:58:49 +01:00
var iphone13 = playwright.Devices["iPhone 13"];
await using var context = await browser.NewContextAsync(iphone13);
2021-05-22 07:55:53 -07:00
}
2021-05-20 04:53:12 +02:00
}
```
2023-03-07 08:58:49 +01:00
< img width = "458" alt = "playwright.dev website emulated for iPhone 13" src = "https://user-images.githubusercontent.com/13063165/220411073-76fe59f9-9a2d-463d-8e30-c19a7deca133.png" / >
2022-09-08 18:57:20 +02:00
## Viewport
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
The viewport is included in the device but you can override it for some tests with [`method: Page.setViewportSize` ].
2020-12-30 18:04:51 -08:00
2023-03-07 08:58:49 +01:00
2023-03-06 16:43:32 +01:00
```js
// playwright.config.ts/js
import { defineConfig } from '@playwright/test ';
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
export default defineConfig({
use: {
// Viewport used for all pages in the context.
viewport: { width: 1280, height: 720 },
},
2023-03-07 08:58:49 +01:00
2020-12-30 18:04:51 -08:00
});
```
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2021-03-01 09:18:44 -08:00
2023-03-07 08:58:49 +01:00
test.use({
viewport: { width: 1600, height: 1200 },
2022-09-08 18:57:20 +02:00
});
2021-01-08 17:39:33 +01:00
2023-03-07 08:58:49 +01:00
test('my test', async ({ page }) => {
2022-09-08 18:57:20 +02:00
// ...
});
2021-05-20 04:53:12 +02:00
```
2022-09-08 18:57:20 +02:00
```js tab=js-library
2020-12-30 18:04:51 -08:00
// Create context with given viewport
const context = await browser.newContext({
viewport: { width: 1280, height: 1024 }
});
// Resize viewport for individual page
await page.setViewportSize({ width: 1600, height: 1200 });
// Emulate high-DPI
const context = await browser.newContext({
viewport: { width: 2560, height: 1440 },
deviceScaleFactor: 2,
});
```
2022-09-08 18:57:20 +02:00
The same works inside a describe block.
2023-03-06 16:43:32 +01:00
```js
// example.spec.ts/js
2022-09-08 18:57:20 +02:00
import { test, expect } from '@playwright/test ';
2023-03-07 08:58:49 +01:00
test.describe('specific viewport block', () => {
test.use({ viewport: { width: 1600, height: 1200 } });
2022-09-08 18:57:20 +02:00
2023-03-07 08:58:49 +01:00
test('my test', async ({ page }) => {
2022-09-08 18:57:20 +02:00
// ...
});
});
```
2021-03-01 09:18:44 -08:00
```java
// Create context with given viewport
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setViewportSize(1280, 1024));
2021-03-01 09:18:44 -08:00
// Resize viewport for individual page
page.setViewportSize(1600, 1200);
// Emulate high-DPI
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setViewportSize(2560, 1440)
.setDeviceScaleFactor(2);
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Create context with given viewport
context = await browser.new_context(
viewport={ 'width': 1280, 'height': 1024 }
)
# Resize viewport for individual page
2022-02-22 20:15:46 +01:00
await page.set_viewport_size({"width": 1600, "height": 1200})
2021-01-08 17:39:33 +01:00
# Emulate high-DPI
context = await browser.new_context(
viewport={ 'width': 2560, 'height': 1440 },
device_scale_factor=2,
)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Create context with given viewport
context = browser.new_context(
viewport={ 'width': 1280, 'height': 1024 }
)
# Resize viewport for individual page
2022-02-22 20:15:46 +01:00
await page.set_viewport_size({"width": 1600, "height": 1200})
2021-01-08 17:39:33 +01:00
# Emulate high-DPI
context = browser.new_context(
viewport={ 'width': 2560, 'height': 1440 },
device_scale_factor=2,
```
2021-05-20 04:53:12 +02:00
```csharp
// Create context with given viewport
2022-07-13 11:50:18 +02:00
await using var context = await browser.NewContextAsync(new()
2021-05-20 08:20:21 -07:00
{
ViewportSize = new ViewportSize() { Width = 1280, Height = 1024 }
});
2021-05-20 04:53:12 +02:00
// Resize viewport for individual page
await page.SetViewportSizeAsync(1600, 1200);
// Emulate high-DPI
2022-07-13 11:50:18 +02:00
await using var context = await browser.NewContextAsync(new()
2021-05-20 08:20:21 -07:00
{
ViewportSize = new ViewportSize() { Width = 2560, Height = 1440 },
DeviceScaleFactor = 2
});
2021-05-20 04:53:12 +02:00
```
2023-03-07 08:58:49 +01:00
< img width = "1714" alt = "website with set viewport" src = "https://user-images.githubusercontent.com/13063165/220405141-a7446ee5-aa1e-42af-b8fc-7a281f901dc2.png" / >
2022-09-08 18:57:20 +02:00
## Locale & Timezone
Emulate the user Locale and Timezone which can be set globally for all tests in the config and then overridden for particular tests.
2021-05-20 04:53:12 +02:00
2023-03-06 16:43:32 +01:00
```js
// playwright.config.ts/js
import { defineConfig } from '@playwright/test ';
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
export default defineConfig({
use: {
// Emulates the user locale.
locale: 'en-GB',
2023-03-07 08:58:49 +01:00
2023-03-06 16:43:32 +01:00
// Emulates the user timezone.
timezoneId: 'Europe/Paris',
},
2022-09-08 18:57:20 +02:00
});
```
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
test.use({
locale: 'de-DE',
timezoneId: 'Europe/Berlin',
});
test('my test for de lang in Berlin timezone', async ({ page }) => {
2023-03-07 08:58:49 +01:00
await page.goto('https://www.bing.com');
2022-09-08 18:57:20 +02:00
// ...
});
```
```js tab=js-library
2020-12-30 18:04:51 -08:00
const context = await browser.newContext({
locale: 'de-DE',
timezoneId: 'Europe/Berlin',
});
```
2021-03-01 09:18:44 -08:00
```java
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setLocale("de-DE")
.setTimezoneId("Europe/Berlin"));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
context = await browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
context = browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)
```
2021-05-20 04:53:12 +02:00
```csharp
2022-07-13 11:50:18 +02:00
await using var context = await browser.NewContextAsync(new()
2021-05-20 08:20:21 -07:00
{
Locale = "de-DE",
TimezoneId = "Europe/Berlin"
});
2021-05-20 04:53:12 +02:00
```
2023-03-07 08:58:49 +01:00
< img width = "1394" alt = "Bing in german lang and timezone" src = "https://user-images.githubusercontent.com/13063165/220416571-ccc96ab1-44bb-4579-8430-64502fc24a15.png" / >
2022-09-08 18:57:20 +02:00
## Permissions
2021-05-20 04:53:12 +02:00
2022-09-08 18:57:20 +02:00
Allow app to show system notifications.
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// playwright.config.ts/js
import { defineConfig } from '@playwright/test ';
2021-01-08 17:39:33 +01:00
2023-01-12 13:12:02 -08:00
export default defineConfig({
2022-09-08 18:57:20 +02:00
use: {
2023-03-06 16:43:32 +01:00
// Grants specified permissions to the browser context.
permissions: 'notifications',
2022-09-08 18:57:20 +02:00
},
2023-01-12 13:12:02 -08:00
});
2022-09-08 18:57:20 +02:00
```
```js tab=js-library
2020-12-30 18:04:51 -08:00
const context = await browser.newContext({
permissions: ['notifications'],
});
```
2021-03-01 09:18:44 -08:00
```java
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setPermissions(Arrays.asList("notifications"));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
context = await browser.new_context(
permissions=['notifications'],
)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
context = browser.new_context(
permissions=['notifications'],
)
```
2022-09-08 18:57:20 +02:00
Allow notifications for a specific domain.
2021-01-08 17:39:33 +01:00
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
2022-12-23 10:57:29 +01:00
import { test } from '@playwright/test ';
test.beforeEach(async ({ context }) => {
// Runs before each test and signs in each page.
await context.grantPermissions(['notifications'], { origin: 'https://skype.com' });
});
test('first', async ({ page }) => {
// page has notifications permission for https://skype.com.
});
2022-09-08 18:57:20 +02:00
```
```js tab=js-library
2022-12-23 10:57:29 +01:00
await context.grantPermissions(['notifications'], { origin: 'https://skype.com' });
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
context.grantPermissions(Arrays.asList("notifications"),
2021-03-05 13:50:34 -08:00
new BrowserContext.GrantPermissionsOptions().setOrigin("https://skype.com"));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
await context.grant_permissions(['notifications'], origin='https://skype.com')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
context.grant_permissions(['notifications'], origin='https://skype.com')
```
2021-05-20 04:53:12 +02:00
```csharp
2021-10-06 16:07:31 -03:00
await context.GrantPermissionsAsync(new[] { "notifications" }, origin: "https://skype.com");
2021-05-20 04:53:12 +02:00
```
2022-09-08 18:57:20 +02:00
Revoke all permissions with [`method: BrowserContext.clearPermissions` ].
2021-01-08 17:39:33 +01:00
2020-12-30 18:04:51 -08:00
```js
2022-09-08 18:57:20 +02:00
// Library
2020-12-30 18:04:51 -08:00
await context.clearPermissions();
```
2021-03-01 09:18:44 -08:00
```java
context.clearPermissions();
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
await context.clear_permissions()
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
context.clear_permissions()
```
2021-05-20 04:53:12 +02:00
```csharp
await context.ClearPermissionsAsync();
```
2022-09-08 18:57:20 +02:00
## Geolocation
2021-05-20 04:53:12 +02:00
2023-03-06 16:43:32 +01:00
Grant `"geolocation"` permissions and set geolocation to a specific area.
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
```js
// playwright.config.ts/js
import { defineConfig } from '@playwright/test ';
2021-01-14 15:01:39 -08:00
2023-03-06 16:43:32 +01:00
export default defineConfig({
use: {
// Context geolocation
geolocation: { longitude: 12.492507, latitude: 41.889938 },
permissions: ['geolocation'],
},
2022-09-08 18:57:20 +02:00
});
```
2021-01-08 17:39:33 +01:00
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2022-09-08 18:57:20 +02:00
test.use({
2023-03-07 08:58:49 +01:00
geolocation: { longitude: 41.890221, latitude: 12.492348 },
2022-09-08 18:57:20 +02:00
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page }) => {
// ...
});
```
```js tab=js-library
2020-12-30 18:04:51 -08:00
const context = await browser.newContext({
2023-03-07 08:58:49 +01:00
geolocation: { longitude: 41.890221, latitude: 12.492348 },
2020-12-30 18:04:51 -08:00
permissions: ['geolocation']
});
2022-09-08 18:57:20 +02:00
2020-12-30 18:04:51 -08:00
```
2021-01-08 17:39:33 +01:00
2021-03-01 09:18:44 -08:00
```java
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2023-03-07 08:58:49 +01:00
.setGeolocation(41.890221, 12.492348)
2021-03-05 13:50:34 -08:00
.setPermissions(Arrays.asList("geolocation")));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
context = await browser.new_context(
2023-03-07 08:58:49 +01:00
geolocation={"longitude": 41.890221, "latitude": 12.492348},
2021-01-10 18:18:35 -08:00
permissions=["geolocation"]
2021-01-08 17:39:33 +01:00
)
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
context = browser.new_context(
2023-03-07 08:58:49 +01:00
geolocation={"longitude": 41.890221, "latitude": 12.492348},
2021-01-10 18:18:35 -08:00
permissions=["geolocation"]
2021-01-08 17:39:33 +01:00
)
```
2021-05-20 04:53:12 +02:00
```csharp
2022-07-13 11:50:18 +02:00
await using var context = await browser.NewContextAsync(new()
2021-05-20 08:20:21 -07:00
{
Permissions = new[] { "geolocation" },
2023-03-07 08:58:49 +01:00
Geolocation = new Geolocation() { Longitude = 41.890221, Latitude = 12.492348 }
2021-05-20 08:20:21 -07:00
});
2021-05-20 04:53:12 +02:00
```
2023-03-07 08:58:49 +01:00
< img width = "1394" alt = "geolocation for italy on bing maps" src = "https://user-images.githubusercontent.com/13063165/220417670-bb22d815-f5cd-47c4-8562-0b88165eac27.png" / >
2020-12-30 18:04:51 -08:00
Change the location later:
2022-10-31 17:39:44 +01:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2022-10-31 17:39:44 +01:00
test.use({
2023-03-07 08:58:49 +01:00
geolocation: { longitude: 41.890221, latitude: 12.492348},
2022-10-31 17:39:44 +01:00
permissions: ['geolocation'],
});
test('my test with geolocation', async ({ page, context }) => {
// overwrite the location for this test
2023-03-07 08:58:49 +01:00
await context.setGeolocation({ longitude: 48.858455, latitude: 2.294474 });
2022-10-31 17:39:44 +01:00
});
```
```js tab=js-library
2023-03-07 08:58:49 +01:00
await context.setGeolocation({ longitude: 48.858455, latitude: 2.294474 });
2020-12-30 18:04:51 -08:00
```
2021-03-01 09:18:44 -08:00
```java
2023-03-07 08:58:49 +01:00
context.setGeolocation(new Geolocation(48.858455, 2.294474));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2023-03-07 08:58:49 +01:00
await context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
2021-01-08 17:39:33 +01:00
```
2021-01-11 09:34:49 -08:00
```python sync
2023-03-07 08:58:49 +01:00
context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
2021-01-08 17:39:33 +01:00
```
2021-05-20 04:53:12 +02:00
```csharp
2023-03-07 08:58:49 +01:00
await context.SetGeolocationAsync(new Geolocation() { Longitude = 48.858455, Latitude = 2.294474 });
2021-05-20 04:53:12 +02:00
```
2020-12-30 18:04:51 -08:00
**Note** you can only change geolocation for all pages in the context.
2022-09-08 18:57:20 +02:00
## Color Scheme and Media
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
Emulate the users `"colorScheme"` . Supported values are 'light', 'dark', 'no-preference'. You can also emulate the media type with [`method: Page.emulateMedia` ].
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
```js
import { defineConfig } from '@playwright/test ';
2020-12-30 18:04:51 -08:00
2023-03-06 16:43:32 +01:00
export default defineConfig({
use: {
colorScheme: 'dark',
},
2022-09-08 18:57:20 +02:00
});
```
2020-12-30 18:04:51 -08:00
2022-09-08 18:57:20 +02:00
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2022-09-08 18:57:20 +02:00
test.use({
colorScheme: 'dark' // or 'light'
});
test('my test with dark mode', async ({ page }) => {
// ...
});
```
```js tab=js-library
2020-12-30 18:04:51 -08:00
// Create context with dark mode
const context = await browser.newContext({
colorScheme: 'dark' // or 'light'
});
// Create page with dark mode
const page = await browser.newPage({
colorScheme: 'dark' // or 'light'
});
// Change color scheme for the page
await page.emulateMedia({ colorScheme: 'dark' });
// Change media for page
await page.emulateMedia({ media: 'print' });
```
2021-03-01 09:18:44 -08:00
```java
// Create context with dark mode
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
2021-03-05 13:50:34 -08:00
.setColorScheme(ColorScheme.DARK)); // or "light"
2021-03-01 09:18:44 -08:00
// Create page with dark mode
Page page = browser.newPage(new Browser.NewPageOptions()
2021-03-05 13:50:34 -08:00
.setColorScheme(ColorScheme.DARK)); // or "light"
2021-03-01 09:18:44 -08:00
// Change color scheme for the page
2021-03-05 13:50:34 -08:00
page.emulateMedia(new Page.EmulateMediaOptions().setColorScheme(ColorScheme.DARK));
2021-03-01 09:18:44 -08:00
// Change media for page
2021-03-05 13:50:34 -08:00
page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.PRINT));
2021-03-01 09:18:44 -08:00
```
2021-01-11 09:34:49 -08:00
```python async
2021-01-08 17:39:33 +01:00
# Create context with dark mode
context = await browser.new_context(
color_scheme='dark' # or 'light'
)
# Create page with dark mode
page = await browser.new_page(
color_scheme='dark' # or 'light'
)
# Change color scheme for the page
await page.emulate_media(color_scheme='dark')
# Change media for page
await page.emulate_media(media='print')
```
2021-01-11 09:34:49 -08:00
```python sync
2021-01-08 17:39:33 +01:00
# Create context with dark mode
context = browser.new_context(
color_scheme='dark' # or 'light'
)
# Create page with dark mode
page = browser.new_page(
color_scheme='dark' # or 'light'
)
# Change color scheme for the page
page.emulate_media(color_scheme='dark')
# Change media for page
page.emulate_media(media='print')
```
2021-05-20 04:53:12 +02:00
```csharp
// Create context with dark mode
2022-07-13 11:50:18 +02:00
await using var context = await browser.NewContextAsync(new()
2021-05-20 08:20:21 -07:00
{
ColorScheme = ColorScheme.Dark
});
2021-05-20 04:53:12 +02:00
// Create page with dark mode
2022-07-13 11:50:18 +02:00
var page = await browser.NewPageAsync(new()
2021-05-20 08:20:21 -07:00
{
ColorScheme = ColorScheme.Dark
});
2021-05-20 04:53:12 +02:00
// Change color scheme for the page
2022-07-13 11:50:18 +02:00
await page.EmulateMediaAsync(new()
2021-05-20 08:20:21 -07:00
{
ColorScheme = ColorScheme.Dark
});
2021-05-20 04:53:12 +02:00
// Change media for page
2022-07-13 11:50:18 +02:00
await page.EmulateMediaAsync(new()
2021-05-20 08:20:21 -07:00
{
Media = Media.Print
});
2021-05-20 04:53:12 +02:00
```
2023-03-07 08:58:49 +01:00
< img width = "1394" alt = "playwright web in dark mode" src = "https://user-images.githubusercontent.com/13063165/220411638-55d2b051-4678-4da7-9f0b-ed22f5a3c47c.png" / >
2022-09-08 18:57:20 +02:00
## User Agent
2021-05-20 04:53:12 +02:00
2022-09-08 18:57:20 +02:00
The User Agent is included in the device and therefore you will rarely need to change it however if you do need to test a different user agent you can override it with the `userAgent` property.
2023-03-06 16:43:32 +01:00
```js tab=js-js
// example.spec.ts/js
2022-09-08 18:57:20 +02:00
import { test, expect } from '@playwright/test ';
test.use({ userAgent: 'My user agent'});
test('my user agent test', async ({ page }) => {
// ...
});
```
```js tab=js-library
const context = await browser.newContext({
userAgent: 'My user agent'
});
```
```java
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setUserAgent("My user agent"));
```
```python async
context = await browser.new_context(
user_agent='My user agent'
)
```
```python sync
context = browser.new_context(
user_agent='My user agent'
)
```
```csharp
2023-03-16 17:23:31 +01:00
var context = await browser.NewContextAsync(new() { UserAgent = "My User Agent" });
2022-09-08 18:57:20 +02:00
```
## JavaScript Enabled
Emulate a user scenario where JavaScript is disabled.
```js tab=js-js
2023-03-06 16:43:32 +01:00
// example.spec.ts/js
import { test, expect } from '@playwright/test ';
2022-09-08 18:57:20 +02:00
test.use({ javaScriptEnabled: false });
test('test with no JavaScript', async ({ page }) => {
// ...
});
```
```js tab=js-library
const context = await browser.newContext({
javaScriptEnabled: false
});
2023-03-06 16:43:32 +01:00
```