Skip to content
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
13 changes: 8 additions & 5 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ async def _expect_impl(
result = await self._call_expect(expression, expect_options, title)
if result["matches"] == self._is_not:
received = result.get("received") or {}
aria_snapshot = None
if isinstance(received, dict):
if "value" in received and received["value"] is not None:
actual = parse_value(received["value"])
else:
actual = received.get("ariaSnapshot")
aria_snapshot = received.get("ariaSnapshot")
value = received.get("value")
actual = parse_value(value) if value is not None else None
else:
actual = received
if self._custom_message:
Expand All @@ -120,9 +120,12 @@ async def _expect_impl(
)
error_message = result.get("errorMessage")
error_message = f"\n{error_message}" if error_message else ""
aria_snapshot_message = (
f"\nAria snapshot:\n{aria_snapshot}" if aria_snapshot else ""
)
_record_soft_or_raise(
AssertionError(
f"{out_message}\nActual value: {actual}{error_message} {format_call_log(result.get('log'))}"
f"{out_message}\nActual value: {actual}{error_message} {format_call_log(result.get('log'))}{aria_snapshot_message}"
),
self._is_soft,
)
Expand Down
20 changes: 20 additions & 0 deletions tests/async/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,3 +1179,23 @@ async def test_soft_does_not_leak_between_scopes(page: Page, server: Server) ->
await page.set_content("<div>hello</div>")
with pytest.raises(RuntimeError, match="pytest-playwright"):
await expect.soft(page.locator("div")).to_have_text("nope", timeout=500)


async def test_assertions_should_include_aria_snapshot_in_separate_section(
page: Page,
) -> None:
await page.set_content(
"""
<div id="hidden" style="display: none"><span>secret</span></div>
<main><h1>Page Heading</h1></main>
"""
)
with pytest.raises(AssertionError) as excinfo:
await expect(page.locator("#hidden")).to_be_visible(timeout=500)
message = str(excinfo.value)
assert "Aria snapshot:" in message
# The aria snapshot lives in its own section, not in the "Actual value" line.
assert "Actual value: hidden" in message
actual_line = message.split("\n")[1]
assert 'heading "Page Heading"' not in actual_line
assert message.index('heading "Page Heading"') > message.index("Aria snapshot:")
20 changes: 20 additions & 0 deletions tests/sync/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,3 +1089,23 @@ def test_soft_inside_scope_collects_failures(page: Page, server: Server) -> None

assert len(errors) == 3
assert all(isinstance(e, AssertionError) for e in errors)


def test_assertions_should_include_aria_snapshot_in_separate_section(
page: Page,
) -> None:
page.set_content(
"""
<div id="hidden" style="display: none"><span>secret</span></div>
<main><h1>Page Heading</h1></main>
"""
)
with pytest.raises(AssertionError) as excinfo:
expect(page.locator("#hidden")).to_be_visible(timeout=500)
message = str(excinfo.value)
assert "Aria snapshot:" in message
# The aria snapshot lives in its own section, not in the "Actual value" line.
assert "Actual value: hidden" in message
actual_line = message.split("\n")[1]
assert 'heading "Page Heading"' not in actual_line
assert message.index('heading "Page Heading"') > message.index("Aria snapshot:")
Loading