MCPcopy Index your code
hub / github.com/microsoft/playwright-python / test_memory_objects

Function test_memory_objects

tests/test_reference_count_async.py:32–86  ·  view source on GitHub ↗
(server: Server, browser_name: str)

Source from the content-addressed store, hash-verified

30
31@pytest.mark.asyncio
32async def test_memory_objects(server: Server, browser_name: str) -> None:
33 async with async_playwright() as p:
34 browser = await p[browser_name].launch()
35 page = await browser.new_page()
36 await page.goto(server.EMPTY_PAGE)
37
38 page.on("dialog", lambda dialog: dialog.dismiss())
39 for _ in range(100):
40 await page.evaluate("""async () => alert()""")
41
42 await page.route("**/*", lambda route, _: route.fulfill(body="OK"))
43
44 def handle_network_response_received(event: Any) -> None:
45 event[_NETWORK_RESPONSE_RECEIVED_MARKER] = (
46 _NETWORK_RESPONSE_RECEIVED_MARKER_VALUE
47 )
48
49 if browser_name == "chromium":
50 # https://github.com/microsoft/playwright-python/issues/1602
51 client = await page.context.new_cdp_session(page)
52 await client.send("Network.enable")
53
54 client.on(
55 "Network.responseReceived",
56 handle_network_response_received,
57 )
58
59 for _ in range(100):
60 response = await page.evaluate("""async () => (await fetch("/")).text()""")
61 assert response == "OK"
62
63 await browser.close()
64
65 gc.collect()
66
67 pw_objects: defaultdict = defaultdict(int)
68 for o in objgraph.by_type("dict"):
69 assert isinstance(o, dict)
70 name = o.get("_type")
71 # https://github.com/microsoft/playwright-python/issues/1602
72 # Python 3.14 can expose CPython's interned-strings dict here, where
73 # a string can appear as both key and value. Check the sentinel by
74 # identity to avoid confusing that internal dict with a leaked event.
75 if (
76 o.get(_NETWORK_RESPONSE_RECEIVED_MARKER)
77 is _NETWORK_RESPONSE_RECEIVED_MARKER_VALUE
78 ):
79 assert False
80 if not name:
81 continue
82 pw_objects[name] += 1
83
84 assert "Dialog" not in pw_objects
85 assert "Request" not in pw_objects
86 assert "Route" not in pw_objects
87
88
89@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 13

async_playwrightFunction · 0.90
launchMethod · 0.45
new_pageMethod · 0.45
gotoMethod · 0.45
onMethod · 0.45
dismissMethod · 0.45
evaluateMethod · 0.45
routeMethod · 0.45
fulfillMethod · 0.45
new_cdp_sessionMethod · 0.45
sendMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected