-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
====================== | ||
Additional Resolutions | ||
====================== | ||
|
||
These are the Resolutions added in ScreenPy Playwright. | ||
|
||
.. module:: screenpy_playwright.resolutions | ||
|
||
|
||
IsVisible | ||
--------- | ||
|
||
.. autoclass:: IsVisible | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Resolutions provide answers to the Actor's Questions.""" | ||
|
||
from .is_visible import IsVisible | ||
|
||
# Natural-language-enabling aliases | ||
Visible = IsVisible | ||
|
||
__all__ = [ | ||
"IsVisible", | ||
"Visible", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Custom matchers for ScreenPy: Playwright Resolutions.""" | ||
|
||
from .is_visible_element import is_visible_element | ||
|
||
__all__ = ["is_visible_element"] |
48 changes: 48 additions & 0 deletions
48
screenpy_playwright/resolutions/custom_matchers/is_visible_element.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
A matcher that matches a visible element. | ||
For example: | ||
assert_that(driver.find_element_by_id("search"), is_visible_element()) | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
from hamcrest.core.base_matcher import BaseMatcher | ||
from playwright.sync_api import Locator | ||
|
||
if TYPE_CHECKING: | ||
from hamcrest.core.description import Description | ||
|
||
|
||
class IsVisibleElement(BaseMatcher[Optional[Locator]]): | ||
"""Matches an element whose ``is_visible`` method returns True.""" | ||
|
||
def _matches(self, item: Locator | None) -> bool: | ||
if item is None: | ||
return False | ||
return item.is_visible() | ||
|
||
def describe_to(self, description: Description) -> None: | ||
"""Describe the passing case.""" | ||
description.append_text("the element is visible") | ||
|
||
def describe_match(self, _: Locator | None, match_description: Description) -> None: | ||
"""Describe the matching case.""" | ||
match_description.append_text("it was visible") | ||
|
||
def describe_mismatch( | ||
self, item: Locator | None, mismatch_description: Description | ||
) -> None: | ||
"""Describe the failing case.""" | ||
if item is None: | ||
mismatch_description.append_text("was not even present") | ||
return | ||
mismatch_description.append_text("was not visible") | ||
|
||
|
||
def is_visible_element() -> IsVisibleElement: | ||
"""This matcher matches any element that is visible.""" | ||
return IsVisibleElement() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"""Matches against a visible WebElement.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from screenpy import beat | ||
|
||
from .custom_matchers import is_visible_element | ||
|
||
if TYPE_CHECKING: | ||
from .custom_matchers.is_visible_element import IsVisibleElement | ||
|
||
|
||
class IsVisible: | ||
"""Match on a visible element. | ||
Examples:: | ||
the_actor.should(See.the(Element(WELCOME_BANNER), IsVisible())) | ||
""" | ||
|
||
def describe(self) -> str: | ||
"""Describe the Resolution's expectation.""" | ||
return "visible" | ||
|
||
@beat("... hoping it's visible.") | ||
def resolve(self) -> IsVisibleElement: | ||
"""Produce the Matcher to make the assertion.""" | ||
return is_visible_element() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from screenpy_playwright import IsVisible | ||
from screenpy_playwright.resolutions.custom_matchers.is_visible_element import ( | ||
IsVisibleElement, | ||
) | ||
|
||
from .useful_mocks import get_mocked_locator | ||
|
||
|
||
class TestIsVisible: | ||
def test_can_be_instantiated(self) -> None: | ||
iv = IsVisible() | ||
|
||
assert isinstance(iv, IsVisible) | ||
|
||
def test_describe(self) -> None: | ||
assert IsVisible().describe() == "visible" | ||
|
||
def test_resolve(self) -> None: | ||
assert isinstance(IsVisible().resolve(), IsVisibleElement) | ||
|
||
def test_the_test(self) -> None: | ||
locator = get_mocked_locator() | ||
locator.is_visible.return_value = True | ||
|
||
assert IsVisible().resolve().matches(locator) | ||
|
||
def test_the_test_fails(self) -> None: | ||
locator = get_mocked_locator() | ||
locator.is_visible.return_value = False | ||
|
||
assert not IsVisible().resolve().matches(locator) | ||
|
||
def test_the_test_fails_for_none(self) -> None: | ||
assert not IsVisible().resolve().matches(None) |