Skip to content

Commit 13d5bb5

Browse files
committed
More timeout handling
1 parent 05ce9bc commit 13d5bb5

File tree

6 files changed

+152
-72
lines changed

6 files changed

+152
-72
lines changed

playwright/_impl/_browser_type.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
)
3636
from playwright._impl._errors import Error
3737
from playwright._impl._helper import (
38+
PLAYWRIGHT_MAX_DEADLINE,
3839
ColorScheme,
3940
Contrast,
4041
Env,
@@ -210,7 +211,6 @@ async def connect(
210211
if slowMo is None:
211212
slowMo = 0
212213

213-
timeout = timeout if timeout is not None else 0
214214
headers = {**(headers if headers else {}), "x-playwright-browser": self.name}
215215
local_utils = self._connection.local_utils
216216
pipe_channel = (
@@ -220,7 +220,7 @@ async def connect(
220220
"wsEndpoint": wsEndpoint,
221221
"headers": headers,
222222
"slowMo": slowMo,
223-
"timeout": timeout,
223+
"timeout": timeout if timeout is not None else 0,
224224
"exposeNetwork": exposeNetwork,
225225
},
226226
)
@@ -260,7 +260,10 @@ def handle_transport_close(reason: Optional[str]) -> None:
260260
connection._loop.create_task(connection.run())
261261
playwright_future = connection.playwright_future
262262

263-
timeout_future = throw_on_timeout(timeout, Error("Connection timed out"))
263+
timeout_future = throw_on_timeout(
264+
timeout if timeout is not None else PLAYWRIGHT_MAX_DEADLINE,
265+
Error("Connection timed out"),
266+
)
264267
done, pending = await asyncio.wait(
265268
{transport.on_error_future, playwright_future, timeout_future},
266269
return_when=asyncio.FIRST_COMPLETED,

playwright/_impl/_element_handle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ async def set_input_files(
213213
await self._channel.send(
214214
"setInputFiles",
215215
{
216-
"timeout": timeout,
216+
"timeout": self._frame._timeout(timeout),
217217
**converted,
218218
},
219219
)

playwright/_impl/_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ async def set_input_files(
739739
{
740740
"selector": selector,
741741
"strict": strict,
742-
"timeout": timeout,
742+
"timeout": self._timeout(timeout),
743743
**converted,
744744
},
745745
)

playwright/_impl/_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class HarLookupResult(TypedDict, total=False):
252252

253253
DEFAULT_PLAYWRIGHT_TIMEOUT_IN_MILLISECONDS = 30000
254254
DEFAULT_PLAYWRIGHT_LAUNCH_TIMEOUT_IN_MILLISECONDS = 180000
255+
PLAYWRIGHT_MAX_DEADLINE = 2147483647 # 2^31-1
255256

256257

257258
class TimeoutSettings:

playwright/_impl/_locator.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async def check(
141141
noWaitAfter: bool = None,
142142
trial: bool = None,
143143
) -> None:
144-
params = locals_to_params(locals())
144+
params = self._locals_to_params_with_timeout(locals())
145145
return await self._frame.check(self._selector, strict=True, **params)
146146

147147
async def click(
@@ -156,7 +156,7 @@ async def click(
156156
noWaitAfter: bool = None,
157157
trial: bool = None,
158158
) -> None:
159-
params = locals_to_params(locals())
159+
params = self._locals_to_params_with_timeout(locals())
160160
return await self._frame.click(self._selector, strict=True, **params)
161161

162162
async def dblclick(
@@ -170,7 +170,7 @@ async def dblclick(
170170
noWaitAfter: bool = None,
171171
trial: bool = None,
172172
) -> None:
173-
params = locals_to_params(locals())
173+
params = self._locals_to_params_with_timeout(locals())
174174
return await self._frame.dblclick(self._selector, strict=True, **params)
175175

176176
async def dispatch_event(
@@ -179,7 +179,7 @@ async def dispatch_event(
179179
eventInit: Dict = None,
180180
timeout: float = None,
181181
) -> None:
182-
params = locals_to_params(locals())
182+
params = self._locals_to_params_with_timeout(locals())
183183
return await self._frame.dispatch_event(self._selector, strict=True, **params)
184184

185185
async def evaluate(
@@ -191,7 +191,7 @@ async def evaluate(
191191
)
192192

193193
async def evaluate_all(self, expression: str, arg: Serializable = None) -> Any:
194-
params = locals_to_params(locals())
194+
params = self._locals_to_params_with_timeout(locals())
195195
return await self._frame.eval_on_selector_all(self._selector, **params)
196196

197197
async def evaluate_handle(
@@ -208,7 +208,7 @@ async def fill(
208208
noWaitAfter: bool = None,
209209
force: bool = None,
210210
) -> None:
211-
params = locals_to_params(locals())
211+
params = self._locals_to_params_with_timeout(locals())
212212
return await self._frame.fill(self._selector, strict=True, **params)
213213

214214
async def clear(
@@ -311,7 +311,7 @@ async def element_handle(
311311
self,
312312
timeout: float = None,
313313
) -> ElementHandle:
314-
params = locals_to_params(locals())
314+
params = self._locals_to_params_with_timeout(locals())
315315
handle = await self._frame.wait_for_selector(
316316
self._selector, strict=True, state="attached", **params
317317
)
@@ -377,7 +377,7 @@ def and_(self, locator: "Locator") -> "Locator":
377377
)
378378

379379
async def focus(self, timeout: float = None) -> None:
380-
params = locals_to_params(locals())
380+
params = self._locals_to_params_with_timeout(locals())
381381
return await self._frame.focus(self._selector, strict=True, **params)
382382

383383
async def blur(self, timeout: float = None) -> None:
@@ -413,14 +413,14 @@ async def drag_to(
413413
sourcePosition: Position = None,
414414
targetPosition: Position = None,
415415
) -> None:
416-
params = locals_to_params(locals())
416+
params = self._locals_to_params_with_timeout(locals())
417417
del params["target"]
418418
return await self._frame.drag_and_drop(
419419
self._selector, target._selector, strict=True, **params
420420
)
421421

422422
async def get_attribute(self, name: str, timeout: float = None) -> Optional[str]:
423-
params = locals_to_params(locals())
423+
params = self._locals_to_params_with_timeout(locals())
424424
return await self._frame.get_attribute(
425425
self._selector,
426426
strict=True,
@@ -436,79 +436,79 @@ async def hover(
436436
force: bool = None,
437437
trial: bool = None,
438438
) -> None:
439-
params = locals_to_params(locals())
439+
params = self._locals_to_params_with_timeout(locals())
440440
return await self._frame.hover(
441441
self._selector,
442442
strict=True,
443443
**params,
444444
)
445445

446446
async def inner_html(self, timeout: float = None) -> str:
447-
params = locals_to_params(locals())
447+
params = self._locals_to_params_with_timeout(locals())
448448
return await self._frame.inner_html(
449449
self._selector,
450450
strict=True,
451451
**params,
452452
)
453453

454454
async def inner_text(self, timeout: float = None) -> str:
455-
params = locals_to_params(locals())
455+
params = self._locals_to_params_with_timeout(locals())
456456
return await self._frame.inner_text(
457457
self._selector,
458458
strict=True,
459459
**params,
460460
)
461461

462462
async def input_value(self, timeout: float = None) -> str:
463-
params = locals_to_params(locals())
463+
params = self._locals_to_params_with_timeout(locals())
464464
return await self._frame.input_value(
465465
self._selector,
466466
strict=True,
467467
**params,
468468
)
469469

470470
async def is_checked(self, timeout: float = None) -> bool:
471-
params = locals_to_params(locals())
471+
params = self._locals_to_params_with_timeout(locals())
472472
return await self._frame.is_checked(
473473
self._selector,
474474
strict=True,
475475
**params,
476476
)
477477

478478
async def is_disabled(self, timeout: float = None) -> bool:
479-
params = locals_to_params(locals())
479+
params = self._locals_to_params_with_timeout(locals())
480480
return await self._frame.is_disabled(
481481
self._selector,
482482
strict=True,
483483
**params,
484484
)
485485

486486
async def is_editable(self, timeout: float = None) -> bool:
487-
params = locals_to_params(locals())
487+
params = self._locals_to_params_with_timeout(locals())
488488
return await self._frame.is_editable(
489489
self._selector,
490490
strict=True,
491491
**params,
492492
)
493493

494494
async def is_enabled(self, timeout: float = None) -> bool:
495-
params = locals_to_params(locals())
495+
params = self._locals_to_params_with_timeout(locals())
496496
return await self._frame.is_enabled(
497497
self._selector,
498498
strict=True,
499499
**params,
500500
)
501501

502502
async def is_hidden(self, timeout: float = None) -> bool:
503-
params = locals_to_params(locals())
503+
params = self._locals_to_params_with_timeout(locals())
504504
return await self._frame.is_hidden(
505505
self._selector,
506506
strict=True,
507507
**params,
508508
)
509509

510510
async def is_visible(self, timeout: float = None) -> bool:
511-
params = locals_to_params(locals())
511+
params = self._locals_to_params_with_timeout(locals())
512512
return await self._frame.is_visible(
513513
self._selector,
514514
strict=True,
@@ -522,7 +522,7 @@ async def press(
522522
timeout: float = None,
523523
noWaitAfter: bool = None,
524524
) -> None:
525-
params = locals_to_params(locals())
525+
params = self._locals_to_params_with_timeout(locals())
526526
return await self._frame.press(self._selector, strict=True, **params)
527527

528528
async def screenshot(
@@ -539,7 +539,7 @@ async def screenshot(
539539
maskColor: str = None,
540540
style: str = None,
541541
) -> bytes:
542-
params = locals_to_params(locals())
542+
params = self._locals_to_params_with_timeout(locals())
543543
return await self._with_element(
544544
lambda h, timeout: h.screenshot(
545545
**{**params, "timeout": timeout},
@@ -574,15 +574,15 @@ async def select_option(
574574
noWaitAfter: bool = None,
575575
force: bool = None,
576576
) -> List[str]:
577-
params = locals_to_params(locals())
577+
params = self._locals_to_params_with_timeout(locals())
578578
return await self._frame.select_option(
579579
self._selector,
580580
strict=True,
581581
**params,
582582
)
583583

584584
async def select_text(self, force: bool = None, timeout: float = None) -> None:
585-
params = locals_to_params(locals())
585+
params = self._locals_to_params_with_timeout(locals())
586586
return await self._with_element(
587587
lambda h, timeout: h.select_text(**{**params, "timeout": timeout}),
588588
timeout,
@@ -600,7 +600,7 @@ async def set_input_files(
600600
timeout: float = None,
601601
noWaitAfter: bool = None,
602602
) -> None:
603-
params = locals_to_params(locals())
603+
params = self._locals_to_params_with_timeout(locals())
604604
return await self._frame.set_input_files(
605605
self._selector,
606606
strict=True,
@@ -616,15 +616,15 @@ async def tap(
616616
noWaitAfter: bool = None,
617617
trial: bool = None,
618618
) -> None:
619-
params = locals_to_params(locals())
619+
params = self._locals_to_params_with_timeout(locals())
620620
return await self._frame.tap(
621621
self._selector,
622622
strict=True,
623623
**params,
624624
)
625625

626626
async def text_content(self, timeout: float = None) -> Optional[str]:
627-
params = locals_to_params(locals())
627+
params = self._locals_to_params_with_timeout(locals())
628628
return await self._frame.text_content(
629629
self._selector,
630630
strict=True,
@@ -638,7 +638,7 @@ async def type(
638638
timeout: float = None,
639639
noWaitAfter: bool = None,
640640
) -> None:
641-
params = locals_to_params(locals())
641+
params = self._locals_to_params_with_timeout(locals())
642642
return await self._frame.type(
643643
self._selector,
644644
strict=True,
@@ -662,7 +662,7 @@ async def uncheck(
662662
noWaitAfter: bool = None,
663663
trial: bool = None,
664664
) -> None:
665-
params = locals_to_params(locals())
665+
params = self._locals_to_params_with_timeout(locals())
666666
return await self._frame.uncheck(
667667
self._selector,
668668
strict=True,
@@ -859,6 +859,11 @@ def nth(self, index: int) -> "FrameLocator":
859859
def __repr__(self) -> str:
860860
return f"<FrameLocator frame={self._frame!r} selector={self._frame_selector!r}>"
861861

862+
def _locals_to_params_with_timeout(self, args: Dict) -> Dict:
863+
params = locals_to_params(args)
864+
params["timeout"] = self._frame._timeout(params.get("timeout"))
865+
return params
866+
862867

863868
_test_id_attribute_name: str = "data-testid"
864869

0 commit comments

Comments
 (0)