Skip to content

chore: expose local_utils #1383

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 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
1 change: 0 additions & 1 deletion playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ async def new_context(
self._contexts.append(context)
context._browser = self
context._options = params
context._tracing._local_utils = self._local_utils
return context

async def new_page(
Expand Down
5 changes: 1 addition & 4 deletions playwright/_impl/_browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ async def launch(
browser = cast(
Browser, from_channel(await self._channel.send("launch", params))
)
browser._local_utils = self._playwright._utils
return browser

async def launch_persistent_context(
Expand Down Expand Up @@ -150,7 +149,6 @@ async def launch_persistent_context(
from_channel(await self._channel.send("launchPersistentContext", params)),
)
context._options = params
context.tracing._local_utils = self._playwright._utils
return context

async def connect_over_cdp(
Expand All @@ -163,7 +161,6 @@ 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"]))
browser._local_utils = self._playwright._utils

default_context = cast(
Optional[BrowserContext],
Expand Down Expand Up @@ -194,6 +191,7 @@ async def connect(
self._connection._object_factory,
transport,
self._connection._loop,
local_utils=self._connection.local_utils,
)
connection.mark_as_remote()
connection._is_sync = self._connection._is_sync
Expand All @@ -216,7 +214,6 @@ async def connect(
assert pre_launched_browser
browser = cast(Browser, from_channel(pre_launched_browser))
browser._should_close_connection_on_close = True
browser._local_utils = self._playwright._utils

def handle_transport_close() -> None:
for context in browser.contexts:
Expand Down
10 changes: 9 additions & 1 deletion playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from playwright._impl._transport import Transport

if TYPE_CHECKING:
from playwright._impl._local_utils import LocalUtils
from playwright._impl._playwright import Playwright


Expand Down Expand Up @@ -109,7 +110,7 @@ def __init__(
parent if isinstance(parent, ChannelOwner) else None
)
self._objects: Dict[str, "ChannelOwner"] = {}
self._channel = Channel(self._connection, guid)
self._channel: Channel = Channel(self._connection, guid)
self._channel._object = self
self._initializer = initializer

Expand Down Expand Up @@ -173,6 +174,7 @@ def __init__(
object_factory: Callable[[ChannelOwner, str, str, Dict], ChannelOwner],
transport: Transport,
loop: asyncio.AbstractEventLoop,
local_utils: Optional["LocalUtils"] = None,
) -> None:
super().__init__()
self._dispatcher_fiber = dispatcher_fiber
Expand All @@ -193,6 +195,12 @@ def __init__(
self._api_zone: contextvars.ContextVar[Optional[Dict]] = contextvars.ContextVar(
"ApiZone", default=None
)
self._local_utils: Optional["LocalUtils"] = local_utils

@property
def local_utils(self) -> "LocalUtils":
assert self._local_utils
return self._local_utils

def mark_as_remote(self) -> None:
self.is_remote = True
Expand Down
1 change: 0 additions & 1 deletion playwright/_impl/_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ async def new_context(
APIRequestContext,
from_channel(await self.playwright._channel.send("newRequest", params)),
)
context._tracing._local_utils = self.playwright._utils
return context


Expand Down
5 changes: 4 additions & 1 deletion playwright/_impl/_object_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def create_remote_object(
if type == "JSHandle":
return JSHandle(parent, type, guid, initializer)
if type == "LocalUtils":
return LocalUtils(parent, type, guid, initializer)
local_utils = LocalUtils(parent, type, guid, initializer)
if not local_utils._connection._local_utils:
local_utils._connection._local_utils = local_utils
return local_utils
if type == "Page":
return Page(parent, type, guid, initializer)
if type == "Playwright":
Expand Down
6 changes: 3 additions & 3 deletions playwright/_impl/_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
from playwright._impl._artifact import Artifact
from playwright._impl._connection import ChannelOwner, from_nullable_channel
from playwright._impl._helper import locals_to_params
from playwright._impl._local_utils import LocalUtils


class Tracing(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
_local_utils: LocalUtils

async def start(
self,
Expand Down Expand Up @@ -86,4 +84,6 @@ async def _do_stop_chunk(self, file_path: Union[pathlib.Path, str] = None) -> No

# Add local sources to the remote trace if necessary.
if result.get("sourceEntries", []):
await self._local_utils.zip(file_path, result["sourceEntries"])
await self._connection.local_utils.zip(
str(file_path), result["sourceEntries"]
)