Skip to content

Commit

Permalink
chore(roll): roll Playwright to 1.32.0-beta-1679448099000 (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Mar 22, 2023
1 parent 890af94 commit f791b88
Show file tree
Hide file tree
Showing 21 changed files with 448 additions and 157 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H

| | Linux | macOS | Windows |
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->112.0.5615.20<!-- GEN:stop --> ||||
| Chromium <!-- GEN:chromium-version -->112.0.5615.29<!-- GEN:stop --> ||||
| WebKit <!-- GEN:webkit-version -->16.4<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->110.0.1<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->111.0<!-- GEN:stop --> ||||

## Documentation

Expand Down
10 changes: 1 addition & 9 deletions playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,7 @@ async def new_context(

channel = await self._channel.send("newContext", params)
context = cast(BrowserContext, from_channel(channel))
self._contexts.append(context)
context._browser = self
context._options = params
context._set_browser_type(self._browser_type)
self._browser_type._did_create_context(context, params, {})
return context

async def new_page(
Expand Down Expand Up @@ -175,11 +172,6 @@ async def new_page(
context._owner_page = page
return page

def _set_browser_type(self, browser_type: "BrowserType") -> None:
self._browser_type = browser_type
for context in self._contexts:
context._set_browser_type(browser_type)

async def close(self) -> None:
if self._is_closed_or_closing:
return
Expand Down
58 changes: 39 additions & 19 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import asyncio
import json
import sys
from pathlib import Path
from types import SimpleNamespace
from typing import (
Expand Down Expand Up @@ -59,7 +60,6 @@
URLMatcher,
async_readfile,
async_writefile,
is_safe_close_error,
locals_to_params,
prepare_record_har_options,
to_impl,
Expand All @@ -71,7 +71,11 @@

if TYPE_CHECKING: # pragma: no cover
from playwright._impl._browser import Browser
from playwright._impl._browser_type import BrowserType

if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal
else: # pragma: no cover
from typing_extensions import Literal


class BrowserContext(ChannelOwner):
Expand All @@ -90,11 +94,15 @@ def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
# circular import workaround:
self._browser: Optional["Browser"] = None
if parent.__class__.__name__ == "Browser":
self._browser = cast("Browser", parent)
self._browser._contexts.append(self)
self._pages: List[Page] = []
self._routes: List[RouteHandler] = []
self._bindings: Dict[str, Any] = {}
self._timeout_settings = TimeoutSettings(None)
self._browser: Optional["Browser"] = None
self._owner_page: Optional[Page] = None
self._options: Dict[str, Any] = {}
self._background_pages: Set[Page] = set()
Expand Down Expand Up @@ -172,6 +180,7 @@ def __init__(
BrowserContext.Events.RequestFailed: "requestFailed",
}
)
self._close_was_called = False

def __repr__(self) -> str:
return f"<BrowserContext browser={self.browser}>"
Expand Down Expand Up @@ -226,13 +235,14 @@ def pages(self) -> List[Page]:
def browser(self) -> Optional["Browser"]:
return self._browser

def _set_browser_type(self, browser_type: "BrowserType") -> None:
self._browser_type = browser_type
def _set_options(self, context_options: Dict, browser_options: Dict) -> None:
self._options = context_options
if self._options.get("recordHar"):
self._har_recorders[""] = {
"path": self._options["recordHar"]["path"],
"content": self._options["recordHar"].get("content"),
}
self._tracing._traces_dir = browser_options.get("tracesDir")

async def new_page(self) -> Page:
if self._owner_page:
Expand Down Expand Up @@ -328,36 +338,43 @@ async def _record_into_har(
har: Union[Path, str],
page: Optional[Page] = None,
url: Union[Pattern[str], str] = None,
content: HarContentPolicy = None,
mode: HarMode = None,
update_content: HarContentPolicy = None,
update_mode: HarMode = None,
) -> None:
params: Dict[str, Any] = {
"options": prepare_record_har_options(
{
"recordHarPath": har,
"recordHarContent": content or "attach",
"recordHarMode": mode or "minimal",
"recordHarContent": update_content or "attach",
"recordHarMode": update_mode or "minimal",
"recordHarUrlFilter": url,
}
)
}
if page:
params["page"] = page._channel
har_id = await self._channel.send("harStart", params)
self._har_recorders[har_id] = {"path": str(har), "content": content or "attach"}
self._har_recorders[har_id] = {
"path": str(har),
"content": update_content or "attach",
}

async def route_from_har(
self,
har: Union[Path, str],
url: Union[Pattern[str], str] = None,
not_found: RouteFromHarNotFoundPolicy = None,
update: bool = None,
content: HarContentPolicy = None,
mode: HarMode = None,
update_content: Literal["attach", "embed"] = None,
update_mode: HarMode = None,
) -> None:
if update:
await self._record_into_har(
har=har, page=None, url=url, content=content, mode=mode
har=har,
page=None,
url=url,
update_content=update_content,
update_mode=update_mode,
)
return
router = await HarRouter.create(
Expand Down Expand Up @@ -400,7 +417,11 @@ def _on_close(self) -> None:
self.emit(BrowserContext.Events.Close, self)

async def close(self) -> None:
try:
if self._close_was_called:
return
self._close_was_called = True

async def _inner_close() -> None:
for har_id, params in self._har_recorders.items():
har = cast(
Artifact,
Expand All @@ -422,11 +443,10 @@ async def close(self) -> None:
else:
await har.save_as(params["path"])
await har.delete()
await self._channel.send("close")
await self._closed_future
except Exception as e:
if not is_safe_close_error(e):
raise e

await self._channel._connection.wrap_api_call(_inner_close, True)
await self._channel.send("close")
await self._closed_future

async def _pause(self) -> None:
await self._channel.send("pause")
Expand Down
22 changes: 15 additions & 7 deletions playwright/_impl/_browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async def launch(
browser = cast(
Browser, from_channel(await self._channel.send("launch", params))
)
browser._set_browser_type(self)
self._did_launch_browser(browser)
return browser

async def launch_persistent_context(
Expand Down Expand Up @@ -154,8 +154,7 @@ async def launch_persistent_context(
BrowserContext,
from_channel(await self._channel.send("launchPersistentContext", params)),
)
context._options = params
context._set_browser_type(self)
self._did_create_context(context, params, params)
return context

async def connect_over_cdp(
Expand All @@ -168,15 +167,14 @@ async def connect_over_cdp(
params = locals_to_params(locals())
response = await self._channel.send_return_as_dict("connectOverCDP", params)
browser = cast(Browser, from_channel(response["browser"]))
self._did_launch_browser(browser)

default_context = cast(
Optional[BrowserContext],
from_nullable_channel(response.get("defaultContext")),
)
if default_context:
browser._contexts.append(default_context)
default_context._browser = browser
browser._set_browser_type(self)
self._did_create_context(default_context, {}, {})
return browser

async def connect(
Expand Down Expand Up @@ -231,6 +229,7 @@ async def connect(
pre_launched_browser = playwright._initializer.get("preLaunchedBrowser")
assert pre_launched_browser
browser = cast(Browser, from_channel(pre_launched_browser))
self._did_launch_browser(browser)
browser._should_close_connection_on_close = True

def handle_transport_close() -> None:
Expand All @@ -243,9 +242,16 @@ def handle_transport_close() -> None:

transport.once("close", handle_transport_close)

browser._set_browser_type(self)
return browser

def _did_create_context(
self, context: BrowserContext, context_options: Dict, browser_options: Dict
) -> None:
context._set_options(context_options, browser_options)

def _did_launch_browser(self, browser: Browser) -> None:
browser._browser_type = self


def normalize_launch_params(params: Dict) -> None:
if "env" in params:
Expand All @@ -261,3 +267,5 @@ def normalize_launch_params(params: Dict) -> None:
params["executablePath"] = str(Path(params["executablePath"]))
if "downloadsPath" in params:
params["downloadsPath"] = str(Path(params["downloadsPath"]))
if "tracesDir" in params:
params["tracesDir"] = str(Path(params["tracesDir"]))
35 changes: 25 additions & 10 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
import sys
import traceback
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Union,
cast,
)

from greenlet import greenlet
from pyee import EventEmitter
Expand Down Expand Up @@ -231,7 +241,7 @@ def __init__(
Optional[ParsedStackTrace]
] = contextvars.ContextVar("ApiZone", default=None)
self._local_utils: Optional["LocalUtils"] = local_utils
self._stack_collector: List[List[Dict[str, Any]]] = []
self._tracing_count = 0

@property
def local_utils(self) -> "LocalUtils":
Expand Down Expand Up @@ -279,12 +289,11 @@ def call_on_object_with_known_name(
) -> None:
self._waiting_for_object[guid] = callback

def start_collecting_call_metadata(self, collector: Any) -> None:
if collector not in self._stack_collector:
self._stack_collector.append(collector)

def stop_collecting_call_metadata(self, collector: Any) -> None:
self._stack_collector.remove(collector)
def set_in_tracing(self, is_tracing: bool) -> None:
if is_tracing:
self._tracing_count += 1
else:
self._tracing_count -= 1

def _send_message_to_server(
self, guid: str, method: str, params: Dict
Expand All @@ -299,8 +308,6 @@ def _send_message_to_server(
)
self._callbacks[id] = callback
stack_trace_information = cast(ParsedStackTrace, self._api_zone.get())
for collector in self._stack_collector:
collector.append({"stack": stack_trace_information["frames"], "id": id})
frames = stack_trace_information.get("frames", [])
location = (
{
Expand All @@ -325,6 +332,10 @@ def _send_message_to_server(
}
self._transport.send(message)
self._callbacks[id] = callback

if self._tracing_count > 0 and frames and guid != "localUtils":
self.local_utils.add_stack_to_tracing_no_reply(id, frames)

return callback

def dispatch(self, msg: ParsedMessagePayload) -> None:
Expand Down Expand Up @@ -521,3 +532,7 @@ def _extract_stack_trace_information_from_stack(
"frames": parsed_frames,
"apiName": "" if is_internal else api_name,
}


def filter_none(d: Mapping) -> Dict:
return {k: v for k, v in d.items() if v is not None}
5 changes: 1 addition & 4 deletions playwright/_impl/_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
ServerFilePayload,
StorageState,
)
from playwright._impl._connection import ChannelOwner, from_channel
from playwright._impl._connection import ChannelOwner, filter_none, from_channel
from playwright._impl._helper import (
Error,
NameValue,
Expand Down Expand Up @@ -366,9 +366,6 @@ async def _inner_fetch(
base64.b64encode(post_data_buffer).decode() if post_data_buffer else None
)

def filter_none(input: Dict) -> Dict:
return {k: v for k, v in input.items() if v is not None}

response = await self._channel.send(
"fetch",
filter_none(
Expand Down
22 changes: 20 additions & 2 deletions playwright/_impl/_local_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# limitations under the License.

import base64
from typing import Dict, Optional, cast
from typing import Dict, List, Optional, cast

from playwright._impl._api_structures import HeadersArray
from playwright._impl._connection import ChannelOwner
from playwright._impl._connection import ChannelOwner, StackFrame
from playwright._impl._helper import HarLookupResult, locals_to_params


Expand Down Expand Up @@ -57,3 +57,21 @@ async def har_close(self, harId: str) -> None:
async def har_unzip(self, zipFile: str, harFile: str) -> None:
params = locals_to_params(locals())
await self._channel.send("harUnzip", params)

async def tracing_started(self, tracesDir: Optional[str], traceName: str) -> str:
params = locals_to_params(locals())
return await self._channel.send("tracingStarted", params)

async def trace_discarded(self, stacks_id: str) -> None:
return await self._channel.send("traceDiscarded", {"stacks_id": stacks_id})

def add_stack_to_tracing_no_reply(self, id: int, frames: List[StackFrame]) -> None:
self._channel.send_no_reply(
"addStackToTracingNoReply",
{
"callData": {
"stack": frames,
"id": id,
}
},
)
Loading

0 comments on commit f791b88

Please sign in to comment.