Skip to content

Commit 8354a91

Browse files
authored
docs: add python snippets for api classes (#5011)
1 parent 5408e26 commit 8354a91

23 files changed

+1699
-307
lines changed

docs/src/api/class-accessibility.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ Captures the current state of the accessibility tree. The returned object repres
4747
page.
4848

4949
:::note
50-
The Chromium accessibility tree contains nodes that go unused on most platforms and by most screen readers.
51-
Playwright will discard them as well for an easier to process tree, unless [`option: interestingOnly`] is set to
52-
`false`.
50+
The Chromium accessibility tree contains nodes that go unused on most platforms and by most screen readers. Playwright
51+
will discard them as well for an easier to process tree, unless [`option: interestingOnly`] is set to `false`.
5352
:::
5453

5554
An example of dumping the entire accessibility tree:
@@ -59,6 +58,16 @@ const snapshot = await page.accessibility.snapshot();
5958
console.log(snapshot);
6059
```
6160

61+
```python async
62+
snapshot = await page.accessibility.snapshot()
63+
print(snapshot)
64+
```
65+
66+
```python sync
67+
snapshot = page.accessibility.snapshot()
68+
print(snapshot)
69+
```
70+
6271
An example of logging the focused node's name:
6372

6473
```js
@@ -77,6 +86,36 @@ function findFocusedNode(node) {
7786
}
7887
```
7988

89+
```python async
90+
def find_focused_node(node):
91+
if (node.get("focused"))
92+
return node
93+
for child in (node.get("children") or []):
94+
found_node = find_focused_node(child)
95+
return found_node
96+
return null
97+
98+
snapshot = await page.accessibility.snapshot()
99+
node = find_focused_node(snapshot)
100+
if node:
101+
print(node["name"])
102+
```
103+
104+
```python sync
105+
def find_focused_node(node):
106+
if (node.get("focused"))
107+
return node
108+
for child in (node.get("children") or []):
109+
found_node = find_focused_node(child)
110+
return found_node
111+
return null
112+
113+
snapshot = page.accessibility.snapshot()
114+
node = find_focused_node(snapshot)
115+
if node:
116+
print(node["name"])
117+
```
118+
80119
### option: Accessibility.snapshot.interestingOnly
81120
- `interestingOnly` <[boolean]>
82121

docs/src/api/class-browser.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.
1414
})();
1515
```
1616

17+
```python async
18+
import asyncio
19+
from playwright.async_api import async_playwright
20+
21+
async def run(playwright):
22+
firefox = playwright.firefox
23+
browser = await firefox.launch()
24+
page = await browser.new_page()
25+
await page.goto("https://example.com")
26+
await browser.close()
27+
28+
async def main():
29+
async with async_playwright() as playwright:
30+
await run(playwright)
31+
asyncio.run(main())
32+
```
33+
34+
```python sync
35+
from playwright.sync_api import sync_playwright
36+
37+
def run(playwright):
38+
firefox = playwright.firefox
39+
browser = firefox.launch()
40+
page = browser.new_page()
41+
page.goto("https://example.com")
42+
browser.close()
43+
44+
with sync_playwright() as playwright:
45+
run(playwright)
46+
```
47+
1748
## event: Browser.disconnected
1849

1950
Emitted when Browser gets disconnected from the browser application. This might happen because of one of the following:
@@ -25,8 +56,8 @@ Emitted when Browser gets disconnected from the browser application. This might
2556
In case this browser is obtained using [`method: BrowserType.launch`], closes the browser and all of its pages (if any
2657
were opened).
2758

28-
In case this browser is connected to, clears all created contexts belonging to this
29-
browser and disconnects from the browser server.
59+
In case this browser is connected to, clears all created contexts belonging to this browser and disconnects from the
60+
browser server.
3061

3162
The [Browser] object itself is considered to be disposed and cannot be used anymore.
3263

@@ -43,6 +74,20 @@ const context = await browser.newContext();
4374
console.log(browser.contexts().length); // prints `1`
4475
```
4576

77+
```python async
78+
browser = await pw.webkit.launch()
79+
print(len(browser.contexts())) # prints `0`
80+
context = await browser.new_context()
81+
print(len(browser.contexts())) # prints `1`
82+
```
83+
84+
```python sync
85+
browser = pw.webkit.launch()
86+
print(len(browser.contexts())) # prints `0`
87+
context = browser.new_context()
88+
print(len(browser.contexts())) # prints `1`
89+
```
90+
4691
## method: Browser.isConnected
4792
- returns: <[boolean]>
4893

@@ -64,6 +109,24 @@ Creates a new browser context. It won't share cookies/cache with other browser c
64109
})();
65110
```
66111

112+
```python async
113+
browser = await playwright.firefox.launch() # or "chromium" or "webkit".
114+
# create a new incognito browser context.
115+
context = await browser.new_context()
116+
# create a new page in a pristine context.
117+
page = await context.new_page()
118+
await page.goto("https://example.com")
119+
```
120+
121+
```python sync
122+
browser = playwright.firefox.launch() # or "chromium" or "webkit".
123+
# create a new incognito browser context.
124+
context = browser.new_context()
125+
# create a new page in a pristine context.
126+
page = context.new_page()
127+
page.goto("https://example.com")
128+
```
129+
67130
### option: Browser.newContext.-inline- = %%-shared-context-params-list-%%
68131

69132
### option: Browser.newContext.proxy = %%-context-option-proxy-%%
@@ -76,8 +139,8 @@ Creates a new browser context. It won't share cookies/cache with other browser c
76139
Creates a new page in a new browser context. Closing this page will close the context as well.
77140

78141
This is a convenience API that should only be used for the single-page scenarios and short snippets. Production code and
79-
testing frameworks should explicitly create [`method: Browser.newContext`] followed by the [`method:
80-
BrowserContext.newPage`] to control their exact life times.
142+
testing frameworks should explicitly create [`method: Browser.newContext`] followed by the
143+
[`method: BrowserContext.newPage`] to control their exact life times.
81144

82145
### option: Browser.newPage.-inline- = %%-shared-context-params-list-%%
83146

0 commit comments

Comments
 (0)