Skip to content

Commit 7701176

Browse files
authored
docs: allow lang-specific sh snippets (#5024)
1 parent de5d671 commit 7701176

File tree

8 files changed

+124
-37
lines changed

8 files changed

+124
-37
lines changed

docs/src/api/class-browsercontext.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ the JavaScript environment, e.g. to seed `Math.random`.
124124

125125
An example of overriding `Math.random` before the page loads:
126126

127-
```js
127+
```js browser
128128
// preload.js
129129
Math.random = () => 42;
130130
```

docs/src/api/class-page.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ the JavaScript environment, e.g. to seed `Math.random`.
429429

430430
An example of overriding `Math.random` before the page loads:
431431

432-
```js
432+
```js browser
433433
// preload.js
434434
Math.random = () => 42;
435435
```

docs/src/cli.md

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ Playwright comes with the command line tools that run via `npx` or as a part of
99

1010
## Usage
1111

12-
```sh
12+
```sh js
1313
$ npx playwright --help
1414
```
1515

16+
```sh python
17+
$ python -m playwright
18+
```
19+
1620
Running from `package.json` script
1721
```json
1822
{
@@ -24,10 +28,14 @@ Running from `package.json` script
2428

2529
## Generate code
2630

27-
```sh
31+
```sh js
2832
$ npx playwright codegen wikipedia.org
2933
```
3034

35+
```sh python
36+
$ python -m playwright codegen wikipedia.org
37+
```
38+
3139
Run `codegen` and perform actions in the browser. Playwright CLI will generate JavaScript code for the user interactions. `codegen` will attempt to generate resilient text-based selectors.
3240

3341
<img src="https://user-images.githubusercontent.com/284612/92536033-7e7ebe00-f1ed-11ea-9e1a-7cbd912e3391.gif"></img>
@@ -36,53 +44,89 @@ Run `codegen` and perform actions in the browser. Playwright CLI will generate J
3644

3745
Run `codegen` with `--save-storage` to save [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) and [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) at the end. This is useful to separately record authentication step and reuse it later.
3846

39-
```sh
47+
```sh js
4048
$ npx playwright --save-storage=auth.json codegen
4149
# Perform authentication and exit.
4250
# auth.json will contain the storage state.
4351
```
4452

53+
```sh python
54+
$ python -m playwright --save-storage=auth.json codegen
55+
# Perform authentication and exit.
56+
# auth.json will contain the storage state.
57+
```
58+
4559
Run with `--load-storage` to consume previously loaded storage. This way, all [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) and [localStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) will be restored, bringing most web apps to the authenticated state.
4660

47-
```sh
61+
```sh js
4862
$ npx playwright --load-storage=auth.json open my.web.app
4963
$ npx playwright --load-storage=auth.json codegen my.web.app
5064
# Perform actions in authenticated state.
5165
```
5266

67+
```sh python
68+
$ python -m playwright --load-storage=auth.json open my.web.app
69+
$ python -m playwright --load-storage=auth.json codegen my.web.app
70+
# Perform actions in authenticated state.
71+
```
72+
5373
## Open pages
5474

5575
With `open`, you can use Playwright bundled browsers to browse web pages. Playwright provides cross-platform WebKit builds that can be used to reproduce Safari rendering across Windows, Linux and macOS.
5676

57-
```sh
77+
```sh js
5878
# Open page in Chromium
59-
npx playwright open example.com
79+
$ npx playwright open example.com
6080
```
6181

62-
```sh
82+
```sh python
83+
# Open page in Chromium
84+
$ python -m playwright open example.com
85+
```
86+
87+
```sh js
88+
# Open page in WebKit
89+
$ npx playwright wk example.com
90+
```
91+
92+
```sh python
6393
# Open page in WebKit
64-
npx playwright wk example.com
94+
$ python -m playwright wk example.com
6595
```
6696

6797
### Emulate devices
6898
`open` can emulate mobile and tablet devices ([see all devices](https://github.com/microsoft/playwright/blob/master/src/server/deviceDescriptors.ts)).
6999

70-
```sh
100+
```sh js
71101
# Emulate iPhone 11.
72-
npx playwright --device="iPhone 11" open wikipedia.org
102+
$ npx playwright --device="iPhone 11" open wikipedia.org
103+
```
104+
105+
```sh python
106+
# Emulate iPhone 11.
107+
$ python -m playwright --device="iPhone 11" open wikipedia.org
73108
```
74109

75110
### Emulate color scheme and viewport size
76-
```sh
111+
```sh js
112+
# Emulate screen size and color scheme.
113+
$ npx playwright --viewport-size=800,600 --color-scheme=dark open twitter.com
114+
```
115+
```sh python
77116
# Emulate screen size and color scheme.
78-
npx playwright --viewport-size=800,600 --color-scheme=dark open twitter.com
117+
$ python -m playwright --viewport-size=800,600 --color-scheme=dark open twitter.com
79118
```
80119

81120
### Emulate geolocation, language and timezone
82-
```sh
121+
```sh js
122+
# Emulate timezone, language & location
123+
# Once page opens, click the "my location" button to see geolocation in action
124+
$ npx playwright --timezone="Europe/Rome" --geolocation="41.890221,12.492348" --lang="it-IT" open maps.google.com
125+
```
126+
```sh python
83127
# Emulate timezone, language & location
84128
# Once page opens, click the "my location" button to see geolocation in action
85-
npx playwright --timezone="Europe/Rome" --geolocation="41.890221,12.492348" --lang="it-IT" open maps.google.com
129+
$ python -m playwright --timezone="Europe/Rome" --geolocation="41.890221,12.492348" --lang="it-IT" open maps.google.com
86130
```
87131

88132
## Inspect selectors
@@ -130,34 +174,59 @@ Generates selector for the given element.
130174

131175
## Take screenshot
132176

133-
```sh
177+
```sh js
134178
# See command help
135179
$ npx playwright screenshot --help
136180
```
137181

138-
```sh
182+
```sh python
183+
# See command help
184+
$ python -m playwright screenshot --help
185+
```
186+
187+
```sh js
139188
# Wait 3 seconds before capturing a screenshot after page loads ('load' event fires)
140-
npx playwright \
189+
$ npx playwright \
141190
--device="iPhone 11" \
142191
--color-scheme=dark \
143192
screenshot \
144193
--wait-for-timeout=3000 \
145194
twitter.com twitter-iphone.png
146195
```
147196

148-
```sh
197+
```sh python
198+
# Wait 3 seconds before capturing a screenshot after page loads ('load' event fires)
199+
$ python -m playwright \
200+
--device="iPhone 11" \
201+
--color-scheme=dark \
202+
screenshot \
203+
--wait-for-timeout=3000 \
204+
twitter.com twitter-iphone.png
205+
```
206+
207+
```sh js
208+
# Capture a full page screenshot
209+
$ npx playwright screenshot --full-page en.wikipedia.org wiki-full.png
210+
```
211+
212+
```sh python
149213
# Capture a full page screenshot
150-
npx playwright screenshot --full-page en.wikipedia.org wiki-full.png
214+
$ python -m playwright screenshot --full-page en.wikipedia.org wiki-full.png
151215
```
152216

153217
## Generate PDF
154218

155219
PDF generation only works in Headless Chromium.
156220

157-
```sh
221+
```sh js
158222
# See command help
159223
$ npx playwright pdf https://en.wikipedia.org/wiki/PDF wiki.pdf
160224
```
161225

226+
```sh python
227+
# See command help
228+
$ python -m playwright pdf https://en.wikipedia.org/wiki/PDF wiki.pdf
229+
```
230+
162231
## Known limitations
163232
Opening WebKit Web Inspector will disconnect Playwright from the browser. In such cases, code generation will stop.

docs/src/debug.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ chromium.launch(devtools=True)
8585
Set the `PWDEBUG` environment variable to run your scripts in debug mode. This
8686
configures the browser for debugging.
8787

88-
```sh
88+
```sh js
8989
# Linux/macOS
9090
$ PWDEBUG=1 npm run test
9191

@@ -94,6 +94,15 @@ $ set PWDEBUG=1
9494
$ npm run test
9595
```
9696

97+
```sh python
98+
# Linux/macOS
99+
$ PWDEBUG=1 pytest -s
100+
101+
# Windows
102+
$ set PWDEBUG=1
103+
$ pytest -s
104+
```
105+
97106
### Defaults
98107

99108
With PWDEBUG, the following defaults are configured for you:
@@ -132,11 +141,20 @@ This improves the debugging experience for JavaScript executions in the page con
132141

133142
Playwright supports verbose logging with the `DEBUG` environment variable.
134143

135-
```sh
144+
```sh js
136145
# Linux/macOS
137146
$ DEBUG=pw:api npm run test
138147

139148
# Windows
140149
$ set DEBUG=pw:api
141150
$ npm run test
142151
```
152+
153+
```sh python
154+
# Linux/macOS
155+
$ DEBUG=pw:api pytest -s
156+
157+
# Windows
158+
$ set DEBUG=pw:api
159+
$ pytest -s
160+
```

docs/src/intro-python.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ title: "Getting Started"
1010
Use pip to install Playwright in your Python project. See [system requirements](#system-requirements).
1111

1212
```sh
13-
pip install playwright
14-
python -m playwright install
13+
$ pip install playwright
14+
$ python -m playwright install
1515
```
1616

1717
These commands download the Playwright package and install browser binaries for Chromium, Firefox and WebKit. To modify this behavior see [installation parameters](./installation.md).
@@ -74,7 +74,7 @@ firefox.launch(headless=False, slowMo=50)
7474
Command Line Interface [CLI](./cli.md) can be used to record user interactions and generate Python code.
7575

7676
```sh
77-
python -m playwright codegen wikipedia.org
77+
$ python -m playwright codegen wikipedia.org
7878
```
7979

8080
## System requirements

docs/src/intro.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title: "Getting Started"
1010
Use npm or Yarn to install Playwright in your Node.js project. See [system requirements](#system-requirements).
1111

1212
```sh
13-
npm i -D playwright
13+
$ npm i -D playwright
1414
```
1515

1616
This single command downloads the Playwright NPM package and browser binaries for Chromium, Firefox and WebKit. To modify this behavior see [installation parameters](./installation.md).
@@ -65,7 +65,7 @@ firefox.launch({ headless: false, slowMo: 50 });
6565
Command Line Interface [CLI](./cli.md) can be used to record user interactions and generate JavaScript code.
6666

6767
```sh
68-
npx playwright codegen wikipedia.org
68+
$ npx playwright codegen wikipedia.org
6969
```
7070

7171
## TypeScript support

docs/src/test-runners-python.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ in Python.
1111
## Usage
1212

1313
```sh
14-
pip install pytest-playwright
14+
$ pip install pytest-playwright
1515
```
1616

1717
Use the `page` fixture to write a basic test. See [more examples](#examples).
@@ -27,16 +27,16 @@ To run your tests, use pytest CLI.
2727

2828
```sh
2929
# Run tests (Chromium and headless by default)
30-
pytest
30+
$ pytest
3131

3232
# Run tests in headful mode
33-
pytest --headful
33+
$ pytest --headful
3434

3535
# Run tests in a different browser (chromium, firefox, webkit)
36-
pytest --browser firefox
36+
$ pytest --browser firefox
3737

3838
# Run tests in multiple browsers
39-
pytest --browser chromium --browser webkit
39+
$ pytest --browser chromium --browser webkit
4040
```
4141

4242
If you want to add the CLI arguments automatically without specifying them, you can use the [pytest.ini](https://docs.pytest.org/en/stable/reference.html#ini-options-ref) file:
@@ -114,7 +114,7 @@ def test_visit_example(page):
114114
Start Pytest with the `base-url` argument.
115115

116116
```sh
117-
pytest --base-url http://localhost:8080
117+
$ pytest --base-url http://localhost:8080
118118
```
119119

120120
```py

types/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ export interface Page {
12271227
*
12281228
* An example of overriding `Math.random` before the page loads:
12291229
*
1230-
* ```js
1230+
* ```js browser
12311231
* // preload.js
12321232
* Math.random = () => 42;
12331233
* ```
@@ -4790,7 +4790,7 @@ export interface BrowserContext {
47904790
*
47914791
* An example of overriding `Math.random` before the page loads:
47924792
*
4793-
* ```js
4793+
* ```js browser
47944794
* // preload.js
47954795
* Math.random = () => 42;
47964796
* ```

0 commit comments

Comments
 (0)