Skip to content

Commit d073877

Browse files
committed
Clean up HAR option serialization
1 parent bbdbfe3 commit d073877

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

playwright/_impl/_browser.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,11 @@ async def new_context(
158158
channel = await self._channel.send("newContext", params)
159159
context = cast(BrowserContext, from_channel(channel))
160160
await context._initialize_har_from_options(
161-
{
162-
"recordHarPath": recordHarPath,
163-
"recordHarContent": recordHarContent,
164-
"recordHarOmitContent": recordHarOmitContent,
165-
"recordHarUrlFilter": recordHarUrlFilter,
166-
"recordHarMode": recordHarMode,
167-
}
161+
record_har_content=recordHarContent,
162+
record_har_mode=recordHarMode,
163+
record_har_omit_content=recordHarOmitContent,
164+
record_har_path=recordHarPath,
165+
record_har_url_filter=recordHarUrlFilter,
168166
)
169167
return context
170168

playwright/_impl/_browser_context.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,32 @@ def pages(self) -> List[Page]:
302302
def browser(self) -> Optional["Browser"]:
303303
return self._browser
304304

305-
async def _initialize_har_from_options(self, options: Dict) -> None:
306-
record_har_path = options.get("recordHarPath")
305+
async def _initialize_har_from_options(
306+
self,
307+
record_har_path: Optional[Union[Path, str]],
308+
record_har_content: Optional[HarContentPolicy],
309+
record_har_omit_content: Optional[bool],
310+
record_har_url_filter: Optional[Union[Pattern[str], str]],
311+
record_har_mode: Optional[HarMode],
312+
) -> None:
307313
if not record_har_path:
308314
return
309315
record_har_path = str(record_har_path)
310316
if len(record_har_path) == 0:
311317
return
312-
default_policy = "attach" if record_har_path.endswith(".zip") else "embed"
313-
content_policy = options.get(
314-
"recordHarContent",
315-
"omit" if options["recordHarOmitContent"] is True else default_policy,
318+
# Forcibly list type to satisfy mypy
319+
default_policy: HarContentPolicy = (
320+
"attach" if record_har_path.endswith(".zip") else "embed"
321+
)
322+
content_policy: HarContentPolicy = record_har_content or (
323+
"omit" if record_har_omit_content is True else default_policy
316324
)
317325
await self._record_into_har(
318326
har=record_har_path,
319327
page=None,
320-
url=options["recordHarUrlFilter"],
328+
url=record_har_url_filter,
321329
update_content=content_policy,
322-
update_mode=options.get("recordHarMode", "full"),
330+
update_mode=(record_har_mode or "full"),
323331
)
324332

325333
async def new_page(self) -> Page:

playwright/_impl/_browser_type.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,11 @@ async def launch_persistent_context(
170170
)
171171
context = cast(BrowserContext, from_channel(result["context"]))
172172
await context._initialize_har_from_options(
173-
{
174-
"recordHarContent": recordHarContent,
175-
"recordHarMode": recordHarMode,
176-
"recordHarOmitContent": recordHarOmitContent,
177-
"recordHarPath": recordHarPath,
178-
"recordHarUrlFilter": (
179-
recordHarUrlFilter if isinstance(recordHarUrlFilter, str) else None
180-
),
181-
"recordHarUrlFilterRegex": (
182-
recordHarUrlFilter
183-
if isinstance(recordHarUrlFilter, Pattern)
184-
else None
185-
),
186-
}
173+
record_har_content=recordHarContent,
174+
record_har_mode=recordHarMode,
175+
record_har_omit_content=recordHarOmitContent,
176+
record_har_path=recordHarPath,
177+
record_har_url_filter=recordHarUrlFilter,
187178
)
188179
return context
189180

@@ -338,6 +329,13 @@ async def _prepare_browser_context_params(self, params: Dict) -> None:
338329
params["selectorEngines"] = self._playwright.selectors._selectorEngines
339330
params["testIdAttributeName"] = self._playwright.selectors._testIdAttributeName
340331

332+
# Remove HAR options
333+
params.pop("recordHarPath", None)
334+
params.pop("recordHarOmitContent", None)
335+
params.pop("recordHarUrlFilter", None)
336+
params.pop("recordHarMode", None)
337+
params.pop("recordHarContent", None)
338+
341339

342340
def normalize_launch_params(params: Dict) -> None:
343341
if "env" in params:

0 commit comments

Comments
 (0)