Skip to content

Commit b2d820a

Browse files
authored
docs(emulation): separate section for dark mode (#2915)
1 parent 9fd30e5 commit b2d820a

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@
2727
- [Upload files](./input.md#upload-files)
2828
- [Focus element](./input.md#focus-element)
2929
1. [Emulation](./emulation.md)
30+
- [Devices](./emulation.md#devices)
3031
- [Overview](./emulation.md#)
3132
- [User agent](./emulation.md#user-agent)
32-
- [Viewport, color scheme](./emulation.md#viewport-color-scheme)
33-
- [Devices](./emulation.md#devices)
33+
- [Viewport](./emulation.md#viewport)
3434
- [Locale & Timezone](./emulation.md#locale--timezone)
3535
- [Permissions](./emulation.md#permissions)
3636
- [Geolocation](./emulation.md#geolocation)
37+
- [Color scheme and media](./emulation.md#color-scheme-and-media)
3738
1. [Network](./network.md)
3839
- [Overview](./network.md#)
3940
- [HTTP Authentication](./network.md#http-authentication)

docs/emulation.md

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,44 @@ Playwright allows overriding various parameters of the device where the browser
55
- locale, timezone
66
- color scheme
77
- geolocation
8-
- etc
98

109
Most of these parameters are configured during the browser context construction, but some of them such as viewport size can be changed for individual pages.
1110

1211
<!-- GEN:toc-top-level -->
13-
- [User agent](#user-agent)
14-
- [Viewport, color scheme](#viewport-color-scheme)
1512
- [Devices](#devices)
13+
- [User agent](#user-agent)
14+
- [Viewport](#viewport)
1615
- [Locale & timezone](#locale--timezone)
1716
- [Permissions](#permissions)
1817
- [Geolocation](#geolocation)
18+
- [Color scheme and media](#color-scheme-and-media)
1919
<!-- GEN:stop -->
2020

2121
<br/>
2222

23+
## Devices
24+
25+
Playwright comes with a registry of device parameters for selected mobile devices. It can be used to simulate browser behavior on a mobile device:
26+
27+
```js
28+
const { chromium, devices } = require('playwright');
29+
const browser = await chromium.launch();
30+
31+
const pixel2 = devices['Pixel 2'];
32+
const context = await browser.newContext({
33+
...pixel2,
34+
});
35+
```
36+
37+
All pages created in the context above will share the same device parameters.
38+
39+
#### API reference
40+
41+
- [`playwright.devices`](./api.md#playwrightdevices)
42+
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
43+
44+
<br/>
45+
2346
## User agent
2447

2548
All pages created in the context above will share the user agent specified:
@@ -36,7 +59,7 @@ const context = await browser.newContext({
3659

3760
<br/>
3861

39-
## Viewport, color scheme
62+
## Viewport
4063

4164
Create a context with custom viewport size:
4265

@@ -54,48 +77,15 @@ const context = await browser.newContext({
5477
viewport: { width: 2560, height: 1440 },
5578
deviceScaleFactor: 2,
5679
});
57-
58-
// Create device with the dark color scheme:
59-
const context = await browser.newContext({
60-
colorScheme: 'dark'
61-
});
62-
63-
// Change color scheme for the page
64-
await page.emulateMedia({ colorScheme: 'dark' });
6580
```
6681

6782
#### API reference
6883

6984
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
70-
- [`page.emulateMedia([options])`](./api.md#pageemulatemediaoptions)
7185
- [`page.setViewportSize(viewportSize)`](./api.md#pagesetviewportsizeviewportsize)
7286

7387
<br/>
7488

75-
## Devices
76-
77-
Playwright comes with a registry of device parameters for selected mobile devices. It can be used to simulate browser behavior on a mobile device:
78-
79-
```js
80-
const { chromium, devices } =
81-
require('playwright');
82-
const browser = await chromium.launch();
83-
84-
const pixel2 = devices['Pixel 2'];
85-
const context = await browser.newContext({
86-
...pixel2,
87-
});
88-
```
89-
90-
All pages created in the context above will share the same device parameters.
91-
92-
#### API reference
93-
94-
- [`playwright.devices`](./api.md#playwrightdevices)
95-
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
96-
97-
<br/>
98-
9989
## Locale & timezone
10090

10191
```js
@@ -166,3 +156,31 @@ await context.setGeolocation({ longitude: 29.979097, latitude: 31.134256 });
166156
- [`browserContext.setGeolocation(geolocation)`](./api.md#browsercontextsetgeolocationgeolocation)
167157

168158
<br/>
159+
160+
## Color scheme and media
161+
162+
Create a context with dark or light mode. Pages created in this context will
163+
follow this color scheme preference.
164+
165+
```js
166+
// Create context with dark mode
167+
const context = await browser.newContext({
168+
colorScheme: 'dark' // or 'light'
169+
});
170+
171+
// Create page with dark mode
172+
const page = await browser.newPage({
173+
colorScheme: 'dark' // or 'light'
174+
});
175+
176+
// Change color scheme for the page
177+
await page.emulateMedia({ colorScheme: 'dark' });
178+
179+
// Change media for page
180+
await page.emulateMedia({ media: 'print' });
181+
```
182+
183+
#### API reference
184+
185+
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
186+
- [`page.emulateMedia([options])`](./api.md#pageemulatemediaoptions)

0 commit comments

Comments
 (0)