Skip to content

Commit

Permalink
docs: add python snippets for api classes (follow up) (#5018)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Jan 14, 2021
1 parent 3ed9f2d commit 1648d23
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 52 deletions.
4 changes: 2 additions & 2 deletions docs/src/api/class-accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def find_focused_node(node):
for child in (node.get("children") or []):
found_node = find_focused_node(child)
return found_node
return null
return None

snapshot = await page.accessibility.snapshot()
node = find_focused_node(snapshot)
Expand All @@ -108,7 +108,7 @@ def find_focused_node(node):
for child in (node.get("children") or []):
found_node = find_focused_node(child)
return found_node
return null
return None

snapshot = page.accessibility.snapshot()
node = find_focused_node(snapshot)
Expand Down
24 changes: 12 additions & 12 deletions docs/src/api/class-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,21 @@ Creates a new browser context. It won't share cookies/cache with other browser c
```

```python async
browser = await playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context.
context = await browser.new_context()
# create a new page in a pristine context.
page = await context.new_page()
await page.goto("https://example.com")
browser = await playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context.
context = await browser.new_context()
# create a new page in a pristine context.
page = await context.new_page()
await page.goto("https://example.com")
```

```python sync
browser = playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context.
context = browser.new_context()
# create a new page in a pristine context.
page = context.new_page()
page.goto("https://example.com")
browser = playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context.
context = browser.new_context()
# create a new page in a pristine context.
page = context.new_page()
page.goto("https://example.com")
```

### option: Browser.newContext.-inline- = %%-shared-context-params-list-%%
Expand Down
6 changes: 4 additions & 2 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,15 +567,17 @@ await browser.close();
```python async
context = await browser.new_context()
page = await context.new_page()
await context.route(r"(\.png$)|(\.jpg$)", lambda page = await context.new_page()
await context.route(r"(\.png$)|(\.jpg$)", lambda route => route.abort())
page = await context.new_page()
await page.goto("https://example.com")
await browser.close()
```

```python sync
context = browser.new_context()
page = context.new_page()
context.route(r"(\.png$)|(\.jpg$)", lambda page = await context.new_page()
context.route(r"(\.png$)|(\.jpg$)", lambda route => route.abort())
page = await context.new_page()
page = context.new_page()
page.goto("https://example.com")
browser.close()
Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/class-dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ from playwright.sync_api import sync_playwright

def handle_dialog(dialog):
print(dialog.message)
await dialog.dismiss()
dialog.dismiss()

def run(playwright):
chromium = playwright.chromium
Expand Down
18 changes: 9 additions & 9 deletions docs/src/api/class-elementhandle.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,31 +547,31 @@ element, the method throws an error.

```js
// single selection matching the value
handle.selectOption('select#colors', 'blue');
handle.selectOption('blue');

// single selection matching the label
handle.selectOption('select#colors', { label: 'Blue' });
handle.selectOption({ label: 'Blue' });

// multiple selection
handle.selectOption('select#colors', ['red', 'green', 'blue']);
handle.selectOption(['red', 'green', 'blue']);
```

```python async
# single selection matching the value
await handle.select_option("select#colors", "blue")
await handle.select_option("blue")
# single selection matching the label
await handle.select_option("select#colors", label="blue")
await handle.select_option(label="blue")
# multiple selection
await handle.select_option("select#colors", value=["red", "green", "blue"])
await handle.select_option(value=["red", "green", "blue"])
```

```python sync
# single selection matching the value
handle.select_option("select#colors", "blue")
handle.select_option("blue")
# single selection matching both the label
handle.select_option("select#colors", label="blue")
handle.select_option(label="blue")
# multiple selection
handle.select_option("select#colors", value=["red", "green", "blue"])
handle.select_option(value=["red", "green", "blue"])
```

```python sync
Expand Down
18 changes: 1 addition & 17 deletions docs/src/api/class-frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,7 @@ async def run(playwright):
webkit = playwright.webkit
browser = await webkit.launch()
page = await browser.new_page()
watch_dog = page.main_frame.wait_for_function("() => window.innerWidth < 100")
watch_dog = asyncio.create_task(page.main_frame.wait_for_function("() => window.innerWidth < 100")
await page.set_viewport_size({"width": 50, "height": 50})
await watch_dog
await browser.close()
Expand All @@ -1057,22 +1057,6 @@ async def main():
asyncio.run(main())
```

```python sync
from playwright.sync_api import sync_playwright

def run(playwright):
webkit = playwright.webkit
browser = await webkit.launch()
page = await browser.new_page()
watch_dog = page.main_frame.wait_for_function("() => window.innerWidth < 100")
await page.set_viewport_size({"width": 50, "height": 50})
await watch_dog
await browser.close()

with sync_playwright() as playwright:
run(playwright)
```

To pass an argument to the predicate of `frame.waitForFunction` function:

```js
Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/class-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ Returns the value of the [`param: pageFunction`] invocation as in-page object (J
The only difference between [`method: Page.evaluate`] and [`method: Page.evaluateHandle`] is that [`method: Page.evaluateHandle`] returns in-page
object (JSHandle).

If the function passed to the [`method: Page.evaluateHandle`] returns a [Promise], then [`method:Ppage.EvaluateHandle`] would wait for the
If the function passed to the [`method: Page.evaluateHandle`] returns a [Promise], then [`method: Page.evaluateHandle`] would wait for the
promise to resolve and return its value.

```js
Expand Down
3 changes: 0 additions & 3 deletions docs/src/api/class-playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ with sync_playwright() as playwright:
run(playwright)
```

By default, the `playwright` NPM package automatically downloads browser executables during installation. The
`playwright-core` NPM package can be used to skip automatic downloads.

## property: Playwright.chromium
- type: <[BrowserType]>

Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/class-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ page.on('requestfailed', request => {
```

```py
page.on("requestfailed", lambda: request => print(request.url + " " + request.failure)
page.on("requestfailed", lambda request: print(request.url + " " + request.failure))
```

## method: Request.frame
Expand Down
10 changes: 6 additions & 4 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export interface Page {
*
* If the function passed to the
* [page.evaluateHandle(…)](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatehandle) returns a
* [Promise], then [`method:Ppage.EvaluateHandle`] would wait for the promise to resolve and return its value.
* [Promise], then
* [page.evaluateHandle(…)](https://github.com/microsoft/playwright/blob/master/docs/api.md#pageevaluatehandle) would wait
* for the promise to resolve and return its value.
*
* ```js
* const aWindowHandle = await page.evaluateHandle(() => Promise.resolve(window));
Expand Down Expand Up @@ -5940,13 +5942,13 @@ export interface ElementHandle<T=Node> extends JSHandle<T> {
*
* ```js
* // single selection matching the value
* handle.selectOption('select#colors', 'blue');
* handle.selectOption('blue');
*
* // single selection matching the label
* handle.selectOption('select#colors', { label: 'Blue' });
* handle.selectOption({ label: 'Blue' });
*
* // multiple selection
* handle.selectOption('select#colors', ['red', 'green', 'blue']);
* handle.selectOption(['red', 'green', 'blue']);
* ```
*
* @param values Options to select. If the `<select>` has the `multiple` attribute, all matching options are selected, otherwise only the first option matching one of the passed options is selected. String values are equivalent to `{value:'string'}`. Option
Expand Down

0 comments on commit 1648d23

Please sign in to comment.