forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
146 lines (130 loc) · 4.88 KB
/
browser.py
File metadata and controls
146 lines (130 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
from types import SimpleNamespace
from typing import TYPE_CHECKING, Dict, List, Union
from playwright.browser_context import BrowserContext
from playwright.connection import ChannelOwner, from_channel
from playwright.helper import (
ColorScheme,
Credentials,
Geolocation,
IntSize,
locals_to_params,
)
from playwright.network import serialize_headers
from playwright.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.browser_type import BrowserType
class Browser(ChannelOwner):
Events = SimpleNamespace(
Disconnected="disconnected",
)
def __init__(
self, parent: "BrowserType", type: str, guid: str, initializer: Dict
) -> None:
super().__init__(parent, type, guid, initializer)
self._browser_type = parent
self._is_connected = True
self._is_closed_or_closing = False
self._contexts: List[BrowserContext] = []
self._channel.on("close", lambda _: self._on_close())
def _on_close(self) -> None:
self._is_connected = False
self.emit(Browser.Events.Disconnected)
self._is_closed_or_closing = True
@property
def contexts(self) -> List[BrowserContext]:
return self._contexts.copy()
def isConnected(self) -> bool:
return self._is_connected
async def newContext(
self,
viewport: Union[IntSize, Literal[0]] = None,
ignoreHTTPSErrors: bool = None,
javaScriptEnabled: bool = None,
bypassCSP: bool = None,
userAgent: str = None,
locale: str = None,
timezoneId: str = None,
geolocation: Geolocation = None,
permissions: List[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: Credentials = None,
deviceScaleFactor: int = None,
isMobile: bool = None,
hasTouch: bool = None,
colorScheme: ColorScheme = None,
acceptDownloads: bool = None,
defaultBrowserType: str = None,
) -> BrowserContext:
params = locals_to_params(locals())
# Python is strict in which variables gets passed to methods. We get this
# value from the device descriptors, thats why we have to strip it out.
if "defaultBrowserType" in params:
del params["defaultBrowserType"]
if viewport == 0:
del params["viewport"]
params["noDefaultViewport"] = True
if extraHTTPHeaders:
params["extraHTTPHeaders"] = serialize_headers(extraHTTPHeaders)
channel = await self._channel.send("newContext", params)
context = from_channel(channel)
self._contexts.append(context)
context._browser = self
return context
async def newPage(
self,
viewport: Union[IntSize, Literal[0]] = None,
ignoreHTTPSErrors: bool = None,
javaScriptEnabled: bool = None,
bypassCSP: bool = None,
userAgent: str = None,
locale: str = None,
timezoneId: str = None,
geolocation: Geolocation = None,
permissions: List[str] = None,
extraHTTPHeaders: Dict[str, str] = None,
offline: bool = None,
httpCredentials: Credentials = None,
deviceScaleFactor: int = None,
isMobile: bool = None,
hasTouch: bool = None,
colorScheme: ColorScheme = None,
acceptDownloads: bool = None,
defaultBrowserType: str = None,
) -> Page:
params = locals_to_params(locals())
# Python is strict in which variables gets passed to methods. We get this
# value from the device descriptors, thats why we have to strip it out.
if "defaultBrowserType" in params:
del params["defaultBrowserType"]
context = await self.newContext(**params)
page = await context.newPage()
page._owned_context = context
context._owner_page = page
return page
async def close(self) -> None:
if self._is_closed_or_closing:
return
self._is_closed_or_closing = True
await self._channel.send("close")
@property
def version(self) -> str:
return self._initializer["version"]