Skip to content

chore: add sync assertions #1047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H

| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->98.0.4695.0<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| Chromium <!-- GEN:chromium-version -->98.0.4714.0<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| WebKit <!-- GEN:webkit-version -->15.4<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| Firefox <!-- GEN:firefox-version -->94.0.1<!-- GEN:stop --> | ✅ | ✅ | ✅ |

Expand Down
13 changes: 7 additions & 6 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AssertionsBase:
def __init__(self, locator: Locator, is_not: bool = False) -> None:
self._actual_locator = locator
self._loop = locator._loop
self._dispatcher_fiber = locator._dispatcher_fiber
self._is_not = is_not

async def _expect_impl(
Expand Down Expand Up @@ -114,7 +115,7 @@ def _not(self) -> "LocatorAssertions":

async def to_contain_text(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
use_inner_text: bool = None,
timeout: float = None,
) -> None:
Expand Down Expand Up @@ -150,7 +151,7 @@ async def to_contain_text(

async def not_to_contain_text(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
use_inner_text: bool = None,
timeout: float = None,
) -> None:
Expand Down Expand Up @@ -185,7 +186,7 @@ async def not_to_have_attribute(

async def to_have_class(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
timeout: float = None,
) -> None:
__tracebackhide__ = True
Expand All @@ -208,7 +209,7 @@ async def to_have_class(

async def not_to_have_class(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
timeout: float = None,
) -> None:
__tracebackhide__ = True
Expand Down Expand Up @@ -332,7 +333,7 @@ async def not_to_have_value(

async def to_have_text(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
use_inner_text: bool = None,
timeout: float = None,
) -> None:
Expand Down Expand Up @@ -368,7 +369,7 @@ async def to_have_text(

async def not_to_have_text(
self,
expected: Union[List[Pattern], List[str], Pattern, str],
expected: Union[List[Union[Pattern, str]], Pattern, str],
use_inner_text: bool = None,
timeout: float = None,
) -> None:
Expand Down
24 changes: 12 additions & 12 deletions playwright/async_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14537,7 +14537,7 @@ class LocatorAssertions(AsyncBase):
async def to_contain_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14552,7 +14552,7 @@ async def to_contain_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand All @@ -14573,7 +14573,7 @@ async def to_contain_text(
async def not_to_contain_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14585,7 +14585,7 @@ async def not_to_contain_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand Down Expand Up @@ -14668,7 +14668,7 @@ async def not_to_have_attribute(
async def to_have_class(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
timeout: float = None
Expand All @@ -14681,7 +14681,7 @@ async def to_have_class(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected class or RegExp or a list of those.
timeout : Union[float, NoneType]
Time to retry the assertion for.
Expand All @@ -14698,7 +14698,7 @@ async def to_have_class(
async def not_to_have_class(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
timeout: float = None
Expand All @@ -14709,7 +14709,7 @@ async def not_to_have_class(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected class or RegExp or a list of those.
timeout : Union[float, NoneType]
Time to retry the assertion for.
Expand Down Expand Up @@ -14974,7 +14974,7 @@ async def not_to_have_value(
async def to_have_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14988,7 +14988,7 @@ async def to_have_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand All @@ -15009,7 +15009,7 @@ async def to_have_text(
async def not_to_have_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -15021,7 +15021,7 @@ async def not_to_have_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand Down
27 changes: 27 additions & 0 deletions playwright/sync_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
web automation that is ever-green, capable, reliable and fast.
"""

from typing import Union, overload

import playwright._impl._api_structures
import playwright._impl._api_types
import playwright.sync_api._generated
from playwright._impl._assertions import LocatorAssertions as LocatorAssertionsImpl
from playwright._impl._assertions import PageAssertions as PageAssertionsImpl
from playwright.sync_api._context_manager import PlaywrightContextManager
from playwright.sync_api._generated import (
Accessibility,
Expand All @@ -40,8 +44,10 @@
JSHandle,
Keyboard,
Locator,
LocatorAssertions,
Mouse,
Page,
PageAssertions,
Playwright,
Request,
Response,
Expand Down Expand Up @@ -76,7 +82,28 @@ def sync_playwright() -> PlaywrightContextManager:
return PlaywrightContextManager()


@overload
def expect(page_or_locator: Page) -> PageAssertions:
...


@overload
def expect(page_or_locator: Locator) -> LocatorAssertions:
...


def expect(
page_or_locator: Union[Page, Locator]
) -> Union[PageAssertions, LocatorAssertions]:
if isinstance(page_or_locator, Page):
return PageAssertions(PageAssertionsImpl(page_or_locator._impl_obj))
elif isinstance(page_or_locator, Locator):
return LocatorAssertions(LocatorAssertionsImpl(page_or_locator._impl_obj))
raise ValueError(f"Unsupported type: {type(page_or_locator)}")


__all__ = [
"expect",
"Accessibility",
"APIRequest",
"APIRequestContext",
Expand Down
24 changes: 12 additions & 12 deletions playwright/sync_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14260,7 +14260,7 @@ class LocatorAssertions(SyncBase):
def to_contain_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14275,7 +14275,7 @@ def to_contain_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand All @@ -14296,7 +14296,7 @@ def to_contain_text(
def not_to_contain_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14308,7 +14308,7 @@ def not_to_contain_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand Down Expand Up @@ -14391,7 +14391,7 @@ def not_to_have_attribute(
def to_have_class(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
timeout: float = None
Expand All @@ -14404,7 +14404,7 @@ def to_have_class(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected class or RegExp or a list of those.
timeout : Union[float, NoneType]
Time to retry the assertion for.
Expand All @@ -14421,7 +14421,7 @@ def to_have_class(
def not_to_have_class(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
timeout: float = None
Expand All @@ -14432,7 +14432,7 @@ def not_to_have_class(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected class or RegExp or a list of those.
timeout : Union[float, NoneType]
Time to retry the assertion for.
Expand Down Expand Up @@ -14697,7 +14697,7 @@ def not_to_have_value(
def to_have_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14711,7 +14711,7 @@ def to_have_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand All @@ -14732,7 +14732,7 @@ def to_have_text(
def not_to_have_text(
self,
expected: typing.Union[
typing.List[typing.Pattern], typing.List[str], typing.Pattern, str
typing.List[typing.Union[typing.Pattern, str]], typing.Pattern, str
],
*,
use_inner_text: bool = None,
Expand All @@ -14744,7 +14744,7 @@ def not_to_have_text(

Parameters
----------
expected : Union[List[Pattern], List[str], Pattern, str]
expected : Union[List[Union[Pattern, str]], Pattern, str]
Expected substring or RegExp or a list of those.
use_inner_text : Union[bool, NoneType]
Whether to use `element.innerText` instead of `element.textContent` when retrieving DOM node text.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
InWheel = None
from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand

driver_version = "1.18.0-alpha-nov-18-2021"
driver_version = "1.18.0-alpha-1637602750000"


def extractall(zip: zipfile.ZipFile, path: str) -> None:
Expand Down
6 changes: 4 additions & 2 deletions tests/async/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ async def test_accessibility_should_work_an_input(page):
}


async def test_accessibility_should_work_on_a_menu(page, is_webkit):
async def test_accessibility_should_work_on_a_menu(
page, is_webkit, is_chromium, browser_channel
):
await page.set_content(
"""
<div role="menu" title="My Menu">
Expand All @@ -395,7 +397,7 @@ async def test_accessibility_should_work_on_a_menu(page, is_webkit):
{"role": "menuitem", "name": "Third Item"},
],
}
if is_webkit:
if is_webkit or (is_chromium and not browser_channel):
golden["orientation"] = "vertical"
assert await page.accessibility.snapshot(root=menu) == golden

Expand Down
6 changes: 4 additions & 2 deletions tests/sync/test_accessibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,9 @@ def test_accessibility_should_work_an_input(page: Page) -> None:
}


def test_accessibility_should_work_on_a_menu(page: Page, is_webkit: bool) -> None:
def test_accessibility_should_work_on_a_menu(
page: Page, is_webkit: bool, is_chromium: str, browser_channel: str
) -> None:
page.set_content(
"""
<div role="menu" title="My Menu">
Expand All @@ -409,7 +411,7 @@ def test_accessibility_should_work_on_a_menu(page: Page, is_webkit: bool) -> Non
{"role": "menuitem", "name": "Third Item"},
],
}
if is_webkit:
if is_webkit or (is_chromium and not browser_channel):
golden["orientation"] = "vertical"
assert page.accessibility.snapshot(root=menu) == golden

Expand Down
Loading