Skip to content

Commit 91c54d9

Browse files
authored
feat(roll): roll Playwright to 1.2.0-next.1595094993390 (#69)
1 parent 0871e3f commit 91c54d9

20 files changed

+241
-172
lines changed

driver/package-lock.json

Lines changed: 9 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

driver/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"license": "Apache-2.0",
1515
"dependencies": {
16-
"playwright": "1.2.0-next.1594412304701"
16+
"playwright": "1.2.0-next.1595094993390"
1717
},
1818
"devDependencies": {
1919
"pkg": "^4.4.9"

playwright/browser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from playwright.browser_context import BrowserContext
1717
from playwright.connection import ChannelOwner, ConnectionScope, from_channel
1818
from playwright.helper import locals_to_params, ColorScheme
19+
from playwright.network import serialize_headers
1920
from playwright.page import Page
2021
from types import SimpleNamespace
2122
from typing import Dict, List, Union
@@ -37,9 +38,6 @@ def __init__(self, scope: ConnectionScope, guid: str, initializer: Dict) -> None
3738

3839
self._contexts: List[BrowserContext] = list()
3940
self._channel.on("close", lambda _: self._on_close())
40-
self._channel.on(
41-
"contextClosed", lambda context: self._contexts.remove(context)
42-
)
4341

4442
def _on_close(self) -> None:
4543
self._is_connected = False
@@ -77,6 +75,8 @@ async def newContext(
7775
params = locals_to_params(locals())
7876
if viewport == 0:
7977
params["viewport"] = None
78+
if extraHTTPHeaders:
79+
params["extraHTTPHeaders"] = serialize_headers(extraHTTPHeaders)
8080
channel = await self._channel.send("newContext", params)
8181
context = from_channel(channel)
8282
self._contexts.append(context)

playwright/browser_context.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
URLMatch,
3030
URLMatcher,
3131
)
32-
from playwright.network import Request, Route
32+
from playwright.network import Request, Route, serialize_headers
3333
from playwright.page import BindingCall, Page, wait_for_event
3434
from types import SimpleNamespace
3535
from typing import Any, Callable, Dict, List, Optional, Union, TYPE_CHECKING
@@ -53,20 +53,18 @@ def __init__(self, scope: ConnectionScope, guid: str, initializer: Dict) -> None
5353
self._owner_page: Optional[Page] = None
5454
self._is_closed_or_closing = False
5555

56-
for channel in initializer["pages"]:
57-
page = from_channel(channel)
58-
self._pages.append(page)
59-
page._set_browser_context(self)
6056
self._channel.on(
6157
"bindingCall",
62-
lambda binding_call: self._on_binding(from_channel(binding_call)),
58+
lambda params: self._on_binding(from_channel(params["binding"])),
6359
)
6460
self._channel.on("close", lambda _: self._on_close())
65-
self._channel.on("page", lambda page: self._on_page(from_channel(page)))
61+
self._channel.on(
62+
"page", lambda params: self._on_page(from_channel(params["page"]))
63+
)
6664
self._channel.on(
6765
"route",
68-
lambda event: self._on_route(
69-
from_channel(event.get("route")), from_channel(event.get("request"))
66+
lambda params: self._on_route(
67+
from_channel(params.get("route")), from_channel(params.get("request"))
7068
),
7169
)
7270

@@ -136,7 +134,9 @@ async def setGeolocation(self, geolocation: Optional[Dict]) -> None:
136134
await self._channel.send("setGeolocation", dict(geolocation=geolocation))
137135

138136
async def setExtraHTTPHeaders(self, headers: Dict) -> None:
139-
await self._channel.send("setExtraHTTPHeaders", dict(headers=headers))
137+
await self._channel.send(
138+
"setExtraHTTPHeaders", dict(headers=serialize_headers(headers))
139+
)
140140

141141
async def setOffline(self, offline: bool) -> None:
142142
await self._channel.send("setOffline", dict(offline=offline))

playwright/browser_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
class BrowserType(ChannelOwner):
2323
def __init__(self, scope: ConnectionScope, guid: str, initializer: Dict) -> None:
24-
super().__init__(scope, guid, initializer)
24+
super().__init__(scope, guid, initializer, True)
2525

2626
@property
2727
def name(self) -> str:

playwright/connection.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
import sys
1617
import traceback
1718
from playwright.helper import parse_error, ParsedMessagePayload
1819
from playwright.transport import Transport
@@ -30,7 +31,13 @@ def __init__(self, scope: "ConnectionScope", guid: str) -> None:
3031
async def send(self, method: str, params: dict = None) -> Any:
3132
if params is None:
3233
params = dict()
33-
return await self._scope.send_message_to_server(self._guid, method, params)
34+
result = await self._scope.send_message_to_server(self._guid, method, params)
35+
# Protocol now has named return values, assume result is one level deeper unless
36+
# there is explicit ambiguity.
37+
if isinstance(result, dict) and len(result) == 1:
38+
key = next(iter(result))
39+
return result[key]
40+
return result
3441

3542

3643
class ChannelOwner(BaseEventEmitter):
@@ -172,7 +179,13 @@ def _dispatch(self, msg: ParsedMessagePayload) -> None:
172179
return
173180

174181
object = self._objects[guid]
175-
object._channel.emit(method, self._replace_guids_with_channels(params))
182+
try:
183+
object._channel.emit(method, self._replace_guids_with_channels(params))
184+
except Exception:
185+
print(
186+
"Error dispatching the event",
187+
"".join(traceback.format_exception(*sys.exc_info())),
188+
)
176189

177190
def _replace_channels_with_guids(self, payload: Any) -> Any:
178191
if payload is None:

playwright/drivers/browsers.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
},
88
{
99
"name": "firefox",
10-
"revision": "1125"
10+
"revision": "1128"
1111
},
1212
{
1313
"name": "webkit",
14-
"revision": "1305"
14+
"revision": "1308"
1515
}
1616
]
17-
}
17+
}

playwright/element_handle.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@
1313
# limitations under the License.
1414

1515
import base64
16+
import mimetypes
17+
import os
1618
import sys
1719
from playwright.connection import (
1820
ConnectionScope,
1921
from_nullable_channel,
2022
)
2123
from playwright.helper import (
2224
FilePayload,
23-
SelectOption,
24-
locals_to_params,
2525
KeyboardModifier,
2626
MouseButton,
27+
SelectOption,
28+
locals_to_params,
2729
)
2830
from playwright.js_handle import parse_result, serialize_argument, JSHandle
31+
2932
from typing import (
3033
Any,
3134
Callable,
@@ -113,10 +116,12 @@ async def dblclick(
113116

114117
async def selectOption(
115118
self, values: "ValuesToSelect", timeout: int = None, noWaitAfter: bool = None
116-
) -> None:
119+
) -> List[str]:
117120
params = locals_to_params(locals())
118-
params["values"] = convertSelectOptionValues(values)
119-
await self._channel.send("selectOption", params)
121+
if "values" in params:
122+
values = params.pop("values")
123+
params = {**params, **convert_select_option_values(values)}
124+
return await self._channel.send("selectOption", params)
120125

121126
async def fill(
122127
self, value: str, timeout: int = None, noWaitAfter: bool = None
@@ -132,7 +137,9 @@ async def setInputFiles(
132137
timeout: int = None,
133138
noWaitAfter: bool = None,
134139
) -> None:
135-
await self._channel.send("setInputFiles", locals_to_params(locals()))
140+
params = locals_to_params(locals())
141+
params["files"] = normalize_file_payloads(files)
142+
await self._channel.send("setInputFiles", params)
136143

137144
async def focus(self) -> None:
138145
await self._channel.send("focus")
@@ -230,11 +237,38 @@ async def evalOnSelectorAll(
230237
]
231238

232239

233-
def convertSelectOptionValues(arg: ValuesToSelect) -> Any:
240+
def convert_select_option_values(arg: ValuesToSelect) -> Any:
234241
if arg is None:
235-
return []
236-
if isinstance(arg, ElementHandle):
237-
return arg._channel
238-
if isinstance(arg, list) and len(arg) and isinstance(arg[0], ElementHandle):
239-
return list(map(lambda e: e._channel, arg))
240-
return arg
242+
return dict()
243+
arg_list = arg if isinstance(arg, list) else [arg]
244+
if not len(arg_list):
245+
return dict()
246+
if isinstance(arg_list[0], ElementHandle):
247+
element_list = cast(List[ElementHandle], arg_list)
248+
return dict(elements=list(map(lambda e: e._channel, element_list)))
249+
if isinstance(arg_list[0], str):
250+
return dict(options=list(map(lambda e: dict(value=e), arg_list)))
251+
return dict(options=arg_list)
252+
253+
254+
def normalize_file_payloads(
255+
files: Union[str, FilePayload, List[str], List[FilePayload]]
256+
) -> List[FilePayload]:
257+
file_list = files if isinstance(files, list) else [files]
258+
file_payloads: List[FilePayload] = []
259+
for item in file_list:
260+
if isinstance(item, str):
261+
with open(item, mode="rb") as fd:
262+
file: FilePayload = {
263+
"name": os.path.basename(item),
264+
"mimeType": mimetypes.guess_type(item)[0]
265+
or "application/octet-stream",
266+
"buffer": base64.b64encode(fd.read()).decode(),
267+
}
268+
file_payloads.append(file)
269+
else:
270+
if isinstance(item["buffer"], bytes):
271+
item["buffer"] = base64.b64encode(item["buffer"]).decode()
272+
file_payloads.append(item)
273+
274+
return file_payloads

0 commit comments

Comments
 (0)