Skip to content

Commit

Permalink
chore: fix tests/async pyright linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Nov 28, 2023
1 parent 8705b35 commit e2fe579
Show file tree
Hide file tree
Showing 40 changed files with 303 additions and 175 deletions.
18 changes: 10 additions & 8 deletions tests/async/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import pytest

from playwright.async_api import Page


async def test_accessibility_should_work(page, is_firefox, is_chromium):
await page.set_content(
Expand Down Expand Up @@ -118,35 +120,35 @@ async def test_accessibility_should_work_with_regular_text(page, is_firefox):
}


async def test_accessibility_roledescription(page):
async def test_accessibility_roledescription(page: Page) -> None:
await page.set_content('<p tabIndex=-1 aria-roledescription="foo">Hi</p>')
snapshot = await page.accessibility.snapshot()
assert snapshot["children"][0]["roledescription"] == "foo"


async def test_accessibility_orientation(page):
async def test_accessibility_orientation(page: Page) -> None:
await page.set_content(
'<a href="" role="slider" aria-orientation="vertical">11</a>'
)
snapshot = await page.accessibility.snapshot()
assert snapshot["children"][0]["orientation"] == "vertical"


async def test_accessibility_autocomplete(page):
async def test_accessibility_autocomplete(page: Page) -> None:
await page.set_content('<div role="textbox" aria-autocomplete="list">hi</div>')
snapshot = await page.accessibility.snapshot()
assert snapshot["children"][0]["autocomplete"] == "list"


async def test_accessibility_multiselectable(page):
async def test_accessibility_multiselectable(page: Page) -> None:
await page.set_content(
'<div role="grid" tabIndex=-1 aria-multiselectable=true>hey</div>'
)
snapshot = await page.accessibility.snapshot()
assert snapshot["children"][0]["multiselectable"]


async def test_accessibility_keyshortcuts(page):
async def test_accessibility_keyshortcuts(page: Page) -> None:
await page.set_content(
'<div role="grid" tabIndex=-1 aria-keyshortcuts="foo">hey</div>'
)
Expand Down Expand Up @@ -292,7 +294,7 @@ async def test_accessibility_checkbox_without_label_should_not_have_children(
assert snapshot["children"][0] == golden


async def test_accessibility_should_work_a_button(page):
async def test_accessibility_should_work_a_button(page: Page) -> None:
await page.set_content("<button>My Button</button>")

button = await page.query_selector("button")
Expand All @@ -302,7 +304,7 @@ async def test_accessibility_should_work_a_button(page):
}


async def test_accessibility_should_work_an_input(page):
async def test_accessibility_should_work_an_input(page: Page) -> None:
await page.set_content('<input title="My Input" value="My Value">')

input = await page.query_selector("input")
Expand Down Expand Up @@ -353,7 +355,7 @@ async def test_accessibility_should_return_null_when_the_element_is_no_longer_in
assert await page.accessibility.snapshot(root=button) is None


async def test_accessibility_should_show_uninteresting_nodes(page):
async def test_accessibility_should_show_uninteresting_nodes(page: Page) -> None:
await page.set_content(
"""
<div id="root" role="textbox">
Expand Down
19 changes: 11 additions & 8 deletions tests/async/test_add_init_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from playwright.async_api import Error
from typing import Optional

from playwright.async_api import Error, Page

async def test_add_init_script_evaluate_before_anything_else_on_the_page(page):

async def test_add_init_script_evaluate_before_anything_else_on_the_page(page: Page):
await page.add_init_script("window.injected = 123")
await page.goto("data:text/html,<script>window.result = window.injected</script>")
assert await page.evaluate("window.result") == 123
Expand All @@ -27,18 +29,19 @@ async def test_add_init_script_work_with_a_path(page, assetdir):
assert await page.evaluate("window.result") == 123


async def test_add_init_script_work_with_content(page):
async def test_add_init_script_work_with_content(page: Page):
await page.add_init_script("window.injected = 123")
await page.goto("data:text/html,<script>window.result = window.injected</script>")
assert await page.evaluate("window.result") == 123


async def test_add_init_script_throw_without_path_and_content(page):
error = None
async def test_add_init_script_throw_without_path_and_content(page: Page):
error: Optional[Error] = None
try:
await page.add_init_script({"foo": "bar"})
await page.add_init_script({"foo": "bar"}) # type: ignore
except Error as e:
error = e
assert error
assert error.message == "Either path or script parameter must be specified"


Expand Down Expand Up @@ -68,15 +71,15 @@ async def test_add_init_script_work_with_browser_context_scripts_for_already_cre
assert await page.evaluate("window.result") == 123


async def test_add_init_script_support_multiple_scripts(page):
async def test_add_init_script_support_multiple_scripts(page: Page):
await page.add_init_script("window.script1 = 1")
await page.add_init_script("window.script2 = 2")
await page.goto("data:text/html,<script>window.result = window.injected</script>")
assert await page.evaluate("window.script1") == 1
assert await page.evaluate("window.script2") == 2


async def test_should_work_with_trailing_comments(page):
async def test_should_work_with_trailing_comments(page: Page):
await page.add_init_script("// comment")
await page.add_init_script("window.secret = 42;")
await page.goto("data:text/html,<html></html>")
Expand Down
3 changes: 2 additions & 1 deletion tests/async/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ async def test_cancel_pending_protocol_call_on_playwright_stop(server: Server) -


async def test_should_collect_stale_handles(page: Page, server: Server) -> None:
page.on("request", lambda: None)
page.on("request", lambda _: None)
response = await page.goto(server.PREFIX + "/title.html")
assert response
for i in range(1000):
await page.evaluate(
"""async () => {
Expand Down
9 changes: 5 additions & 4 deletions tests/async/test_browsercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@

import asyncio
import re
from typing import List
from urllib.parse import urlparse

import pytest

from playwright.async_api import Browser, Error
from playwright.async_api import Browser, Error, Page
from tests.server import Server
from tests.utils import TARGET_CLOSED_ERROR_MESSAGE

Expand Down Expand Up @@ -772,11 +773,11 @@ async def test_page_event_should_have_an_opener(context, server):


async def test_page_event_should_fire_page_lifecycle_events(context, server):
events = []
events: List[str] = []

def handle_page(page):
def handle_page(page: Page):
events.append("CREATED: " + page.url)
page.on("close", lambda: events.append("DESTROYED: " + page.url))
page.on("close", lambda _: events.append("DESTROYED: " + page.url))

context.on("page", handle_page)

Expand Down
14 changes: 8 additions & 6 deletions tests/async/test_browsercontext_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

import asyncio
from typing import Optional

import pytest

from playwright.sync_api import Page
from playwright.async_api import Page

from ..server import HttpRequestWithPostBody, Server
from .utils import must


async def test_console_event_should_work(page: Page) -> None:
Expand Down Expand Up @@ -86,7 +88,7 @@ async def test_console_event_should_work_in_immediately_closed_popup(


async def test_dialog_event_should_work1(page: Page) -> None:
prompt_task = None
prompt_task: Optional[asyncio.Future[str]] = None

async def open_dialog() -> None:
nonlocal prompt_task
Expand All @@ -101,11 +103,11 @@ async def open_dialog() -> None:
assert dialog1.message == "hey?"
assert dialog1.page == page
await dialog1.accept("hello")
assert await prompt_task == "hello"
assert await must(prompt_task) == "hello"


async def test_dialog_event_should_work_in_popup(page: Page) -> None:
prompt_task = None
prompt_task: Optional[asyncio.Future[str]] = None

async def open_dialog() -> None:
nonlocal prompt_task
Expand All @@ -121,7 +123,7 @@ async def open_dialog() -> None:
assert dialog.message == "hey?"
assert dialog.page == popup
await dialog.accept("hello")
assert await prompt_task == "hello"
assert await must(prompt_task) == "hello"


# console message from javascript: url is not reported at all
Expand Down Expand Up @@ -179,7 +181,7 @@ def handle_route(request: HttpRequestWithPostBody) -> None:
assert dialog.page == popup
await dialog.accept("hello")
await promise
await popup.evaluate("window.result") == "hello"
assert await popup.evaluate("window.result") == "hello"


async def test_console_event_should_work_with_context_manager(page: Page) -> None:
Expand Down
5 changes: 4 additions & 1 deletion tests/async/test_browsercontext_request_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
from typing import Any, cast

import pytest

Expand Down Expand Up @@ -90,6 +91,7 @@ async def test_should_chain_once(
)

resp = await page.goto(server.PREFIX + "/madeup.txt")
assert resp
body = await resp.body()
assert body == b"fulfilled one"

Expand All @@ -112,6 +114,7 @@ def handler(route: Route):
)

response = await page.goto(server.EMPTY_PAGE)
assert response
body = await response.body()
assert body == b"fulfilled"
assert not failed[0]
Expand Down Expand Up @@ -155,7 +158,7 @@ async def test_should_fall_back_after_exception(

async def handler(route: Route):
try:
await route.fulfill(response=47)
await route.fulfill(response=cast(Any, {}))
except Exception:
await route.fallback()

Expand Down
5 changes: 5 additions & 0 deletions tests/async/test_browsercontext_request_intercept.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async def handle(route: Route):

await context.route("**/*", handle)
response = await page.goto(server.PREFIX + "/empty.html")
assert response
assert response.status == 201
assert response.headers["foo"] == "bar"
assert response.headers["content-type"] == "text/plain"
Expand All @@ -53,6 +54,7 @@ async def handle(route: Route):

await context.route("**/*", handle)
response = await page.goto(server.PREFIX + "/title.html")
assert response
assert response.status == 201
assert await response.text() == ""

Expand All @@ -73,6 +75,7 @@ async def handle(route: Route):

await context.route("**/*", handle)
response = await page.goto(server.EMPTY_PAGE)
assert response
assert response.status == 201
assert await response.text() == ""
if browser_name == "webkit":
Expand All @@ -98,6 +101,7 @@ def server_handler(request: http.Request):
),
)
response = await page.goto(server.EMPTY_PAGE)
assert response
assert response.status == 201
assert await response.text() == "Woo-hoo"
assert response.headers["foo"] == "bar"
Expand All @@ -114,6 +118,7 @@ async def handle_route(route: Route):

await context.route("**", handle_route)
response = await page.goto(server.PREFIX + "/title.html")
assert response
request = await request_future
assert request.uri.decode() == "/title.html"
original = (assetdir / "title.html").read_text()
Expand Down
8 changes: 5 additions & 3 deletions tests/async/test_browsertype_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ async def test_browser_type_connect_disconnected_event_should_be_emitted_when_br

disconnected1 = []
disconnected2 = []
browser1.on("disconnected", lambda: disconnected1.append(True))
browser2.on("disconnected", lambda: disconnected2.append(True))
browser1.on("disconnected", lambda _: disconnected1.append(True))
browser2.on("disconnected", lambda _: disconnected2.append(True))

page2 = await browser2.new_page()

Expand All @@ -108,7 +108,7 @@ async def test_browser_type_connect_disconnected_event_should_be_emitted_when_re
browser = await browser_type.connect(remote.ws_endpoint)

disconnected = []
browser.on("disconnected", lambda: disconnected.append(True))
browser.on("disconnected", lambda _: disconnected.append(True))
page = await browser.new_page()
remote.kill()
with pytest.raises(Error):
Expand Down Expand Up @@ -295,11 +295,13 @@ async def test_should_upload_large_file(
assert contents[:1024] == data
# flake8: noqa: E203
assert contents[len(contents) - 1024 :] == data
assert request.post_body
match = re.search(
rb'^.*Content-Disposition: form-data; name="(?P<name>.*)"; filename="(?P<filename>.*)".*$',
request.post_body,
re.MULTILINE,
)
assert match
assert match.group("name") == b"file1"
assert match.group("filename") == b"200MB.zip"

Expand Down
Loading

0 comments on commit e2fe579

Please sign in to comment.