diff --git a/playwright/_impl/_assertions.py b/playwright/_impl/_assertions.py
index c75814df5..47b4e2d8b 100644
--- a/playwright/_impl/_assertions.py
+++ b/playwright/_impl/_assertions.py
@@ -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:
@@ -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,
)
diff --git a/tests/async/test_assertions.py b/tests/async/test_assertions.py
index 0d974543a..3fd403ac1 100644
--- a/tests/async/test_assertions.py
+++ b/tests/async/test_assertions.py
@@ -1179,3 +1179,23 @@ async def test_soft_does_not_leak_between_scopes(page: Page, server: Server) ->
await page.set_content("
hello
")
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(
+ """
+ secret
+ Page Heading
+ """
+ )
+ 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:")
diff --git a/tests/sync/test_assertions.py b/tests/sync/test_assertions.py
index a3b98ed1d..ed58cdcdd 100644
--- a/tests/sync/test_assertions.py
+++ b/tests/sync/test_assertions.py
@@ -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(
+ """
+ secret
+ Page Heading
+ """
+ )
+ 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:")