Skip to content

Commit 8f917d2

Browse files
committed
Fix assertion messages
1 parent d7750ae commit 8f917d2

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

playwright/_impl/_api_structures.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class FrameExpectResult(TypedDict):
218218
matches: bool
219219
received: Any
220220
log: List[str]
221+
errorMessage: Optional[str]
221222

222223

223224
AriaRole = Literal[

playwright/_impl/_assertions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ async def _expect_impl(
8080
out_message = (
8181
f"{message} '{expected}'" if expected is not None else f"{message}"
8282
)
83+
error_message = result.get("errorMessage")
84+
error_message = f"\n{error_message}" if error_message else ""
8385
raise AssertionError(
84-
f"{out_message}\nActual value: {actual} {format_call_log(result.get('log'))}"
86+
f"{out_message}\nActual value: {actual}{error_message} {format_call_log(result.get('log'))}"
8587
)
8688

8789

tests/async/test_assertions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ async def test_to_have_values_fails_when_multiple_not_specified(
516516
)
517517
locator = page.locator("select")
518518
await locator.select_option(["B"])
519-
with pytest.raises(Error) as excinfo:
519+
with pytest.raises(AssertionError) as excinfo:
520520
await expect(locator).to_have_values(["R", "G"], timeout=500)
521521
assert "Error: Not a select element with a multiple attribute" in str(excinfo.value)
522522

@@ -530,7 +530,7 @@ async def test_to_have_values_fails_when_not_a_select_element(
530530
"""
531531
)
532532
locator = page.locator("input")
533-
with pytest.raises(Error) as excinfo:
533+
with pytest.raises(AssertionError) as excinfo:
534534
await expect(locator).to_have_values(["R", "G"], timeout=500)
535535
assert "Error: Not a select element with a multiple attribute" in str(excinfo.value)
536536

@@ -564,7 +564,7 @@ async def test_assertions_boolean_checked_with_intermediate_true_and_checked(
564564
await page.set_content("<input type=checkbox></input>")
565565
await page.locator("input").evaluate("e => e.indeterminate = true")
566566
with pytest.raises(
567-
Error, match="Can't assert indeterminate and checked at the same time"
567+
AssertionError, match="Can't assert indeterminate and checked at the same time"
568568
):
569569
await expect(page.locator("input")).to_be_checked(
570570
checked=False, indeterminate=True
@@ -658,7 +658,7 @@ async def test_assertions_locator_to_be_editable_throws(
658658
await page.goto(server.EMPTY_PAGE)
659659
await page.set_content("<button disabled>Text</button>")
660660
with pytest.raises(
661-
Error,
661+
AssertionError,
662662
match=r"Element is not an <input>, <textarea>, <select> or \[contenteditable\] and does not have a role allowing \[aria-readonly\]",
663663
):
664664
await expect(page.locator("button")).not_to_be_editable()

tests/sync/test_assertions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ def test_to_have_values_fails_when_multiple_not_specified(
492492
)
493493
locator = page.locator("select")
494494
locator.select_option(["B"])
495-
with pytest.raises(Error) as excinfo:
495+
with pytest.raises(AssertionError) as excinfo:
496496
expect(locator).to_have_values(["R", "G"], timeout=500)
497497
assert "Error: Not a select element with a multiple attribute" in str(excinfo.value)
498498

@@ -506,7 +506,7 @@ def test_to_have_values_fails_when_not_a_select_element(
506506
"""
507507
)
508508
locator = page.locator("input")
509-
with pytest.raises(Error) as excinfo:
509+
with pytest.raises(AssertionError) as excinfo:
510510
expect(locator).to_have_values(["R", "G"], timeout=500)
511511
assert "Error: Not a select element with a multiple attribute" in str(excinfo.value)
512512

@@ -540,7 +540,7 @@ def test_assertions_boolean_checked_with_intermediate_true_and_checked(
540540
page.set_content("<input type=checkbox></input>")
541541
page.locator("input").evaluate("e => e.indeterminate = true")
542542
with pytest.raises(
543-
Error, match="Can't assert indeterminate and checked at the same time"
543+
AssertionError, match="Can't assert indeterminate and checked at the same time"
544544
):
545545
expect(page.locator("input")).to_be_checked(checked=False, indeterminate=True)
546546

@@ -626,7 +626,7 @@ def test_assertions_locator_to_be_editable_throws(page: Page, server: Server) ->
626626
page.goto(server.EMPTY_PAGE)
627627
page.set_content("<button disabled>Text</button>")
628628
with pytest.raises(
629-
Error,
629+
AssertionError,
630630
match=r"Element is not an <input>, <textarea>, <select> or \[contenteditable\] and does not have a role allowing \[aria-readonly\]",
631631
):
632632
expect(page.locator("button")).not_to_be_editable()

0 commit comments

Comments
 (0)