8000 fix(tracing): apiName determination with event listeners (#2651) · mxschmitt/playwright-python@1452881 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1452881

Browse files
authored
fix(tracing): apiName determination with event listeners (microsoft#2651)
1 parent c2dc664 commit 1452881

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

playwright/_impl/_connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from pyee.asyncio import AsyncIOEventEmitter
3838

3939
import playwright
40+
import playwright._impl._impl_to_api_mapping
4041
from playwright._impl._errors import TargetClosedError, rewrite_error
4142
from playwright._impl._greenlets import EventGreenlet
4243
from playwright._impl._helper import Error, ParsedMessagePayload, parse_error
@@ -573,6 +574,12 @@ def _extract_stack_trace_information_from_stack(
573574
api_name = ""
574575
parsed_frames: List[StackFrame] = []
575576
for frame in st:
577+
# Sync and Async implementations can have event handlers. When these are sync, they
578+
# get evaluated in the context of the event loop, so they contain the stack trace of when
579+
# the message was received. _impl_to_api_mapping is glue between the user-code and internal
580+
# code to translate impl classes to api classes. We want to ignore these frames.
581+
if playwright._impl._impl_to_api_mapping.__file__ == frame.filename:
582+
continue
576583
is_playwright_internal = frame.filename.startswith(playwright_module_path)
577584

578585
method_name = ""

tests/async/test_tracing.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import asyncio
1516
import re
1617
from pathlib import Path
1718
from typing import Dict, List
1819

19-
from playwright.async_api import Browser, BrowserContext, BrowserType, Page
20+
from playwright.async_api import Browser, BrowserContext, BrowserType, Page, Response
2021
from tests.server import Server
2122
from tests.utils import get_trace_actions, parse_trace
2223

@@ -145,6 +146,35 @@ async def test_should_collect_trace_with_resources_but_no_js(
145146
assert script["snapshot"]["response"]["content"].get("_sha1") is None
146147

147148

149+
async def test_should_correctly_determine_sync_apiname(
150+
context: BrowserContext, page: Page, server: Server, tmpdir: Path
151+
) -> None:
152+
await context.tracing.start(screenshots=True, snapshots=True)
153+
154+
received_response: "asyncio.Future[None]" = asyncio.Future()
155+
156+
async def _handle_response(response: Response) -> None:
157+
await response.request.all_headers()
158+
await response.text()
159+
received_response.set_result(None)
160+
161+
page.once("response", _handle_response)
162+
await page.goto(server.PREFIX + "/grid.html")
163+
await received_response
164+
await page.close()
165+
trace_file_path = tmpdir / "trace.zip"
166+
await context.tracing.stop(path=trace_file_path)
167+
168+
(_, events) = parse_trace(trace_file_path)
169+
assert events[0]["type"] == "context-options"
170+
assert get_trace_actions(events) == [
171+
"Page.goto",
172+
"Request.all_headers",
173+
"Response.text",
174+
"Page.close",
175+
]
176+
177+
148178
async def test_should_collect_two_traces(
149179
context: BrowserContext, page: Page, server: Server, tmpdir: Path
150180
) -> None:

tests/sync/test_tracing.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
# limitations under the License.
1414

1515
import re
16+
import threading
1617
from pathlib import Path
1718
from typing import Any, Dict, List
1819

19-
from playwright.sync_api import Browser, BrowserContext, BrowserType, Page
20+
from playwright.sync_api import Browser, BrowserContext, BrowserType, Page, Response
2021
from tests.server import Server
2122
from tests.utils import get_trace_actions, parse_trace
2223

@@ -138,6 +139,35 @@ def test_should_collect_trace_with_resources_but_no_js(
138139
assert script["snapshot"]["response"]["content"].get("_sha1") is None
139140

140141

142+
def test_should_correctly_determine_sync_apiname(
143+
context: BrowserContext, page: Page, server: Server, tmpdir: Path
144+
) -> None:
145+
context.tracing.start(screenshots=True, snapshots=True)
146+
received_response = threading.Event()
147+
148+
def _handle_response(response: Response) -> None:
149+
response.request.all_headers()
150+
response.text()
151+
received_response.set()
152+
153+
page.once("response", _handle_response)
154+
page.goto(server.PREFIX + "/grid.html")
155+
received_response.wait()
156+
157+
page.close()
158+
trace_file_path = tmpdir / "trace.zip"
159+
context.tracing.stop(path=trace_file_path)
160+
161+
(_, events) = parse_trace(trace_file_path)
162+
assert events[0]["type"] == "context-options"
163+
assert get_trace_actions(events) == [
164+
"Page.goto",
165+
"Request.all_headers",
166+
"Response.text",
167+
"Page.close",
168+
]
169+
170+
141171
def test_should_collect_two_traces(
142172
context: BrowserContext, page: Page, server: Server, tmpdir: Path
143173
) -> None:

0 commit comments

Comments
 (0)
0