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 29, 2023
1 parent 8705b35 commit 886feb5
Show file tree
Hide file tree
Showing 80 changed files with 3,234 additions and 1,941 deletions.
49 changes: 35 additions & 14 deletions tests/async/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,60 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import asyncio
from typing import Any, AsyncGenerator, Awaitable, Callable, Dict, Generator, List

from playwright.async_api import async_playwright
import pytest

from playwright.async_api import (
Browser,
BrowserContext,
BrowserType,
Page,
Playwright,
Selectors,
async_playwright,
)

from .utils import Utils
from .utils import utils as utils_object


@pytest.fixture
def utils():
def utils() -> Generator[Utils, None, None]:
yield utils_object


# Will mark all the tests as async
def pytest_collection_modifyitems(items):
def pytest_collection_modifyitems(items: List[pytest.Item]) -> None:
for item in items:
item.add_marker(pytest.mark.asyncio)


@pytest.fixture(scope="session")
async def playwright():
async def playwright() -> AsyncGenerator[Playwright, None]:
async with async_playwright() as playwright_object:
yield playwright_object


@pytest.fixture(scope="session")
def browser_type(playwright, browser_name: str):
def browser_type(playwright: Playwright, browser_name: str) -> BrowserType:
if browser_name == "chromium":
return playwright.chromium
if browser_name == "firefox":
return playwright.firefox
if browser_name == "webkit":
return playwright.webkit
raise Exception(f"Invalid browser_name: {browser_name}")


@pytest.fixture(scope="session")
async def browser_factory(launch_arguments, browser_type):
async def browser_factory(
launch_arguments: Dict, browser_type: BrowserType
) -> AsyncGenerator[Callable[..., Awaitable[Browser]], None]:
browsers = []

async def launch(**kwargs):
async def launch(**kwargs: Any) -> Browser:
browser = await browser_type.launch(**launch_arguments, **kwargs)
browsers.append(browser)
return browser
Expand All @@ -61,17 +76,21 @@ async def launch(**kwargs):


@pytest.fixture(scope="session")
async def browser(browser_factory):
async def browser(
browser_factory: "Callable[..., asyncio.Future[Browser]]",
) -> AsyncGenerator[Browser, None]:
browser = await browser_factory()
yield browser
await browser.close()


@pytest.fixture
async def context_factory(browser):
async def context_factory(
browser: Browser,
) -> AsyncGenerator["Callable[..., Awaitable[BrowserContext]]", None]:
contexts = []

async def launch(**kwargs):
async def launch(**kwargs: Any) -> BrowserContext:
context = await browser.new_context(**kwargs)
contexts.append(context)
return context
Expand All @@ -82,19 +101,21 @@ async def launch(**kwargs):


@pytest.fixture
async def context(context_factory):
async def context(
context_factory: "Callable[..., asyncio.Future[BrowserContext]]",
) -> AsyncGenerator[BrowserContext, None]:
context = await context_factory()
yield context
await context.close()


@pytest.fixture
async def page(context):
async def page(context: BrowserContext) -> AsyncGenerator[Page, None]:
page = await context.new_page()
yield page
await page.close()


@pytest.fixture(scope="session")
def selectors(playwright):
def selectors(playwright: Playwright) -> Selectors:
return playwright.selectors
75 changes: 46 additions & 29 deletions tests/async/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

import pytest

from playwright.async_api import Page

async def test_accessibility_should_work(page, is_firefox, is_chromium):

async def test_accessibility_should_work(
page: Page, is_firefox: bool, is_chromium: bool
) -> None:
await page.set_content(
"""<head>
<title>Accessibility Test</title>
Expand Down Expand Up @@ -109,54 +113,62 @@ async def test_accessibility_should_work(page, is_firefox, is_chromium):
assert await page.accessibility.snapshot() == golden


async def test_accessibility_should_work_with_regular_text(page, is_firefox):
async def test_accessibility_should_work_with_regular_text(
page: Page, is_firefox: bool
) -> None:
await page.set_content("<div>Hello World</div>")
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == {
"role": "text leaf" if is_firefox else "text",
"name": "Hello World",
}


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
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
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
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
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>'
)
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0]["keyshortcuts"] == "foo"


async def test_accessibility_filtering_children_of_leaf_nodes_should_not_report_text_nodes_inside_controls(
page, is_firefox
):
page: Page, is_firefox: bool
) -> None:
await page.set_content(
"""
<div role="tablist">
Expand All @@ -179,13 +191,14 @@ async def test_accessibility_filtering_children_of_leaf_nodes_should_not_report_
# WebKit rich text accessibility is iffy
@pytest.mark.only_browser("chromium")
async def test_accessibility_plain_text_field_with_role_should_not_have_children(
page, browser_channel
):
page: Page,
) -> None:
await page.set_content(
"""
<div contenteditable="plaintext-only" role='textbox'>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == {
"multiline": True,
"name": "",
Expand All @@ -196,13 +209,14 @@ async def test_accessibility_plain_text_field_with_role_should_not_have_children

@pytest.mark.only_browser("chromium")
async def test_accessibility_plain_text_field_without_role_should_not_have_content(
page, browser_channel
):
page: Page,
) -> None:
await page.set_content(
"""
<div contenteditable="plaintext-only">Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == {
"name": "",
"role": "generic",
Expand All @@ -212,13 +226,14 @@ async def test_accessibility_plain_text_field_without_role_should_not_have_conte

@pytest.mark.only_browser("chromium")
async def test_accessibility_plain_text_field_with_tabindex_and_without_role_should_not_have_content(
page, browser_channel
):
page: Page,
) -> None:
await page.set_content(
"""
<div contenteditable="plaintext-only" tabIndex=0>Edit this image:<img src="fakeimage.png" alt="my fake image"></div>"""
)
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == {
"name": "",
"role": "generic",
Expand All @@ -227,8 +242,8 @@ async def test_accessibility_plain_text_field_with_tabindex_and_without_role_sho


async def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_label_should_not_have_children(
page, is_chromium, is_firefox
):
page: Page, is_chromium: bool, is_firefox: bool
) -> None:
await page.set_content(
"""
<div role="textbox" tabIndex=0 aria-checked="true" aria-label="my favorite textbox">
Expand All @@ -255,12 +270,13 @@ async def test_accessibility_non_editable_textbox_with_role_and_tabIndex_and_lab
"value": "this is the inner content ",
}
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == golden


async def test_accessibility_checkbox_with_and_tabIndex_and_label_should_not_have_children(
page,
):
page: Page,
) -> None:
await page.set_content(
"""
<div role="checkbox" tabIndex=0 aria-checked="true" aria-label="my favorite checkbox">
Expand All @@ -270,12 +286,13 @@ async def test_accessibility_checkbox_with_and_tabIndex_and_label_should_not_hav
)
golden = {"role": "checkbox", "name": "my favorite checkbox", "checked": True}
snapshot = await page.accessibility.snapshot()
assert snapshot
assert snapshot["children"][0] == golden


async def test_accessibility_checkbox_without_label_should_not_have_children(
page, is_firefox
):
page: Page, is_firefox: bool
) -> None:
await page.set_content(
"""
<div role="checkbox" aria-checked="true">
Expand All @@ -289,10 +306,11 @@ async def test_accessibility_checkbox_without_label_should_not_have_children(
"checked": True,
}
snapshot = await page.accessibility.snapshot()
assert snapshot
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 +320,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 All @@ -313,9 +331,7 @@ async def test_accessibility_should_work_an_input(page):
}


async def test_accessibility_should_work_on_a_menu(
page, is_webkit, is_chromium, browser_channel
):
async def test_accessibility_should_work_on_a_menu(page: Page) -> None:
await page.set_content(
"""
<div role="menu" title="My Menu">
Expand Down Expand Up @@ -345,15 +361,15 @@ async def test_accessibility_should_work_on_a_menu(


async def test_accessibility_should_return_null_when_the_element_is_no_longer_in_DOM(
page,
):
page: Page,
) -> None:
await page.set_content("<button>My Button</button>")
button = await page.query_selector("button")
await page.eval_on_selector("button", "button => button.remove()")
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 All @@ -369,6 +385,7 @@ async def test_accessibility_should_show_uninteresting_nodes(page):

root = await page.query_selector("#root")
snapshot = await page.accessibility.snapshot(root=root, interesting_only=False)
assert snapshot
assert snapshot["role"] == "textbox"
assert "hello" in snapshot["value"]
assert "world" in snapshot["value"]
Expand Down
Loading

0 comments on commit 886feb5

Please sign in to comment.