Skip to content
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

chore(roll): roll Playwright to 1.40.0 #2170

Merged
merged 1 commit into from
Nov 21, 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
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 -->119.0.6045.21<!-- GEN:stop --> ||||
| Chromium <!-- GEN:chromium-version -->120.0.6099.28<!-- GEN:stop --> ||||
| WebKit <!-- GEN:webkit-version -->17.4<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->118.0.1<!-- GEN:stop --> ||||
| Firefox <!-- GEN:firefox-version -->119.0<!-- GEN:stop --> ||||

## Documentation

Expand Down
5 changes: 3 additions & 2 deletions playwright/_impl/_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,11 @@ async def to_have_attribute(
self,
name: str,
value: Union[str, Pattern[str]],
ignore_case: bool = None,
timeout: float = None,
) -> None:
__tracebackhide__ = True
expected_text = to_expected_text_values([value])
expected_text = to_expected_text_values([value], ignore_case=ignore_case)
await self._expect_impl(
"to.have.attribute.value",
FrameExpectOptions(
Expand All @@ -235,7 +236,7 @@ async def not_to_have_attribute(
timeout: float = None,
) -> None:
__tracebackhide__ = True
await self._not.to_have_attribute(name, value, timeout)
await self._not.to_have_attribute(name, value, timeout=timeout)

async def to_have_class(
self,
Expand Down
1 change: 1 addition & 0 deletions playwright/_impl/_browser_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ async def launch_persistent_context(
acceptDownloads: bool = None,
tracesDir: Union[pathlib.Path, str] = None,
chromiumSandbox: bool = None,
firefoxUserPrefs: Dict[str, Union[str, float, bool]] = None,
recordHarPath: Union[Path, str] = None,
recordHarOmitContent: bool = None,
recordVideoDir: Union[Path, str] = None,
Expand Down
35 changes: 13 additions & 22 deletions playwright/_impl/_element_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast

from playwright._impl._api_structures import FilePayload, FloatRect, Position
from playwright._impl._connection import ChannelOwner, from_nullable_channel
from playwright._impl._connection import (
ChannelOwner,
filter_none,
from_nullable_channel,
)
from playwright._impl._helper import (
Error,
KeyboardModifier,
Expand Down Expand Up @@ -191,20 +195,17 @@ async def set_input_files(
timeout: float = None,
noWaitAfter: bool = None,
) -> None:
params = locals_to_params(locals())
frame = await self.owner_frame()
if not frame:
raise Error("Cannot set input files to detached element")
converted = await convert_input_files(files, frame.page.context)
if converted["files"] is not None:
await self._channel.send(
"setInputFiles", {**params, "files": converted["files"]}
)
else:
await self._channel.send(
"setInputFilePaths",
locals_to_params({**params, **converted, "files": None}),
)
await self._channel.send(
"setInputFiles",
{
**filter_none({"timeout": timeout, "noWaitAfter": noWaitAfter}),
**converted,
},
)

async def focus(self) -> None:
await self._channel.send("focus")
Expand Down Expand Up @@ -407,14 +408,4 @@ def convert_select_option_values(
element = [element]
elements = list(map(lambda e: e._channel, element))

return filter_out_none(dict(options=options, elements=elements))


def filter_out_none(args: Dict) -> Any:
copy = {}
for key in args:
if key == "self":
continue
if args[key] is not None:
copy[key] = args[key]
return copy
return filter_none(dict(options=options, elements=elements))
25 changes: 15 additions & 10 deletions playwright/_impl/_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from playwright._impl._api_structures import AriaRole, FilePayload, Position
from playwright._impl._connection import (
ChannelOwner,
filter_none,
from_channel,
from_nullable_channel,
)
Expand Down Expand Up @@ -689,17 +690,21 @@ async def set_input_files(
timeout: float = None,
noWaitAfter: bool = None,
) -> None:
params = locals_to_params(locals())
converted = await convert_input_files(files, self.page.context)
if converted["files"] is not None:
await self._channel.send(
"setInputFiles", {**params, "files": converted["files"]}
)
else:
await self._channel.send(
"setInputFilePaths",
locals_to_params({**params, **converted, "files": None}),
)
await self._channel.send(
"setInputFiles",
{
**filter_none(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might follow up and do it like in .NET that we remove None by default in send().

{
"selector": selector,
"strict": strict,
"timeout": timeout,
"noWaitAfter": noWaitAfter,
}
),
**converted,
},
)

async def type(
self,
Expand Down
102 changes: 41 additions & 61 deletions playwright/_impl/_set_input_files_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Union, cast

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

from playwright._impl._connection import Channel, from_channel
from playwright._impl._helper import Error, async_readfile
from playwright._impl._helper import Error
from playwright._impl._writable_stream import WritableStream

if TYPE_CHECKING: # pragma: no cover
Expand All @@ -34,81 +34,61 @@
SIZE_LIMIT_IN_BYTES = 50 * 1024 * 1024


class InputFilesList(TypedDict):
class InputFilesList(TypedDict, total=False):
streams: Optional[List[Channel]]
localPaths: Optional[List[str]]
files: Optional[List[FilePayload]]
payloads: Optional[List[Dict[str, Union[str, bytes]]]]


async def convert_input_files(
files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]],
context: "BrowserContext",
) -> InputFilesList:
file_list = files if isinstance(files, list) else [files]
items = files if isinstance(files, list) else [files]

total_buffer_size_exceeds_limit = (
sum(
[
len(f.get("buffer", ""))
for f in file_list
if not isinstance(f, (str, Path))
]
)
> SIZE_LIMIT_IN_BYTES
)
if total_buffer_size_exceeds_limit:
raise Error(
"Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead."
)

total_file_size_exceeds_limit = (
sum([os.stat(f).st_size for f in file_list if isinstance(f, (str, Path))])
> SIZE_LIMIT_IN_BYTES
)
if total_file_size_exceeds_limit:
if any([isinstance(item, (str, Path)) for item in items]):
if not all([isinstance(item, (str, Path)) for item in items]):
raise Error("File paths cannot be mixed with buffers")
if context._channel._connection.is_remote:
streams = []
for file in file_list:
assert isinstance(file, (str, Path))
for item in items:
assert isinstance(item, (str, Path))
last_modified_ms = int(os.path.getmtime(item) * 1000)
stream: WritableStream = from_channel(
await context._channel.send(
"createTempFile", {"name": os.path.basename(file)}
"createTempFile",
{
"name": os.path.basename(item),
"lastModifiedMs": last_modified_ms,
},
)
)
await stream.copy(file)
await stream.copy(item)
streams.append(stream._channel)
return InputFilesList(streams=streams, localPaths=None, files=None)
local_paths = []
for p in file_list:
assert isinstance(p, (str, Path))
local_paths.append(str(Path(p).absolute().resolve()))
return InputFilesList(streams=None, localPaths=local_paths, files=None)
return InputFilesList(streams=streams)
return InputFilesList(
localPaths=[
str(Path(cast(Union[str, Path], item)).absolute().resolve())
for item in items
]
)

return InputFilesList(
streams=None, localPaths=None, files=await _normalize_file_payloads(files)
file_payload_exceeds_size_limit = (
sum([len(f.get("buffer", "")) for f in items if not isinstance(f, (str, Path))])
> SIZE_LIMIT_IN_BYTES
)
if file_payload_exceeds_size_limit:
raise Error(
"Cannot set buffer larger than 50Mb, please write it to a file and pass its path instead."
)


async def _normalize_file_payloads(
files: Union[str, Path, FilePayload, List[Union[str, Path]], List[FilePayload]]
) -> List:
file_list = files if isinstance(files, list) else [files]
file_payloads: List = []
for item in file_list:
if isinstance(item, (str, Path)):
file_payloads.append(
{
"name": os.path.basename(item),
"buffer": base64.b64encode(await async_readfile(item)).decode(),
}
)
else:
file_payloads.append(
{
"name": item["name"],
"mimeType": item["mimeType"],
"buffer": base64.b64encode(item["buffer"]).decode(),
}
)

return file_payloads
return InputFilesList(
payloads=[
{
"name": item["name"],
"mimeType": item["mimeType"],
"buffer": base64.b64encode(item["buffer"]).decode(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it encode than decode, is it just the python way to do base64 encoding?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b64encode returns bytes which we need to decode to a str.

}
for item in cast(List[FilePayload], items)
]
)
Loading
Loading