Skip to content

Commit 84d94a3

Browse files
authored
chore: port record_har_* options (content, mode, url_filter) (#1382)
This is part 3/n of the 1.23 port. Relates #1308, #1374, #1376. Ports: - [x] microsoft/playwright@fdcdd58 (feat(har): introduce urlFilter (#14693)) - [x] microsoft/playwright@c349c1d (feat: newContext.har (#14892)) - [x] microsoft/playwright@245c33a (feat(har): allow storing content as separate files (#14934)) - [x] microsoft/playwright@be64e9c (chore(har): attach resources for .zip hars (#14938)) - [x] microsoft/playwright@7bd7271 (feat(har): introduce the slim mode (#15053))
1 parent 7b9ac37 commit 84d94a3

File tree

12 files changed

+451
-22
lines changed

12 files changed

+451
-22
lines changed

playwright/_impl/_browser.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import json
1717
from pathlib import Path
1818
from types import SimpleNamespace
19-
from typing import TYPE_CHECKING, Any, Dict, List, Union, cast
19+
from typing import TYPE_CHECKING, Any, Dict, List, Pattern, Union, cast
2020

2121
from playwright._impl._api_structures import (
2222
Geolocation,
@@ -31,6 +31,8 @@
3131
from playwright._impl._helper import (
3232
ColorScheme,
3333
ForcedColors,
34+
HarContentPolicy,
35+
HarMode,
3436
ReducedMotion,
3537
ServiceWorkersPolicy,
3638
async_readfile,
@@ -40,6 +42,7 @@
4042
from playwright._impl._local_utils import LocalUtils
4143
from playwright._impl._network import serialize_headers
4244
from playwright._impl._page import Page
45+
from playwright._impl._str_utils import escape_regex_flags
4346

4447
if TYPE_CHECKING: # pragma: no cover
4548
from playwright._impl._browser_type import BrowserType
@@ -116,6 +119,9 @@ async def new_context(
116119
baseURL: str = None,
117120
strictSelectors: bool = None,
118121
serviceWorkers: ServiceWorkersPolicy = None,
122+
recordHarUrlFilter: Union[Pattern, str] = None,
123+
recordHarMode: HarMode = None,
124+
recordHarContent: HarContentPolicy = None,
119125
) -> BrowserContext:
120126
params = locals_to_params(locals())
121127
await normalize_context_params(self._connection._is_sync, params)
@@ -160,6 +166,9 @@ async def new_page(
160166
baseURL: str = None,
161167
strictSelectors: bool = None,
162168
serviceWorkers: ServiceWorkersPolicy = None,
169+
recordHarUrlFilter: Union[Pattern, str] = None,
170+
recordHarMode: HarMode = None,
171+
recordHarContent: HarContentPolicy = None,
163172
) -> Page:
164173
params = locals_to_params(locals())
165174
context = await self.new_context(**params)
@@ -217,9 +226,30 @@ async def normalize_context_params(is_sync: bool, params: Dict) -> None:
217226
if "recordHarPath" in params:
218227
recordHar: Dict[str, Any] = {"path": str(params["recordHarPath"])}
219228
params["recordHar"] = recordHar
229+
if "recordHarUrlFilter" in params:
230+
opt = params["recordHarUrlFilter"]
231+
if isinstance(opt, str):
232+
params["recordHar"]["urlGlob"] = opt
233+
if isinstance(opt, Pattern):
234+
params["recordHar"]["urlRegexSource"] = opt.pattern
235+
params["recordHar"]["urlRegexFlags"] = escape_regex_flags(opt)
236+
del params["recordHarUrlFilter"]
237+
if "recordHarMode" in params:
238+
params["recordHar"]["mode"] = params["recordHarMode"]
239+
del params["recordHarMode"]
240+
241+
new_content_api = None
242+
old_content_api = None
243+
if "recordHarContent" in params:
244+
new_content_api = params["recordHarContent"]
245+
del params["recordHarContent"]
220246
if "recordHarOmitContent" in params:
221-
params["recordHar"]["omitContent"] = params["recordHarOmitContent"]
247+
old_content_api = params["recordHarOmitContent"]
222248
del params["recordHarOmitContent"]
249+
content = new_content_api or ("omit" if old_content_api else None)
250+
if content:
251+
params["recordHar"]["content"] = content
252+
223253
del params["recordHarPath"]
224254
if "recordVideoDir" in params:
225255
params["recordVideo"] = {"dir": str(params["recordVideoDir"])}

playwright/_impl/_browser_type.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import asyncio
1616
import pathlib
1717
from pathlib import Path
18-
from typing import TYPE_CHECKING, Dict, List, Optional, Union, cast
18+
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast
1919

2020
from playwright._impl._api_structures import (
2121
Geolocation,
@@ -36,6 +36,8 @@
3636
ColorScheme,
3737
Env,
3838
ForcedColors,
39+
HarContentPolicy,
40+
HarMode,
3941
ReducedMotion,
4042
ServiceWorkersPolicy,
4143
locals_to_params,
@@ -139,6 +141,9 @@ async def launch_persistent_context(
139141
baseURL: str = None,
140142
strictSelectors: bool = None,
141143
serviceWorkers: ServiceWorkersPolicy = None,
144+
recordHarUrlFilter: Union[Pattern, str] = None,
145+
recordHarMode: HarMode = None,
146+
recordHarContent: HarContentPolicy = None,
142147
) -> BrowserContext:
143148
userDataDir = str(Path(userDataDir))
144149
params = locals_to_params(locals())

playwright/_impl/_helper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
KeyboardModifier = Literal["Alt", "Control", "Meta", "Shift"]
6666
MouseButton = Literal["left", "middle", "right"]
6767
ServiceWorkersPolicy = Literal["allow", "block"]
68+
HarMode = Literal["full", "minimal"]
69+
HarContentPolicy = Literal["attach", "embed", "omit"]
6870

6971

7072
class ErrorPayload(TypedDict, total=False):

playwright/async_api/_generated.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10647,7 +10647,10 @@ async def new_context(
1064710647
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
1064810648
base_url: str = None,
1064910649
strict_selectors: bool = None,
10650-
service_workers: Literal["allow", "block"] = None
10650+
service_workers: Literal["allow", "block"] = None,
10651+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
10652+
record_har_mode: Literal["full", "minimal"] = None,
10653+
record_har_content: Literal["attach", "embed", "omit"] = None
1065110654
) -> "BrowserContext":
1065210655
"""Browser.new_context
1065310656

@@ -10756,6 +10759,14 @@ async def new_context(
1075610759
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1075710760
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1075810761
- `'block'`: Playwright will block all registration of Service Workers.
10762+
record_har_url_filter : Union[Pattern, str, NoneType]
10763+
record_har_mode : Union["full", "minimal", NoneType]
10764+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
10765+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
10766+
record_har_content : Union["attach", "embed", "omit", NoneType]
10767+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
10768+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
10769+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1075910770

1076010771
Returns
1076110772
-------
@@ -10795,6 +10806,9 @@ async def new_context(
1079510806
baseURL=base_url,
1079610807
strictSelectors=strict_selectors,
1079710808
serviceWorkers=service_workers,
10809+
recordHarUrlFilter=record_har_url_filter,
10810+
recordHarMode=record_har_mode,
10811+
recordHarContent=record_har_content,
1079810812
)
1079910813
)
1080010814

@@ -10831,7 +10845,10 @@ async def new_page(
1083110845
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
1083210846
base_url: str = None,
1083310847
strict_selectors: bool = None,
10834-
service_workers: Literal["allow", "block"] = None
10848+
service_workers: Literal["allow", "block"] = None,
10849+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
10850+
record_har_mode: Literal["full", "minimal"] = None,
10851+
record_har_content: Literal["attach", "embed", "omit"] = None
1083510852
) -> "Page":
1083610853
"""Browser.new_page
1083710854

@@ -10935,6 +10952,14 @@ async def new_page(
1093510952
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1093610953
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1093710954
- `'block'`: Playwright will block all registration of Service Workers.
10955+
record_har_url_filter : Union[Pattern, str, NoneType]
10956+
record_har_mode : Union["full", "minimal", NoneType]
10957+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
10958+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
10959+
record_har_content : Union["attach", "embed", "omit", NoneType]
10960+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
10961+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
10962+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1093810963

1093910964
Returns
1094010965
-------
@@ -10974,6 +10999,9 @@ async def new_page(
1097410999
baseURL=base_url,
1097511000
strictSelectors=strict_selectors,
1097611001
serviceWorkers=service_workers,
11002+
recordHarUrlFilter=record_har_url_filter,
11003+
recordHarMode=record_har_mode,
11004+
recordHarContent=record_har_content,
1097711005
)
1097811006
)
1097911007

@@ -11269,7 +11297,10 @@ async def launch_persistent_context(
1126911297
record_video_size: ViewportSize = None,
1127011298
base_url: str = None,
1127111299
strict_selectors: bool = None,
11272-
service_workers: Literal["allow", "block"] = None
11300+
service_workers: Literal["allow", "block"] = None,
11301+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
11302+
record_har_mode: Literal["full", "minimal"] = None,
11303+
record_har_content: Literal["attach", "embed", "omit"] = None
1127311304
) -> "BrowserContext":
1127411305
"""BrowserType.launch_persistent_context
1127511306

@@ -11413,6 +11444,14 @@ async def launch_persistent_context(
1141311444
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1141411445
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1141511446
- `'block'`: Playwright will block all registration of Service Workers.
11447+
record_har_url_filter : Union[Pattern, str, NoneType]
11448+
record_har_mode : Union["full", "minimal", NoneType]
11449+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
11450+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
11451+
record_har_content : Union["attach", "embed", "omit", NoneType]
11452+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
11453+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
11454+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1141611455

1141711456
Returns
1141811457
-------
@@ -11466,6 +11505,9 @@ async def launch_persistent_context(
1146611505
baseURL=base_url,
1146711506
strictSelectors=strict_selectors,
1146811507
serviceWorkers=service_workers,
11508+
recordHarUrlFilter=record_har_url_filter,
11509+
recordHarMode=record_har_mode,
11510+
recordHarContent=record_har_content,
1146911511
)
1147011512
)
1147111513

playwright/sync_api/_generated.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10669,7 +10669,10 @@ def new_context(
1066910669
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
1067010670
base_url: str = None,
1067110671
strict_selectors: bool = None,
10672-
service_workers: Literal["allow", "block"] = None
10672+
service_workers: Literal["allow", "block"] = None,
10673+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
10674+
record_har_mode: Literal["full", "minimal"] = None,
10675+
record_har_content: Literal["attach", "embed", "omit"] = None
1067310676
) -> "BrowserContext":
1067410677
"""Browser.new_context
1067510678

@@ -10778,6 +10781,14 @@ def new_context(
1077810781
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1077910782
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1078010783
- `'block'`: Playwright will block all registration of Service Workers.
10784+
record_har_url_filter : Union[Pattern, str, NoneType]
10785+
record_har_mode : Union["full", "minimal", NoneType]
10786+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
10787+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
10788+
record_har_content : Union["attach", "embed", "omit", NoneType]
10789+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
10790+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
10791+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1078110792

1078210793
Returns
1078310794
-------
@@ -10818,6 +10829,9 @@ def new_context(
1081810829
baseURL=base_url,
1081910830
strictSelectors=strict_selectors,
1082010831
serviceWorkers=service_workers,
10832+
recordHarUrlFilter=record_har_url_filter,
10833+
recordHarMode=record_har_mode,
10834+
recordHarContent=record_har_content,
1082110835
)
1082210836
)
1082310837
)
@@ -10855,7 +10869,10 @@ def new_page(
1085510869
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
1085610870
base_url: str = None,
1085710871
strict_selectors: bool = None,
10858-
service_workers: Literal["allow", "block"] = None
10872+
service_workers: Literal["allow", "block"] = None,
10873+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
10874+
record_har_mode: Literal["full", "minimal"] = None,
10875+
record_har_content: Literal["attach", "embed", "omit"] = None
1085910876
) -> "Page":
1086010877
"""Browser.new_page
1086110878

@@ -10959,6 +10976,14 @@ def new_page(
1095910976
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1096010977
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1096110978
- `'block'`: Playwright will block all registration of Service Workers.
10979+
record_har_url_filter : Union[Pattern, str, NoneType]
10980+
record_har_mode : Union["full", "minimal", NoneType]
10981+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
10982+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
10983+
record_har_content : Union["attach", "embed", "omit", NoneType]
10984+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
10985+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
10986+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1096210987

1096310988
Returns
1096410989
-------
@@ -10999,6 +11024,9 @@ def new_page(
1099911024
baseURL=base_url,
1100011025
strictSelectors=strict_selectors,
1100111026
serviceWorkers=service_workers,
11027+
recordHarUrlFilter=record_har_url_filter,
11028+
recordHarMode=record_har_mode,
11029+
recordHarContent=record_har_content,
1100211030
)
1100311031
)
1100411032
)
@@ -11299,7 +11327,10 @@ def launch_persistent_context(
1129911327
record_video_size: ViewportSize = None,
1130011328
base_url: str = None,
1130111329
strict_selectors: bool = None,
11302-
service_workers: Literal["allow", "block"] = None
11330+
service_workers: Literal["allow", "block"] = None,
11331+
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
11332+
record_har_mode: Literal["full", "minimal"] = None,
11333+
record_har_content: Literal["attach", "embed", "omit"] = None
1130311334
) -> "BrowserContext":
1130411335
"""BrowserType.launch_persistent_context
1130511336

@@ -11443,6 +11474,14 @@ def launch_persistent_context(
1144311474
Whether to allow sites to register Service workers. Defaults to `'allow'`.
1144411475
- `'allow'`: [Service Workers](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API) can be registered.
1144511476
- `'block'`: Playwright will block all registration of Service Workers.
11477+
record_har_url_filter : Union[Pattern, str, NoneType]
11478+
record_har_mode : Union["full", "minimal", NoneType]
11479+
When set to `minimal`, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies,
11480+
security and other types of HAR information that are not used when replaying from HAR. Defaults to `full`.
11481+
record_har_content : Union["attach", "embed", "omit", NoneType]
11482+
Optional setting to control resource content management. If `omit` is specified, content is not persisted. If `attach`
11483+
is specified, resources are persistet as separate files and all of these files are archived along with the HAR file.
11484+
Defaults to `embed`, which stores content inline the HAR file as per HAR specification.
1144611485

1144711486
Returns
1144811487
-------
@@ -11497,6 +11536,9 @@ def launch_persistent_context(
1149711536
baseURL=base_url,
1149811537
strictSelectors=strict_selectors,
1149911538
serviceWorkers=service_workers,
11539+
recordHarUrlFilter=record_har_url_filter,
11540+
recordHarMode=record_har_mode,
11541+
recordHarContent=record_har_content,
1150011542
)
1150111543
)
1150211544
)

scripts/expected_api_mismatch.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ Method not implemented: Error.message
2020
Method not implemented: PlaywrightAssertions.expect
2121

2222
# Pending 1.23 ports
23-
Parameter not implemented: BrowserType.launch_persistent_context(record_har_url_filter=)
2423
Method not implemented: BrowserContext.route_from_har
2524
Method not implemented: Route.fallback
26-
Parameter not implemented: Browser.new_page(record_har_url_filter=)
2725
Method not implemented: Page.route_from_har
28-
Parameter not implemented: Browser.new_context(record_har_url_filter=)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
InWheel = None
3131
from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand
3232

33-
driver_version = "1.23.0-beta-1656026605000"
33+
driver_version = "1.23.0-beta-1656093125000"
3434

3535

3636
def extractall(zip: zipfile.ZipFile, path: str) -> None:

0 commit comments

Comments
 (0)