Skip to content

Commit

Permalink
fix a bunch of minor issues found while fixing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
perrygoy committed Feb 20, 2024
1 parent f02e503 commit 079df43
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
2 changes: 1 addition & 1 deletion screenpy_playwright/questions/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def of(target: Target) -> Number:
return Number(target)

def describe(self) -> str:
"""Describe the Question.
"""Describe the Question in the present tense.
Returns:
A description of this Question.
Expand Down
2 changes: 1 addition & 1 deletion screenpy_playwright/questions/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def of_the(target: Target) -> Text:
return Text(target)

def describe(self) -> str:
"""Describe the Question.
"""Describe the Question in the present tense.
Returns:
A description of this Question.
Expand Down
7 changes: 4 additions & 3 deletions screenpy_playwright/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
if TYPE_CHECKING:
from playwright.sync_api import Locator
from screenpy import Actor
from typing_extensions import Self


class Target:
Expand All @@ -31,10 +32,10 @@ class Target:
locator: str | None
_description: str | None

@staticmethod
def the(name: str) -> Target:
@classmethod
def the(cls: type[Self], name: str) -> Self:
"""Provide a human-readable description of the target."""
return Target(name)
return cls(name)

def located_by(self, locator: str) -> Target:
"""Provide the Playwright locator which describes the element."""
Expand Down
31 changes: 14 additions & 17 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,47 @@

from screenpy_playwright import BrowseTheWebSynchronously, Click, Enter, Visit

from .useful_mocks import get_mocked_target_and_element
from .useful_mocks import get_mock_target_class, get_mocked_target_and_locator

FakeTarget = get_mock_target_class()
TARGET = FakeTarget()


class TestClick:
def test_can_be_instantiated(self) -> None:
target, _ = get_mocked_target_and_element()

c1 = Click(target)
c2 = Click.on_the(target)
c1 = Click(TARGET)
c2 = Click.on_the(TARGET)

assert isinstance(c1, Click)
assert isinstance(c2, Click)

def test_implements_protocol(self) -> None:
target, _ = get_mocked_target_and_element()

c = Click(target)
c = Click(TARGET)

assert isinstance(c, Describable)
assert isinstance(c, Performable)

def test_describe(self) -> None:
target, _ = get_mocked_target_and_element()
target = FakeTarget()
target._description = "The Holy Hand Grenade"

assert Click(target).describe() == f"Click on the {target}."

def test_perform_click(self, Tester: Actor) -> None:
target, element = get_mocked_target_and_element()
target, locator = get_mocked_target_and_locator()

Click.on_the(target).perform_as(Tester)

target.found_by.assert_called_once_with(Tester)
element.click.assert_called_once()
locator.click.assert_called_once()


class TestEnter:
def test_can_be_instantiated(self) -> None:
target, _ = get_mocked_target_and_element()

e1 = Enter("")
e2 = Enter.the_text("")
e3 = Enter.the_secret("")
e4 = Enter.the_text("").into_the(target)
e4 = Enter.the_text("").into_the(TARGET)

assert isinstance(e1, Enter)
assert isinstance(e2, Enter)
Expand All @@ -64,7 +61,7 @@ def test_implements_protocol(self) -> None:
assert isinstance(e, Performable)

def test_describe(self) -> None:
target, _ = get_mocked_target_and_element()
target = FakeTarget()
target._description = "Sir Robin ran away away, brave brave Sir Robin!"
text = "Sir Robin ran away!"

Expand All @@ -83,13 +80,13 @@ def test_complains_for_no_target(self, Tester: Actor) -> None:
Enter.the_text("").perform_as(Tester)

def test_perform_enter(self, Tester: Actor) -> None:
target, element = get_mocked_target_and_element()
target, locator = get_mocked_target_and_locator()
text = "I wanna be, the very best."

Enter.the_text(text).into_the(target).perform_as(Tester)

target.found_by.assert_called_once_with(Tester)
element.fill.assert_called_once_with(text)
locator.fill.assert_called_once_with(text)


class TestVisit:
Expand Down
24 changes: 9 additions & 15 deletions tests/test_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,52 +54,46 @@ def test_can_be_instantiated(self) -> None:
assert isinstance(n, Number)

def test_implements_protocol(self) -> None:
target, _ = get_mocked_target_and_element()

n = Number(target)
n = Number(TARGET)

assert isinstance(n, Answerable)
assert isinstance(n, Describable)

def test_describe(self) -> None:
target, _ = get_mocked_target_and_element()
target = FakeTarget()
target._description = "Somebody once told me"

assert Number.of(target).describe() == f"The number of {target}."

def test_ask_number(self, Tester: Actor) -> None:
target, element = get_mocked_target_and_element()
target, locator = get_mocked_target_and_locator()
num_elements = 10
element.count.return_value = num_elements
locator.count.return_value = num_elements

assert Number.of(target).answered_by(Tester) == num_elements


class TestText:
def test_can_be_instantiated(self) -> None:
target, _ = get_mocked_target_and_element()

t = Text.of_the(target)
t = Text.of_the(TARGET)

assert isinstance(t, Text)

def test_implements_protocol(self) -> None:
target, _ = get_mocked_target_and_element()

t = Text(target)
t = Text(TARGET)

assert isinstance(t, Answerable)
assert isinstance(t, Describable)

def test_describe(self) -> None:
target, _ = get_mocked_target_and_element()
target = FakeTarget()
target._description = "the world is gonna roll me"

assert Text.of_the(target).describe() == f"The text from the {target}."

def test_ask_text(self, Tester: Actor) -> None:
target, element = get_mocked_target_and_element()
target, locator = get_mocked_target_and_locator()
words = "Number 1, the larch."
element.text_content.return_value = words
locator.text_content.return_value = words

assert Text.of_the(target).answered_by(Tester) == words

0 comments on commit 079df43

Please sign in to comment.