Skip to content

chore: roll to ToT driver #420

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 1 commit into from
Jan 11, 2021
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
56 changes: 55 additions & 1 deletion playwright/_impl/_api_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import sys
from typing import List, Optional
from typing import List, Optional, Union

if sys.version_info >= (3, 8): # pragma: no cover
from typing import Literal, TypedDict
Expand All @@ -40,6 +40,24 @@ class Cookie(TypedDict, total=False):
sameSite: Optional[Literal["Lax", "None", "Strict"]]


class FloatRect(TypedDict):
x: float
y: float
width: float
height: float


class Geolocation(TypedDict, total=False):
latitude: float
longitude: float
accuracy: Optional[float]


class HttpCredentials(TypedDict):
username: str
password: str


class LocalStorageEntry(TypedDict):
name: str
value: str
Expand All @@ -50,6 +68,25 @@ class OriginState(TypedDict):
localStorage: List[LocalStorageEntry]


class PdfMargins(TypedDict, total=False):
top: Optional[Union[str, float]]
right: Optional[Union[str, float]]
bottom: Optional[Union[str, float]]
left: Optional[Union[str, float]]


class Position(TypedDict):
x: float
y: float


class ProxySettings(TypedDict, total=False):
server: str
bypass: Optional[str]
username: Optional[str]
password: Optional[str]


class StorageState(TypedDict, total=False):
cookies: Optional[List[Cookie]]
origins: Optional[List[OriginState]]
Expand All @@ -65,3 +102,20 @@ class ResourceTiming(TypedDict):
requestStart: float
responseStart: float
responseEnd: float


class ViewportSize(TypedDict):
width: int
height: int


class SourceLocation(TypedDict):
url: str
lineNumber: int
columnNumber: int


class FilePayload(TypedDict):
name: str
mimeType: str
buffer: bytes
127 changes: 0 additions & 127 deletions playwright/_impl/_api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from typing import Any, Dict, Optional, Tuple, Union

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

# These are types that we use in the API. They are public and are a part of the
# stable API.

Expand All @@ -33,122 +25,3 @@ def __init__(self, message: str, stack: str = None) -> None:

class TimeoutError(Error):
pass


class ApiType:
def __eq__(self, other: Any) -> bool:
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
else:
return False

def _to_json(self) -> Dict:
return filter_out_none(self.__dict__)


class FilePayload(ApiType):
name: str
mime_type: str
buffer: bytes

def __init__(self, name: str, mime_type: str, buffer: bytes):
self.name = name
self.mime_type = mime_type
self.buffer = buffer


class FloatRect(ApiType):
x: float
y: float
width: float
height: float

@classmethod
def _parse(cls, dict: Optional[Dict]) -> Optional["FloatRect"]:
if not dict:
return None
return FloatRect(dict["x"], dict["y"], dict["width"], dict["height"])

def __init__(self, x: float, y: float, width: float, height: float):
self.x = x
self.y = y
self.width = width
self.height = height


class DeviceDescriptor(TypedDict):
user_agent: Optional[str]
viewport: Optional[Tuple[int, int]]
device_scale_factor: Optional[int]
is_mobile: Optional[bool]
has_touch: Optional[bool]


class Geolocation(ApiType):
latitude: float
longitude: float
accuracy: Optional[float]

def __init__(self, latitude: float, longitude: float, accuracy: float = None):
self.latitude = latitude
self.longitude = longitude
self.accuracy = accuracy


class PdfMargins(ApiType):
top: Optional[Union[str, float]]
right: Optional[Union[str, float]]
bottom: Optional[Union[str, float]]
left: Optional[Union[str, float]]

def __init__(
self,
top: Union[str, float],
right: Union[str, float],
bottom: Union[str, float],
left: Union[str, float],
):
self.top = top
self.right = right
self.bottom = bottom
self.left = left


class ProxySettings(ApiType):
server: str
bypass: Optional[str]
username: Optional[str]
password: Optional[str]

def __init__(
self,
server: str,
bypass: str = None,
username: str = None,
password: str = None,
):
self.server = server
self.bypass = bypass
self.username = username
self.password = password


class SourceLocation(ApiType):
url: str
line_number: int
column_number: int

def __init__(self, url: str, line_number: int, column_number: int):
self.url = url
self.line_number = line_number
self.column_number = column_number


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
42 changes: 20 additions & 22 deletions playwright/_impl/_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@
# limitations under the License.

import json
import sys
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Dict, List, Tuple, Union

from playwright._impl._api_structures import StorageState
from playwright._impl._api_types import Geolocation, ProxySettings
from typing import TYPE_CHECKING, Dict, List, Union

from playwright._impl._api_structures import (
Geolocation,
HttpCredentials,
ProxySettings,
StorageState,
ViewportSize,
)
from playwright._impl._browser_context import BrowserContext
from playwright._impl._connection import ChannelOwner, from_channel
from playwright._impl._helper import ColorScheme, is_safe_close_error, locals_to_params
from playwright._impl._network import serialize_headers
from playwright._impl._page import Page

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

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

Expand Down Expand Up @@ -66,7 +65,8 @@ def is_connected(self) -> bool:

async def new_context(
self,
viewport: Union[Tuple[int, int], Literal[0]] = None,
viewport: ViewportSize = None,
noViewport: bool = None,
ignoreHTTPSErrors: bool = None,
javaScriptEnabled: bool = None,
bypassCSP: bool = None,
Expand All @@ -77,7 +77,7 @@ async def new_context(
permissions: List[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: Tuple[str, str] = None,
httpCredentials: HttpCredentials = None,
deviceScaleFactor: float = None,
isMobile: bool = None,
hasTouch: bool = None,
Expand All @@ -88,7 +88,7 @@ async def new_context(
recordHarPath: Union[Path, str] = None,
recordHarOmitContent: bool = None,
recordVideoDir: Union[Path, str] = None,
recordVideoSize: Tuple[int, int] = None,
recordVideoSize: ViewportSize = None,
storageState: Union[StorageState, str, Path] = None,
) -> BrowserContext:
params = locals_to_params(locals())
Expand All @@ -103,7 +103,8 @@ async def new_context(

async def new_page(
self,
viewport: Union[Tuple[int, int], Literal[0]] = None,
viewport: ViewportSize = None,
noViewport: bool = None,
ignoreHTTPSErrors: bool = None,
javaScriptEnabled: bool = None,
bypassCSP: bool = None,
Expand All @@ -114,7 +115,7 @@ async def new_page(
permissions: List[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: Tuple[str, str] = None,
httpCredentials: HttpCredentials = None,
deviceScaleFactor: float = None,
isMobile: bool = None,
hasTouch: bool = None,
Expand All @@ -125,7 +126,7 @@ async def new_page(
recordHarPath: Union[Path, str] = None,
recordHarOmitContent: bool = None,
recordVideoDir: Union[Path, str] = None,
recordVideoSize: Tuple[int, int] = None,
recordVideoSize: ViewportSize = None,
storageState: Union[StorageState, str, Path] = None,
) -> Page:
params = locals_to_params(locals())
Expand All @@ -151,8 +152,8 @@ def version(self) -> str:


def normalize_context_params(params: Dict) -> None:
if "viewport" in params and params["viewport"] == 0:
del params["viewport"]
if params.get("noViewport"):
del params["noViewport"]
params["noDefaultViewport"] = True
if "defaultBrowserType" in params:
del params["defaultBrowserType"]
Expand All @@ -167,10 +168,7 @@ def normalize_context_params(params: Dict) -> None:
if "recordVideoDir" in params:
params["recordVideo"] = {"dir": str(params["recordVideoDir"])}
if "recordVideoSize" in params:
params["recordVideo"]["size"] = {
"width": params["recordVideoSize"][0],
"height": params["recordVideoSize"][1],
}
params["recordVideo"]["size"] = params["recordVideoSize"]
del params["recordVideoSize"]
del params["recordVideoDir"]
if "storageState" in params:
Expand Down
13 changes: 3 additions & 10 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast

from playwright._impl._api_structures import Cookie, StorageState
from playwright._impl._api_structures import Cookie, Geolocation, StorageState
from playwright._impl._api_types import Error
from playwright._impl._connection import ChannelOwner, from_channel
from playwright._impl._event_context_manager import EventContextManagerImpl
Expand Down Expand Up @@ -141,15 +141,8 @@ async def grant_permissions(
async def clear_permissions(self) -> None:
await self._channel.send("clearPermissions")

async def set_geolocation(
self, latitude: float, longitude: float, accuracy: Optional[float]
) -> None:
await self._channel.send(
"setGeolocation", {"geolocation": locals_to_params(locals())}
)

async def reset_geolocation(self) -> None:
await self._channel.send("setGeolocation", {})
async def set_geolocation(self, geolocation: Geolocation = None) -> None:
await self._channel.send("setGeolocation", locals_to_params(locals()))

async def set_extra_http_headers(self, headers: Dict[str, str]) -> None:
await self._channel.send(
Expand Down
Loading