Skip to content

chore: port record_har_* options (content, mode, url_filter) #1382

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 8 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import json
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Dict, List, Union, cast
from typing import TYPE_CHECKING, Any, Dict, List, Pattern, Union, cast

from playwright._impl._api_structures import (
Geolocation,
Expand All @@ -31,6 +31,8 @@
from playwright._impl._helper import (
ColorScheme,
ForcedColors,
HarContentPolicy,
HarMode,
ReducedMotion,
ServiceWorkersPolicy,
async_readfile,
Expand All @@ -40,6 +42,7 @@
from playwright._impl._local_utils import LocalUtils
from playwright._impl._network import serialize_headers
from playwright._impl._page import Page
from playwright._impl._str_utils import escape_regex_flags

if TYPE_CHECKING: # pragma: no cover
from playwright._impl._browser_type import BrowserType
Expand Down Expand Up @@ -116,6 +119,9 @@ async def new_context(
baseURL: str = None,
strictSelectors: bool = None,
serviceWorkers: ServiceWorkersPolicy = None,
recordHarUrlFilter: Union[Pattern, str] = None,
recordHarMode: HarMode = None,
recordHarContent: HarContentPolicy = None,
) -> BrowserContext:
params = locals_to_params(locals())
await normalize_context_params(self._connection._is_sync, params)
Expand Down Expand Up @@ -160,6 +166,9 @@ async def new_page(
baseURL: str = None,
strictSelectors: bool = None,
serviceWorkers: ServiceWorkersPolicy = None,
recordHarUrlFilter: Union[Pattern, str] = None,
recordHarMode: HarMode = None,
recordHarContent: HarContentPolicy = None,
) -> Page:
params = locals_to_params(locals())
context = await self.new_context(**params)
Expand Down Expand Up @@ -217,9 +226,30 @@ async def normalize_context_params(is_sync: bool, params: Dict) -> None:
if "recordHarPath" in params:
recordHar: Dict[str, Any] = {"path": str(params["recordHarPath"])}
params["recordHar"] = recordHar
if "recordHarUrlFilter" in params:
opt = params["recordHarUrlFilter"]
if isinstance(opt, str):
params["recordHar"]["urlGlob"] = opt
if isinstance(opt, Pattern):
params["recordHar"]["urlRegexSource"] = opt.pattern
params["recordHar"]["urlRegexFlags"] = escape_regex_flags(opt)
del params["recordHarUrlFilter"]
if "recordHarMode" in params:
params["recordHar"]["mode"] = params["recordHarMode"]
del params["recordHarMode"]

new_content_api = None
old_content_api = None
if "recordHarContent" in params:
new_content_api = params["recordHarContent"]
del params["recordHarContent"]
if "recordHarOmitContent" in params:
params["recordHar"]["omitContent"] = params["recordHarOmitContent"]
old_content_api = params["recordHarOmitContent"]
del params["recordHarOmitContent"]
content = new_content_api or ("omit" if old_content_api else None)
if content:
params["recordHar"]["content"] = content

del params["recordHarPath"]
if "recordVideoDir" in params:
params["recordVideo"] = {"dir": str(params["recordVideoDir"])}
Expand Down
7 changes: 6 additions & 1 deletion playwright/_impl/_browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import asyncio
import pathlib
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Union, cast
from typing import TYPE_CHECKING, Dict, List, Optional, Pattern, Union, cast

from playwright._impl._api_structures import (
Geolocation,
Expand All @@ -36,6 +36,8 @@
ColorScheme,
Env,
ForcedColors,
HarContentPolicy,
HarMode,
ReducedMotion,
ServiceWorkersPolicy,
locals_to_params,
Expand Down Expand Up @@ -139,6 +141,9 @@ async def launch_persistent_context(
baseURL: str = None,
strictSelectors: bool = None,
serviceWorkers: ServiceWorkersPolicy = None,
recordHarUrlFilter: Union[Pattern, str] = None,
recordHarMode: HarMode = None,
recordHarContent: HarContentPolicy = None,
) -> BrowserContext:
userDataDir = str(Path(userDataDir))
params = locals_to_params(locals())
Expand Down
2 changes: 2 additions & 0 deletions playwright/_impl/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
KeyboardModifier = Literal["Alt", "Control", "Meta", "Shift"]
MouseButton = Literal["left", "middle", "right"]
ServiceWorkersPolicy = Literal["allow", "block"]
HarMode = Literal["full", "minimal"]
HarContentPolicy = Literal["attach", "embed", "omit"]


class ErrorPayload(TypedDict, total=False):
Expand Down
48 changes: 45 additions & 3 deletions playwright/async_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -10647,7 +10647,10 @@ async def new_context(
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "BrowserContext":
"""Browser.new_context

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

Returns
-------
Expand Down Expand Up @@ -10795,6 +10806,9 @@ async def new_context(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)

Expand Down Expand Up @@ -10831,7 +10845,10 @@ async def new_page(
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "Page":
"""Browser.new_page

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

Returns
-------
Expand Down Expand Up @@ -10974,6 +10999,9 @@ async def new_page(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)

Expand Down Expand Up @@ -11269,7 +11297,10 @@ async def launch_persistent_context(
record_video_size: ViewportSize = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "BrowserContext":
"""BrowserType.launch_persistent_context

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

Returns
-------
Expand Down Expand Up @@ -11466,6 +11505,9 @@ async def launch_persistent_context(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)

Expand Down
48 changes: 45 additions & 3 deletions playwright/sync_api/_generated.py
Original file line number Diff line number Diff line change
Expand Up @@ -10669,7 +10669,10 @@ def new_context(
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "BrowserContext":
"""Browser.new_context

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

Returns
-------
Expand Down Expand Up @@ -10818,6 +10829,9 @@ def new_context(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)
)
Expand Down Expand Up @@ -10855,7 +10869,10 @@ def new_page(
storage_state: typing.Union[StorageState, str, pathlib.Path] = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "Page":
"""Browser.new_page

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

Returns
-------
Expand Down Expand Up @@ -10999,6 +11024,9 @@ def new_page(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)
)
Expand Down Expand Up @@ -11299,7 +11327,10 @@ def launch_persistent_context(
record_video_size: ViewportSize = None,
base_url: str = None,
strict_selectors: bool = None,
service_workers: Literal["allow", "block"] = None
service_workers: Literal["allow", "block"] = None,
record_har_url_filter: typing.Union[str, typing.Pattern] = None,
record_har_mode: Literal["full", "minimal"] = None,
record_har_content: Literal["attach", "embed", "omit"] = None
) -> "BrowserContext":
"""BrowserType.launch_persistent_context

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

Returns
-------
Expand Down Expand Up @@ -11497,6 +11536,9 @@ def launch_persistent_context(
baseURL=base_url,
strictSelectors=strict_selectors,
serviceWorkers=service_workers,
recordHarUrlFilter=record_har_url_filter,
recordHarMode=record_har_mode,
recordHarContent=record_har_content,
)
)
)
Expand Down
3 changes: 0 additions & 3 deletions scripts/expected_api_mismatch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ Method not implemented: Error.message
Method not implemented: PlaywrightAssertions.expect

# Pending 1.23 ports
Parameter not implemented: BrowserType.launch_persistent_context(record_har_url_filter=)
Method not implemented: BrowserContext.route_from_har
Method not implemented: Route.fallback
Parameter not implemented: Browser.new_page(record_har_url_filter=)
Method not implemented: Page.route_from_har
Parameter not implemented: Browser.new_context(record_har_url_filter=)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
InWheel = None
from wheel.bdist_wheel import bdist_wheel as BDistWheelCommand

driver_version = "1.23.0-beta-1656026605000"
driver_version = "1.23.0-beta-1656093125000"


def extractall(zip: zipfile.ZipFile, path: str) -> None:
Expand Down
Loading