Skip to content

Commit 344c56f

Browse files
authored
chore: roll Playwright to 1.32.0-alpha-mar-15-2023 (microsoft#1812)
1 parent e45a219 commit 344c56f

20 files changed

+881
-553
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Playwright is a Python library to automate [Chromium](https://www.chromium.org/H
44

55
| | Linux | macOS | Windows |
66
| :--- | :---: | :---: | :---: |
7-
| Chromium <!-- GEN:chromium-version -->111.0.5563.19<!-- GEN:stop --> ||||
7+
| Chromium <!-- GEN:chromium-version -->112.0.5615.20<!-- GEN:stop --> ||||
88
| WebKit <!-- GEN:webkit-version -->16.4<!-- GEN:stop --> ||||
9-
| Firefox <!-- GEN:firefox-version -->109.0<!-- GEN:stop --> ||||
9+
| Firefox <!-- GEN:firefox-version -->110.0.1<!-- GEN:stop --> ||||
1010

1111
## Documentation
1212

playwright/_impl/_browser_context.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
from playwright._impl._frame import Frame
4949
from playwright._impl._har_router import HarRouter
5050
from playwright._impl._helper import (
51+
HarContentPolicy,
52+
HarMode,
5153
HarRecordingMetadata,
5254
RouteFromHarNotFoundPolicy,
5355
RouteHandler,
@@ -326,31 +328,37 @@ async def _record_into_har(
326328
har: Union[Path, str],
327329
page: Optional[Page] = None,
328330
url: Union[Pattern[str], str] = None,
331+
content: HarContentPolicy = None,
332+
mode: HarMode = None,
329333
) -> None:
330334
params: Dict[str, Any] = {
331335
"options": prepare_record_har_options(
332336
{
333337
"recordHarPath": har,
334-
"recordHarContent": "attach",
335-
"recordHarMode": "minimal",
338+
"recordHarContent": content or "attach",
339+
"recordHarMode": mode or "minimal",
336340
"recordHarUrlFilter": url,
337341
}
338342
)
339343
}
340344
if page:
341345
params["page"] = page._channel
342346
har_id = await self._channel.send("harStart", params)
343-
self._har_recorders[har_id] = {"path": str(har), "content": "attach"}
347+
self._har_recorders[har_id] = {"path": str(har), "content": content or "attach"}
344348

345349
async def route_from_har(
346350
self,
347351
har: Union[Path, str],
348352
url: Union[Pattern[str], str] = None,
349353
not_found: RouteFromHarNotFoundPolicy = None,
350354
update: bool = None,
355+
content: HarContentPolicy = None,
356+
mode: HarMode = None,
351357
) -> None:
352358
if update:
353-
await self._record_into_har(har=har, page=None, url=url)
359+
await self._record_into_har(
360+
har=har, page=None, url=url, content=content, mode=mode
361+
)
354362
return
355363
router = await HarRouter.create(
356364
local_utils=self._connection.local_utils,

playwright/_impl/_connection.py

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import asyncio
1616
import contextvars
17+
import datetime
1718
import inspect
1819
import sys
1920
import traceback
@@ -33,6 +34,12 @@
3334
from playwright._impl._playwright import Playwright
3435

3536

37+
if sys.version_info >= (3, 8): # pragma: no cover
38+
from typing import TypedDict
39+
else: # pragma: no cover
40+
from typing_extensions import TypedDict
41+
42+
3643
class Channel(AsyncIOEventEmitter):
3744
def __init__(self, connection: "Connection", guid: str) -> None:
3845
super().__init__()
@@ -220,10 +227,11 @@ def __init__(
220227
self._error: Optional[BaseException] = None
221228
self.is_remote = False
222229
self._init_task: Optional[asyncio.Task] = None
223-
self._api_zone: contextvars.ContextVar[Optional[Dict]] = contextvars.ContextVar(
224-
"ApiZone", default=None
225-
)
230+
self._api_zone: contextvars.ContextVar[
231+
Optional[ParsedStackTrace]
232+
] = contextvars.ContextVar("ApiZone", default=None)
226233
self._local_utils: Optional["LocalUtils"] = local_utils
234+
self._stack_collector: List[List[Dict[str, Any]]] = []
227235

228236
@property
229237
def local_utils(self) -> "LocalUtils":
@@ -271,6 +279,13 @@ def call_on_object_with_known_name(
271279
) -> None:
272280
self._waiting_for_object[guid] = callback
273281

282+
def start_collecting_call_metadata(self, collector: Any) -> None:
283+
if collector not in self._stack_collector:
284+
self._stack_collector.append(collector)
285+
286+
def stop_collecting_call_metadata(self, collector: Any) -> None:
287+
self._stack_collector.remove(collector)
288+
274289
def _send_message_to_server(
275290
self, guid: str, method: str, params: Dict
276291
) -> ProtocolCallback:
@@ -283,12 +298,30 @@ def _send_message_to_server(
283298
getattr(task, "__pw_stack_trace__", traceback.extract_stack()),
284299
)
285300
self._callbacks[id] = callback
301+
stack_trace_information = cast(ParsedStackTrace, self._api_zone.get())
302+
for collector in self._stack_collector:
303+
collector.append({"stack": stack_trace_information["frames"], "id": id})
304+
frames = stack_trace_information.get("frames", [])
305+
location = (
306+
{
307+
"file": frames[0]["file"],
308+
"line": frames[0]["line"],
309+
"column": frames[0]["column"],
310+
}
311+
if len(frames) > 0
312+
else None
313+
)
286314
message = {
287315
"id": id,
288316
"guid": guid,
289317
"method": method,
290318
"params": self._replace_channels_with_guids(params),
291-
"metadata": self._api_zone.get(),
319+
"metadata": {
320+
"wallTime": int(datetime.datetime.now().timestamp() * 1000),
321+
"apiName": stack_trace_information["apiName"],
322+
"location": location,
323+
"internal": not stack_trace_information["apiName"],
324+
},
292325
}
293326
self._transport.send(message)
294327
self._callbacks[id] = callback
@@ -412,9 +445,7 @@ async def wrap_api_call(
412445
return await cb()
413446
task = asyncio.current_task(self._loop)
414447
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
415-
metadata = _extract_metadata_from_stack(st, is_internal)
416-
if metadata:
417-
self._api_zone.set(metadata)
448+
self._api_zone.set(_extract_stack_trace_information_from_stack(st, is_internal))
418449
try:
419450
return await cb()
420451
finally:
@@ -427,9 +458,7 @@ def wrap_api_call_sync(
427458
return cb()
428459
task = asyncio.current_task(self._loop)
429460
st: List[inspect.FrameInfo] = getattr(task, "__pw_stack__", inspect.stack())
430-
metadata = _extract_metadata_from_stack(st, is_internal)
431-
if metadata:
432-
self._api_zone.set(metadata)
461+
self._api_zone.set(_extract_stack_trace_information_from_stack(st, is_internal))
433462
try:
434463
return cb()
435464
finally:
@@ -444,19 +473,25 @@ def from_nullable_channel(channel: Optional[Channel]) -> Optional[Any]:
444473
return channel._object if channel else None
445474

446475

447-
def _extract_metadata_from_stack(
476+
class StackFrame(TypedDict):
477+
file: str
478+
line: int
479+
column: int
480+
function: Optional[str]
481+
482+
483+
class ParsedStackTrace(TypedDict):
484+
frames: List[StackFrame]
485+
apiName: Optional[str]
486+
487+
488+
def _extract_stack_trace_information_from_stack(
448489
st: List[inspect.FrameInfo], is_internal: bool
449-
) -> Optional[Dict]:
450-
if is_internal:
451-
return {
452-
"apiName": "",
453-
"stack": [],
454-
"internal": True,
455-
}
490+
) -> Optional[ParsedStackTrace]:
456491
playwright_module_path = str(Path(playwright.__file__).parents[0])
457492
last_internal_api_name = ""
458493
api_name = ""
459-
stack: List[Dict] = []
494+
parsed_frames: List[StackFrame] = []
460495
for frame in st:
461496
is_playwright_internal = frame.filename.startswith(playwright_module_path)
462497

@@ -466,10 +501,11 @@ def _extract_metadata_from_stack(
466501
method_name += frame[0].f_code.co_name
467502

468503
if not is_playwright_internal:
469-
stack.append(
504+
parsed_frames.append(
470505
{
471506
"file": frame.filename,
472507
"line": frame.lineno,
508+
"column": 0,
473509
"function": method_name,
474510
}
475511
)
@@ -480,9 +516,8 @@ def _extract_metadata_from_stack(
480516
last_internal_api_name = ""
481517
if not api_name:
482518
api_name = last_internal_api_name
483-
if api_name:
484-
return {
485-
"apiName": api_name,
486-
"stack": stack,
487-
}
488-
return None
519+
520+
return {
521+
"frames": parsed_frames,
522+
"apiName": "" if is_internal else api_name,
523+
}

playwright/_impl/_local_utils.py

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

1515
import base64
16-
from typing import Dict, List, Optional, cast
16+
from typing import Dict, Optional, cast
1717

18-
from playwright._impl._api_structures import HeadersArray, NameValue
18+
from playwright._impl._api_structures import HeadersArray
1919
from playwright._impl._connection import ChannelOwner
2020
from playwright._impl._helper import HarLookupResult, locals_to_params
2121

@@ -26,8 +26,8 @@ def __init__(
2626
) -> None:
2727
super().__init__(parent, type, guid, initializer)
2828

29-
async def zip(self, zip_file: str, entries: List[NameValue]) -> None:
30-
await self._channel.send("zip", {"zipFile": zip_file, "entries": entries})
29+
async def zip(self, params: Dict) -> None:
30+
await self._channel.send("zip", params)
3131

3232
async def har_open(self, file: str) -> None:
3333
params = locals_to_params(locals())

playwright/_impl/_locator.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
MouseButton,
4545
locals_to_params,
4646
monotonic_time,
47+
to_impl,
4748
)
4849
from playwright._impl._js_handle import Serializable, parse_value, serialize_argument
4950
from playwright._impl._str_utils import (
@@ -207,13 +208,23 @@ async def clear(
207208

208209
def locator(
209210
self,
210-
selector: str,
211+
selector_or_locator: Union[str, "Locator"],
211212
has_text: Union[str, Pattern[str]] = None,
212213
has: "Locator" = None,
213214
) -> "Locator":
215+
if isinstance(selector_or_locator, str):
216+
return Locator(
217+
self._frame,
218+
f"{self._selector} >> {selector_or_locator}",
219+
has_text=has_text,
220+
has=has,
221+
)
222+
selector_or_locator = to_impl(selector_or_locator)
223+
if selector_or_locator._frame != self._frame:
224+
raise Error("Locators must belong to the same frame.")
214225
return Locator(
215226
self._frame,
216-
f"{self._selector} >> {selector}",
227+
f"{self._selector} >> {selector_or_locator._selector}",
217228
has_text=has_text,
218229
has=has,
219230
)
@@ -663,13 +674,23 @@ def __init__(self, frame: "Frame", frame_selector: str) -> None:
663674

664675
def locator(
665676
self,
666-
selector: str,
677+
selector_or_locator: Union["Locator", str],
667678
has_text: Union[str, Pattern[str]] = None,
668679
has: "Locator" = None,
669680
) -> Locator:
681+
if isinstance(selector_or_locator, str):
682+
return Locator(
683+
self._frame,
684+
f"{self._frame_selector} >> internal:control=enter-frame >> {selector_or_locator}",
685+
has_text=has_text,
686+
has=has,
687+
)
688+
selector_or_locator = to_impl(selector_or_locator)
689+
if selector_or_locator._frame != self._frame:
690+
raise ValueError("Locators must belong to the same frame.")
670691
return Locator(
671692
self._frame,
672-
f"{self._frame_selector} >> internal:control=enter-frame >> {selector}",
693+
f"{self._frame_selector} >> internal:control=enter-frame >> {selector_or_locator._selector}",
673694
has_text=has_text,
674695
has=has,
675696
)

playwright/_impl/_page.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
ColorScheme,
6060
DocumentLoadState,
6161
ForcedColors,
62+
HarContentPolicy,
63+
HarMode,
6264
KeyboardModifier,
6365
MouseButton,
6466
ReducedMotion,
@@ -617,9 +619,13 @@ async def route_from_har(
617619
url: Union[Pattern[str], str] = None,
618620
not_found: RouteFromHarNotFoundPolicy = None,
619621
update: bool = None,
622+
content: HarContentPolicy = None,
623+
mode: HarMode = None,
620624
) -> None:
621625
if update:
622-
await self._browser_context._record_into_har(har=har, page=self, url=url)
626+
await self._browser_context._record_into_har(
627+
har=har, page=self, url=url, content=content, mode=mode
628+
)
623629
return
624630
router = await HarRouter.create(
625631
local_utils=self._connection.local_utils,

0 commit comments

Comments
 (0)