Skip to content

Commit e67603d

Browse files
committed
docs(emulation): review, fix nits
1 parent 42eefa6 commit e67603d

File tree

1 file changed

+64
-31
lines changed

1 file changed

+64
-31
lines changed

docs/emulation.md

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
# Device and environment emulation
2-
Playwright allows overriding various parameters that depend on the device where the browser is running (such as viewport size, touch support, dpr etc.) as well as custom system settings such as locale and timezone. Most of these parameters are configured during context construction but some of them (e.g. viewport size) can be changed for individual pages.
2+
3+
Playwright allows overriding various parameters of the device where the browser is running:
4+
- viewport size, device scale factor, touch support
5+
- locale, timezone
6+
- color scheme
7+
- geolocation
8+
- etc
9+
10+
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.
11+
12+
<br/>
313

414
## Emulating popular devices
5-
Playwright comes with a registry of device parameters for some popular mobile devices. It can be used to simulate browser behavior on a mobile device like this:
15+
16+
Playwright comes with a registry of device parameters for selected mobile devices. It can be used to simulate browser behavior on a mobile device:
17+
618
```js
719
const { chromium, devices } = require('playwright');
820
const browser = await chromium.launch();
@@ -12,6 +24,7 @@ Playwright comes with a registry of device parameters for some popular mobile de
1224
...pixel2,
1325
});
1426
```
27+
1528
All pages created in the context above will share the same device parameters.
1629

1730
#### API reference
@@ -20,34 +33,36 @@ All pages created in the context above will share the same device parameters.
2033
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
2134

2235
<br/>
23-
<br/>
2436

25-
## Configuring screen size(viewport), touch support, isMobile ...
37+
## Configuring screen size, touch support, mobile viewport
38+
2639
Create a context with custom viewport size:
40+
2741
```js
2842
const context = await browser.newContext({
2943
viewport: {
3044
width: 1280,
3145
height: 1024
3246
}
3347
});
48+
3449
```
3550
Resize viewport for individual pages:
3651

3752
```js
3853
await page.setViewportSize({ 'width': 1600, 'height': 1200 });
3954
```
4055

41-
Emulate custom mobile device _without_ touch support:
56+
Emulate desktop device with the high-DPI screen and touch support:
57+
4258
```js
4359
const context = await browser.newContext({
4460
viewport: {
45-
width: 400,
46-
height: 900,
61+
width: 2560,
62+
height: 1440,
4763
},
4864
deviceScaleFactor: 2,
49-
isMobile: true,
50-
hasTouch: false
65+
hasTouch: true
5166
});
5267
```
5368

@@ -57,11 +72,48 @@ Emulate custom mobile device _without_ touch support:
5772
- [`page.setViewportSize(viewportSize)`](./api.md#pagesetviewportsizeviewportsize)
5873

5974
<br/>
75+
76+
## Configuring color scheme
77+
78+
Create device with the dark color scheme:
79+
80+
81+
```js
82+
const context = await browser.newContext({
83+
colorScheme: 'dark'
84+
});
85+
```
86+
87+
Change color scheme for individual pages:
88+
89+
```js
90+
await page.emulateMedia({ colorScheme: 'dark' });
91+
```
92+
93+
#### API reference
94+
95+
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
96+
- [`page.emulateMedia([options])`](./api.md#pageemulatemediaoptions)
97+
6098
<br/>
6199

100+
## Locale and timezone
101+
102+
```js
103+
const context = await browser.newContext({
104+
locale: 'de-DE',
105+
timezoneId: 'Europe/Berlin',
106+
});
107+
```
108+
109+
#### API reference
110+
111+
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
112+
113+
<br/>
62114

63115
## Geolocation
64-
Create a context with 'geolocation' permissions granted:
116+
Create a context with `"geolocation"` permissions granted:
65117
```js
66118
const context = await browser.newContext({
67119
geolocation: { longitude: 48.858455, latitude: 2.294474 },
@@ -80,7 +132,6 @@ Change the location later:
80132
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
81133
- [`browserContext.setGeolocation(geolocation)`](./api.md#browsercontextsetgeolocationgeolocation)
82134
83-
<br/>
84135
<br/>
85136
86137
## Permissions
@@ -96,9 +147,9 @@ Grant all pages in the existing context access to current location:
96147
await context.grantPermissions(['geolocation']);
97148
```
98149
99-
Grant camera and mic access from a specific domain:
150+
Grant notifications access from a specific domain:
100151
```js
101-
await context.grantPermissions(['camera', 'microphone'], {origin: 'https://skype.com'} );
152+
await context.grantPermissions(['notifications'], {origin: 'https://skype.com'} );
102153
```
103154
Revoke all permissions:
104155
```js
@@ -112,21 +163,3 @@ Revoke all permissions:
112163
- [`browserContext.clearPermissions()`](./api.md#browsercontextclearpermissions)
113164
114165
<br/>
115-
<br/>
116-
117-
## Locale and timzeone
118-
119-
```js
120-
const context = await browser.newContext({
121-
locale: 'ru-RU',
122-
timezoneId: 'Europe/Moscow',
123-
});
124-
```
125-
126-
#### API reference
127-
128-
- [`browser.newContext([options])`](./api.md#browsernewcontextoptions)
129-
130-
<br/>
131-
<br/>
132-

0 commit comments

Comments
 (0)