docs(python): fix syntactically incorrect snippets (#23904)

This commit is contained in:
Max Schmitt 2023-06-27 11:53:27 +02:00 коммит произвёл GitHub
Родитель 4b1b4dc23b
Коммит 71650f9bd1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 56 добавлений и 59 удалений

Просмотреть файл

@ -117,11 +117,11 @@ String snapshot = page.accessibility().snapshot();
```python async
def find_focused_node(node):
if (node.get("focused"))
if node.get("focused"):
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
if (found_node)
if found_node:
return found_node
return None
@ -133,11 +133,11 @@ if node:
```python sync
def find_focused_node(node):
if (node.get("focused"))
if node.get("focused"):
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
if (found_node)
if found_node:
return found_node
return None

Просмотреть файл

@ -1125,18 +1125,18 @@ context.route("/api/**", route -> {
```python async
def handle_route(route):
if ("my-string" in route.request.post_data)
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else
else:
route.continue_()
await context.route("/api/**", handle_route)
```
```python sync
def handle_route(route):
if ("my-string" in route.request.post_data)
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else
else:
route.continue_()
context.route("/api/**", handle_route)
```

Просмотреть файл

@ -410,13 +410,13 @@ assertEquals("10", tweetHandle.evalOnSelector(".retweets", "node => node.innerTe
```python async
tweet_handle = await page.query_selector(".tweet")
assert await tweet_handle.eval_on_selector(".like", "node => node.innerText") == "100"
assert await tweet_handle.eval_on_selector(".retweets", "node => node.innerText") = "10"
assert await tweet_handle.eval_on_selector(".retweets", "node => node.innerText") == "10"
```
```python sync
tweet_handle = page.query_selector(".tweet")
assert tweet_handle.eval_on_selector(".like", "node => node.innerText") == "100"
assert tweet_handle.eval_on_selector(".retweets", "node => node.innerText") = "10"
assert tweet_handle.eval_on_selector(".retweets", "node => node.innerText") == "10"
```
```csharp

Просмотреть файл

@ -966,19 +966,18 @@ rowLocator
```python async
row_locator = page.locator("tr")
# ...
await row_locator
.filter(has_text="text in column 1")
.filter(has=page.get_by_role("button", name="column 2 button"))
.screenshot()
await row_locator.filter(has_text="text in column 1").filter(
has=page.get_by_role("button", name="column 2 button")
).screenshot()
```
```python sync
row_locator = page.locator("tr")
# ...
row_locator
.filter(has_text="text in column 1")
.filter(has=page.get_by_role("button", name="column 2 button"))
.screenshot()
row_locator.filter(has_text="text in column 1").filter(
has=page.get_by_role("button", name="column 2 button")
).screenshot()
```
```csharp
@ -1589,7 +1588,7 @@ newEmail.click();
new_email = page.get_by_role("button", name="New")
dialog = page.get_by_text("Confirm security settings")
await expect(new_email.or_(dialog)).to_be_visible()
if (await dialog.is_visible())
if (await dialog.is_visible()):
await page.get_by_role("button", name="Dismiss").click()
await new_email.click()
```
@ -1598,7 +1597,7 @@ await new_email.click()
new_email = page.get_by_role("button", name="New")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).to_be_visible()
if (dialog.is_visible())
if (dialog.is_visible()):
page.get_by_role("button", name="Dismiss").click()
new_email.click()
```

Просмотреть файл

@ -255,6 +255,7 @@ try:
# or while waiting for an event.
await page.wait_for_event("popup")
except Error as e:
pass
# when the page crashes, exception message contains "crash".
```
@ -265,6 +266,7 @@ try:
# or while waiting for an event.
page.wait_for_event("popup")
except Error as e:
pass
# when the page crashes, exception message contains "crash".
```
@ -3252,18 +3254,18 @@ page.route("/api/**", route -> {
```python async
def handle_route(route):
if ("my-string" in route.request.post_data)
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else
else:
route.continue_()
await page.route("/api/**", handle_route)
```
```python sync
def handle_route(route):
if ("my-string" in route.request.post_data)
if ("my-string" in route.request.post_data):
route.fulfill(body="mocked-data")
else
else:
route.continue_()
page.route("/api/**", handle_route)
```

Просмотреть файл

@ -70,7 +70,7 @@ async def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value" # set "foo" header
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
await route.continue_(headers=headers)
@ -83,7 +83,7 @@ def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value" # set "foo" header
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
route.continue_(headers=headers)
@ -356,7 +356,7 @@ async def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value" # set "foo" header
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
await route.fallback(headers=headers)
@ -369,7 +369,7 @@ def handle(route, request):
# override headers
headers = {
**request.headers,
"foo": "foo-value" # set "foo" header
"foo": "foo-value", # set "foo" header
"bar": None # remove "bar" header
}
route.fallback(headers=headers)

Просмотреть файл

@ -412,15 +412,13 @@ assertThat(page
```
```python async
await expect(page
.get_by_text(re.compile("welcome, john", re.IGNORECASE)))
.to_be_visible()
await expect(
page.get_by_text(re.compile("welcome, john", re.IGNORECASE))
).to_be_visible()
```
```python sync
expect(page
.get_by_text(re.compile("welcome, john", re.IGNORECASE)))
.to_be_visible()
expect(page.get_by_text(re.compile("welcome, john", re.IGNORECASE))).to_be_visible()
```
```csharp
@ -1193,7 +1191,7 @@ newEmail.click();
new_email = page.get_by_role("button", name="New")
dialog = page.get_by_text("Confirm security settings")
await expect(new_email.or_(dialog)).to_be_visible()
if (await dialog.is_visible())
if (await dialog.is_visible()):
await page.get_by_role("button", name="Dismiss").click()
await new_email.click()
```
@ -1202,7 +1200,7 @@ await new_email.click()
new_email = page.get_by_role("button", name="New")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).to_be_visible()
if (dialog.is_visible())
if (dialog.is_visible()):
page.get_by_role("button", name="Dismiss").click()
new_email.click()
```
@ -1541,19 +1539,17 @@ await rowLocator
```python async
row_locator = page.get_by_role("listitem")
await row_locator
.filter(has_text="Mary")
.filter(has=page.get_by_role("button", name="Say goodbye"))
.screenshot(path="screenshot.png")
await row_locator.filter(has_text="Mary").filter(
has=page.get_by_role("button", name="Say goodbye")
).screenshot(path="screenshot.png")
```
```python sync
row_locator = page.get_by_role("listitem")
row_locator
.filter(has_text="Mary")
.filter(has=page.get_by_role("button", name="Say goodbye"))
.screenshot(path="screenshot.png")
row_locator.filter(has_text="Mary").filter(
has=page.get_by_role("button", name="Say goodbye")
).screenshot(path="screenshot.png")
```
```java

Просмотреть файл

@ -60,24 +60,23 @@ This version was also tested against the following stable channels:
Consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead.
In this case, you can wait for either a "New email" button, or a dialog and act accordingly:
```python
new_email = page.get_by_role("button", name="New email")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).is_visible()
if (dialog.is_visible())
page.get_by_role("button", name="Dismiss").click()
new_email.click()
```
```python
new_email = page.get_by_role("button", name="New email")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).is_visible()
if (dialog.is_visible()):
page.get_by_role("button", name="Dismiss").click()
new_email.click()
```
* Use new options [`option: hasNot`] and [`option: hasNotText`] in [`method: Locator.filter`]
to find elements that **do not match** certain conditions.
```python
row_locator = page.locator("tr")
row_locator
.filter(has_not_text="text in column 1")
.filter(has_not=page.get_by_role("button", name="column 2 button"))
.screenshot()
```
```python
row_locator = page.locator("tr")
row_locator.filter(has_not_text="text in column 1").filter(
has_not=page.get_by_role("button", name="column 2 button")
).screenshot()
```
* Use new web-first assertion [`method: LocatorAssertions.toBeAttached`] to ensure that the element
is present in the page's DOM. Do not confuse with the [`method: LocatorAssertions.toBeVisible`] that ensures that
element is both attached & visible.

Просмотреть файл

@ -65,6 +65,7 @@ The Playwright Pytest plugin is based on the concept of test fixtures such as th
from playwright.sync_api import Page
def test_basic_test(page: Page):
pass
# ...
```