Skip to content

Commit 9475cff

Browse files
authored
feat: added BrowserType.connect_over_cdp (#640)
1 parent 0d30599 commit 9475cff

14 files changed

+510
-51
lines changed

local-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pytest-cov==2.11.1
1616
pytest-sugar==0.9.4
1717
pytest-timeout==1.4.2
1818
pytest-xdist==2.2.1
19+
requests==2.25.1
1920
service_identity==18.1.0
2021
setuptools==54.0.0
2122
twine==3.3.0

playwright/_impl/_browser_type.py

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

1515
from pathlib import Path
16-
from typing import Dict, List, Union
16+
from typing import Dict, List, Optional, Union, cast
1717

1818
from playwright._impl._api_structures import (
1919
Geolocation,
@@ -23,7 +23,11 @@
2323
)
2424
from playwright._impl._browser import Browser, normalize_context_params
2525
from playwright._impl._browser_context import BrowserContext
26-
from playwright._impl._connection import ChannelOwner, from_channel
26+
from playwright._impl._connection import (
27+
ChannelOwner,
28+
from_channel,
29+
from_nullable_channel,
30+
)
2731
from playwright._impl._helper import (
2832
BrowserChannel,
2933
ColorScheme,
@@ -135,6 +139,27 @@ async def launch_persistent_context(
135139
raise not_installed_error(f'"{self.name}" browser was not found.')
136140
raise e
137141

142+
async def connect_over_cdp(
143+
self, endpointURL: str, timeout: float = None, slow_mo: float = None
144+
) -> Browser:
145+
params = locals_to_params(locals())
146+
params["sdkLanguage"] = (
147+
"python" if self._connection._is_sync else "python-async"
148+
)
149+
response = await self._channel.send_return_as_dict("connectOverCDP", params)
150+
browser = cast(Browser, from_channel(response["browser"]))
151+
browser._is_remote = True
152+
153+
default_context = cast(
154+
Optional[BrowserContext],
155+
from_nullable_channel(response.get("defaultContext")),
156+
)
157+
if default_context:
158+
browser._contexts.append(default_context)
159+
default_context._browser = browser
160+
161+
return browser
162+
138163

139164
def normalize_launch_params(params: Dict) -> None:
140165
if "env" in params:

playwright/_impl/_element_handle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ async def hover(
104104
position: Position = None,
105105
timeout: float = None,
106106
force: bool = None,
107+
trial: bool = None,
107108
) -> None:
108109
await self._channel.send("hover", locals_to_params(locals()))
109110

@@ -117,6 +118,7 @@ async def click(
117118
timeout: float = None,
118119
force: bool = None,
119120
noWaitAfter: bool = None,
121+
trial: bool = None,
120122
) -> None:
121123
await self._channel.send("click", locals_to_params(locals()))
122124

@@ -129,6 +131,7 @@ async def dblclick(
129131
timeout: float = None,
130132
force: bool = None,
131133
noWaitAfter: bool = None,
134+
trial: bool = None,
132135
) -> None:
133136
await self._channel.send("dblclick", locals_to_params(locals()))
134137

@@ -157,6 +160,7 @@ async def tap(
157160
timeout: float = None,
158161
force: bool = None,
159162
noWaitAfter: bool = None,
163+
trial: bool = None,
160164
) -> None:
161165
await self._channel.send("tap", locals_to_params(locals()))
162166

@@ -205,6 +209,7 @@ async def check(
205209
timeout: float = None,
206210
force: bool = None,
207211
noWaitAfter: bool = None,
212+
trial: bool = None,
208213
) -> None:
209214
await self._channel.send("check", locals_to_params(locals()))
210215

@@ -214,6 +219,7 @@ async def uncheck(
214219
timeout: float = None,
215220
force: bool = None,
216221
noWaitAfter: bool = None,
222+
trial: bool = None,
217223
) -> None:
218224
await self._channel.send("uncheck", locals_to_params(locals()))
219225

playwright/_impl/_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ async def click(
388388
timeout: float = None,
389389
force: bool = None,
390390
noWaitAfter: bool = None,
391+
trial: bool = None,
391392
) -> None:
392393
await self._channel.send("click", locals_to_params(locals()))
393394

@@ -401,6 +402,7 @@ async def dblclick(
401402
timeout: float = None,
402403
force: bool = None,
403404
noWaitAfter: bool = None,
405+
trial: bool = None,
404406
) -> None:
405407
await self._channel.send("dblclick", locals_to_params(locals()))
406408

@@ -412,6 +414,7 @@ async def tap(
412414
timeout: float = None,
413415
force: bool = None,
414416
noWaitAfter: bool = None,
417+
trial: bool = None,
415418
) -> None:
416419
await self._channel.send("tap", locals_to_params(locals()))
417420

@@ -444,6 +447,7 @@ async def hover(
444447
position: Position = None,
445448
timeout: float = None,
446449
force: bool = None,
450+
trial: bool = None,
447451
) -> None:
448452
await self._channel.send("hover", locals_to_params(locals()))
449453

@@ -505,6 +509,7 @@ async def check(
505509
timeout: float = None,
506510
force: bool = None,
507511
noWaitAfter: bool = None,
512+
trial: bool = None,
508513
) -> None:
509514
await self._channel.send("check", locals_to_params(locals()))
510515

@@ -515,6 +520,7 @@ async def uncheck(
515520
timeout: float = None,
516521
force: bool = None,
517522
noWaitAfter: bool = None,
523+
trial: bool = None,
518524
) -> None:
519525
await self._channel.send("uncheck", locals_to_params(locals()))
520526

playwright/_impl/_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def parse_error(error: ErrorPayload) -> Error:
176176

177177

178178
def patch_error_message(message: Optional[str]) -> Optional[str]:
179-
if not message:
179+
if message is None:
180180
return None
181181

182182
match = re.match(r"(\w+)(: expected .*)", message)

playwright/_impl/_page.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ async def click(
608608
timeout: float = None,
609609
force: bool = None,
610610
noWaitAfter: bool = None,
611+
trial: bool = None,
611612
) -> None:
612613
return await self._main_frame.click(**locals_to_params(locals()))
613614

@@ -621,6 +622,7 @@ async def dblclick(
621622
timeout: float = None,
622623
force: bool = None,
623624
noWaitAfter: bool = None,
625+
trial: bool = None,
624626
) -> None:
625627
return await self._main_frame.dblclick(**locals_to_params(locals()))
626628

@@ -632,6 +634,7 @@ async def tap(
632634
timeout: float = None,
633635
force: bool = None,
634636
noWaitAfter: bool = None,
637+
trial: bool = None,
635638
) -> None:
636639
return await self._main_frame.tap(**locals_to_params(locals()))
637640

@@ -664,6 +667,7 @@ async def hover(
664667
position: Position = None,
665668
timeout: float = None,
666669
force: bool = None,
670+
trial: bool = None,
667671
) -> None:
668672
return await self._main_frame.hover(**locals_to_params(locals()))
669673

@@ -716,6 +720,7 @@ async def check(
716720
timeout: float = None,
717721
force: bool = None,
718722
noWaitAfter: bool = None,
723+
trial: bool = None,
719724
) -> None:
720725
return await self._main_frame.check(**locals_to_params(locals()))
721726

@@ -726,6 +731,7 @@ async def uncheck(
726731
timeout: float = None,
727732
force: bool = None,
728733
noWaitAfter: bool = None,
734+
trial: bool = None,
729735
) -> None:
730736
return await self._main_frame.uncheck(**locals_to_params(locals()))
731737

0 commit comments

Comments
 (0)