Skip to content

Commit 92bb1d0

Browse files
authored
chore: custom expect messages follow-up (#1780)
1 parent 29fe478 commit 92bb1d0

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

playwright/_impl/_assertions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ async def _expect_impl(
5555
if log:
5656
log = "\nCall log:\n" + log
5757
if self._custom_message:
58-
out_message = (
59-
f"{self._custom_message}\nExpected value: {expected or '<None>'}"
60-
)
58+
out_message = self._custom_message
59+
if expected is not None:
60+
out_message += f"\nExpected value: '{expected or '<None>'}'"
6161
else:
6262
out_message = (
6363
f"{message} '{expected}'" if expected is not None else f"{message}"

tests/async/test_assertions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,20 @@ async def test_should_print_users_message_for_page_based_assertion(
671671
with pytest.raises(AssertionError) as excinfo:
672672
await expect(page).to_have_title("old title", timeout=100)
673673
assert "Page title expected to be" in str(excinfo.value)
674+
675+
676+
async def test_should_print_expected_value_with_custom_message(
677+
page: Page, server: Server
678+
) -> None:
679+
await page.goto(server.EMPTY_PAGE)
680+
await page.set_content("<title>new title</title>")
681+
with pytest.raises(AssertionError) as excinfo:
682+
await expect(page, "custom-message").to_have_title("old title", timeout=100)
683+
assert "custom-message" in str(excinfo.value)
684+
assert "Expected value: 'old title'" in str(excinfo.value)
685+
with pytest.raises(AssertionError) as excinfo:
686+
await expect(page.get_by_text("hello"), "custom-message").to_be_visible(
687+
timeout=100
688+
)
689+
assert "custom-message" in str(excinfo.value)
690+
assert "Expected value" not in str(excinfo.value)

tests/sync/test_assertions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,3 +759,18 @@ def test_should_print_users_message_for_page_based_assertion(
759759
with pytest.raises(AssertionError) as excinfo:
760760
expect(page).to_have_title("old title", timeout=100)
761761
assert "Page title expected to be" in str(excinfo.value)
762+
763+
764+
def test_should_print_expected_value_with_custom_message(
765+
page: Page, server: Server
766+
) -> None:
767+
page.goto(server.EMPTY_PAGE)
768+
page.set_content("<title>new title</title>")
769+
with pytest.raises(AssertionError) as excinfo:
770+
expect(page, "custom-message").to_have_title("old title", timeout=100)
771+
assert "custom-message" in str(excinfo.value)
772+
assert "Expected value: 'old title'" in str(excinfo.value)
773+
with pytest.raises(AssertionError) as excinfo:
774+
expect(page.get_by_text("hello"), "custom-message").to_be_visible(timeout=100)
775+
assert "custom-message" in str(excinfo.value)
776+
assert "Expected value" not in str(excinfo.value)

0 commit comments

Comments
 (0)