Skip to content

chore(roll): roll Playwright to 1.34.0-alpha-may-17-2023 #1924

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 5 commits into from
May 17, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H
| :--- | :---: | :---: | :---: |
| Chromium <!-- GEN:chromium-version -->113.0.5672.53<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| WebKit <!-- GEN:webkit-version -->16.4<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| Firefox <!-- GEN:firefox-version -->112.0<!-- GEN:stop --> | ✅ | ✅ | ✅ |
| Firefox <!-- GEN:firefox-version -->113.0<!-- GEN:stop --> | ✅ | ✅ | ✅ |

## Documentation

Expand Down
14 changes: 9 additions & 5 deletions playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ async def new_page(
recordHarContent: HarContentPolicy = None,
) -> Page:
params = locals_to_params(locals())
context = await self.new_context(**params)
page = await context.new_page()
page._owned_context = context
context._owner_page = page
return page

async def inner() -> Page:
context = await self.new_context(**params)
page = await context.new_page()
page._owned_context = context
context._owner_page = page
return page

return await self._connection.wrap_api_call(inner)

async def close(self) -> None:
if self._is_closed_or_closing:
Expand Down
35 changes: 35 additions & 0 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
from_channel,
from_nullable_channel,
)
from playwright._impl._console_message import ConsoleMessage
from playwright._impl._dialog import Dialog
from playwright._impl._event_context_manager import EventContextManagerImpl
from playwright._impl._fetch import APIRequestContext
from playwright._impl._frame import Frame
Expand Down Expand Up @@ -82,6 +84,8 @@ class BrowserContext(ChannelOwner):
Events = SimpleNamespace(
BackgroundPage="backgroundpage",
Close="close",
Console="console",
Dialog="dialog",
Page="page",
ServiceWorker="serviceworker",
Request="request",
Expand Down Expand Up @@ -136,6 +140,14 @@ def __init__(
"serviceWorker",
lambda params: self._on_service_worker(from_channel(params["worker"])),
)
self._channel.on(
"console",
lambda params: self._on_console_message(from_channel(params["message"])),
)

self._channel.on(
"dialog", lambda params: self._on_dialog(from_channel(params["dialog"]))
)
self._channel.on(
"request",
lambda params: self._on_request(
Expand Down Expand Up @@ -174,6 +186,8 @@ def __init__(
)
self._set_event_to_subscription_mapping(
{
BrowserContext.Events.Console: "console",
BrowserContext.Events.Dialog: "dialog",
BrowserContext.Events.Request: "request",
BrowserContext.Events.Response: "response",
BrowserContext.Events.RequestFinished: "requestFinished",
Expand Down Expand Up @@ -507,6 +521,27 @@ def _on_request_finished(
if response:
response._finished_future.set_result(True)

def _on_console_message(self, message: ConsoleMessage) -> None:
self.emit(BrowserContext.Events.Console, message)
page = message.page
if page:
page.emit(Page.Events.Console, message)

def _on_dialog(self, dialog: Dialog) -> None:
has_listeners = self.emit(BrowserContext.Events.Dialog, dialog)
page = dialog.page
if page:
has_listeners = page.emit(Page.Events.Dialog, dialog) or has_listeners
if not has_listeners:
# Although we do similar handling on the server side, we still need this logic
# on the client side due to a possible race condition between two async calls:
# a) removing "dialog" listener subscription (client->server)
# b) actual "dialog" event (server->client)
if dialog.type == "beforeunload":
asyncio.create_task(dialog.accept())
else:
asyncio.create_task(dialog.dismiss())

def _on_request(self, request: Request, page: Optional[Page]) -> None:
self.emit(BrowserContext.Events.Request, request)
if page:
Expand Down
7 changes: 5 additions & 2 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ def _set_event_to_subscription_mapping(self, mapping: Dict[str, str]) -> None:
def _update_subscription(self, event: str, enabled: bool) -> None:
protocol_event = self._event_to_subscription_mapping.get(event)
if protocol_event:
self._channel.send_no_reply(
"updateSubscription", {"event": protocol_event, "enabled": enabled}
self._connection.wrap_api_call_sync(
lambda: self._channel.send_no_reply(
"updateSubscription", {"event": protocol_event, "enabled": enabled}
),
True,
)

def _add_event_handler(self, event: str, k: Any, v: Any) -> None:
Expand Down
19 changes: 17 additions & 2 deletions playwright/_impl/_console_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, List
from typing import TYPE_CHECKING, Dict, List, Optional

from playwright._impl._api_structures import SourceLocation
from playwright._impl._connection import ChannelOwner, from_channel
from playwright._impl._connection import (
ChannelOwner,
from_channel,
from_nullable_channel,
)
from playwright._impl._js_handle import JSHandle

if TYPE_CHECKING: # pragma: no cover
from playwright._impl._page import Page


class ConsoleMessage(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
# Note: currently, we only report console messages for pages and they always have a page.
# However, in the future we might report console messages for service workers or something else,
# where page() would be null.
self._page: Optional["Page"] = from_nullable_channel(initializer.get("page"))

def __repr__(self) -> str:
return f"<ConsoleMessage type={self.type} text={self.text}>"
Expand All @@ -46,3 +57,7 @@ def args(self) -> List[JSHandle]:
@property
def location(self) -> SourceLocation:
return self._initializer["location"]

@property
def page(self) -> Optional["Page"]:
return self._page
12 changes: 10 additions & 2 deletions playwright/_impl/_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict
from typing import TYPE_CHECKING, Dict, Optional

from playwright._impl._connection import ChannelOwner
from playwright._impl._connection import ChannelOwner, from_nullable_channel
from playwright._impl._helper import locals_to_params

if TYPE_CHECKING: # pragma: no cover
from playwright._impl._page import Page


class Dialog(ChannelOwner):
def __init__(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._page: Optional["Page"] = from_nullable_channel(initializer.get("page"))

def __repr__(self) -> str:
return f"<Dialog type={self.type} message={self.message} default_value={self.default_value}>"
Expand All @@ -39,6 +43,10 @@ def message(self) -> str:
def default_value(self) -> str:
return self._initializer["defaultValue"]

@property
def page(self) -> Optional["Page"]:
return self._page

async def accept(self, promptText: str = None) -> None:
await self._channel.send("accept", locals_to_params(locals()))

Expand Down
8 changes: 8 additions & 0 deletions playwright/_impl/_locator.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ def or_(self, locator: "Locator") -> "Locator":
self._selector + " >> internal:or=" + json.dumps(locator._selector),
)

def and_(self, locator: "Locator") -> "Locator":
if locator._frame != self._frame:
raise Error("Locators must belong to the same frame.")
return Locator(
self._frame,
self._selector + " >> internal:and=" + json.dumps(locator._selector),
)

async def focus(self, timeout: float = None) -> None:
params = locals_to_params(locals())
return await self._frame.focus(self._selector, strict=True, **params)
Expand Down
20 changes: 2 additions & 18 deletions playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
from_nullable_channel,
)
from playwright._impl._console_message import ConsoleMessage
from playwright._impl._dialog import Dialog
from playwright._impl._download import Download
from playwright._impl._element_handle import ElementHandle
from playwright._impl._event_context_manager import EventContextManagerImpl
Expand Down Expand Up @@ -159,14 +158,7 @@ def __init__(
lambda params: self._on_binding(from_channel(params["binding"])),
)
self._channel.on("close", lambda _: self._on_close())
self._channel.on(
"console",
lambda params: self.emit(
Page.Events.Console, from_channel(params["message"])
),
)
self._channel.on("crash", lambda _: self._on_crash())
self._channel.on("dialog", lambda params: self._on_dialog(params))
self._channel.on("download", lambda params: self._on_download(params))
self._channel.on(
"fileChooser",
Expand Down Expand Up @@ -223,6 +215,8 @@ def __init__(

self._set_event_to_subscription_mapping(
{
Page.Events.Console: "console",
Page.Events.Dialog: "dialog",
Page.Events.Request: "request",
Page.Events.Response: "response",
Page.Events.RequestFinished: "requestFinished",
Expand Down Expand Up @@ -286,16 +280,6 @@ def _on_close(self) -> None:
def _on_crash(self) -> None:
self.emit(Page.Events.Crash, self)

def _on_dialog(self, params: Any) -> None:
dialog = cast(Dialog, from_channel(params["dialog"]))
if self.listeners(Page.Events.Dialog):
self.emit(Page.Events.Dialog, dialog)
else:
if dialog.type == "beforeunload":
asyncio.create_task(dialog.accept())
else:
asyncio.create_task(dialog.dismiss())

def _on_download(self, params: Any) -> None:
url = params["url"]
suggested_filename = params["suggestedFilename"]
Expand Down
Loading