Skip to content

Commit 53efab9

Browse files
committed
chg: Improve handling of temp HAR file
1 parent debb05b commit 53efab9

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

playwrightcapture/capture.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,6 @@ def __prepare_proxy_aiohttp(self, proxy: ProxySettings) -> str:
234234

235235
async def __aenter__(self) -> Capture:
236236
'''Launch the browser'''
237-
self._temp_harfile = NamedTemporaryFile(delete=False)
238-
239237
self.playwright = await async_playwright().start()
240238

241239
if self.device_name:
@@ -255,11 +253,12 @@ async def __aenter__(self) -> Capture:
255253
# Set of URLs that were captured in that context
256254
self._already_captured: set[str] = set()
257255

256+
# Create the temporary file to store the HAR content.
257+
self._temp_harfile = NamedTemporaryFile(delete=False, prefix="playwright_capture_har", suffix=".json")
258+
258259
return self
259260

260261
async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> bool:
261-
if hasattr(self, '_temp_harfile'):
262-
os.unlink(self._temp_harfile.name)
263262

264263
try:
265264
await self.browser.close(reason="Closing browser at the end of the capture.")
@@ -271,6 +270,13 @@ async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> bool:
271270
except Exception as e:
272271
# this should't happen, but just in case it does...
273272
self.logger.info(f'Unable to stop playwright: {e}')
273+
274+
if hasattr(self, '_temp_harfile'):
275+
try:
276+
os.unlink(self._temp_harfile.name)
277+
except Exception as e:
278+
self.logger.warning(f'Unable to remove temp HAR file {self._temp_harfile.name}: {e}')
279+
274280
return True
275281

276282
@property

0 commit comments

Comments
 (0)