@@ -262,19 +262,28 @@ def _get_element(self, elem_or_selector):
262
262
return self .find_element (elem_or_selector )
263
263
return elem_or_selector
264
264
265
- def _wait_for (self , method , args , timeout , msg ):
265
+ def _wait_for (self , method , timeout , msg ):
266
266
"""Abstract generic pattern for explicit WebDriverWait."""
267
- _wait = (
268
- self ._wd_wait if timeout is None else WebDriverWait (self .driver , timeout )
269
- )
270
- logger .debug (
271
- "method, timeout, poll => %s %s %s" ,
272
- method ,
273
- _wait ._timeout , # pylint: disable=protected-access
274
- _wait ._poll , # pylint: disable=protected-access
275
- )
267
+ try :
268
+ _wait = (
269
+ self ._wd_wait
270
+ if timeout is None
271
+ else WebDriverWait (self .driver , timeout )
272
+ )
273
+ logger .debug (
274
+ "method, timeout, poll => %s %s %s" ,
275
+ method ,
276
+ _wait ._timeout , # pylint: disable=protected-access
277
+ _wait ._poll , # pylint: disable=protected-access
278
+ )
276
279
277
- return _wait .until (method (* args ), msg )
280
+ return _wait .until (method )
281
+ except Exception as err :
282
+ if callable (msg ):
283
+ message = msg (self .driver )
284
+ else :
285
+ message = msg
286
+ raise TimeoutException (message ) from err
278
287
279
288
def wait_for_element (self , selector , timeout = None ):
280
289
"""wait_for_element is shortcut to `wait_for_element_by_css_selector`
@@ -286,8 +295,9 @@ def wait_for_element_by_css_selector(self, selector, timeout=None):
286
295
equals to the fixture's `wait_timeout` shortcut to `WebDriverWait` with
287
296
`EC.presence_of_element_located`."""
288
297
return self ._wait_for (
289
- EC .presence_of_element_located ,
290
- ((By .CSS_SELECTOR , selector ),),
298
+ EC .presence_of_element_located (
299
+ (By .CSS_SELECTOR , selector ),
300
+ ),
291
301
timeout ,
292
302
f"timeout { timeout or self ._wait_timeout } s => waiting for selector { selector } " ,
293
303
)
@@ -310,8 +320,9 @@ def wait_for_element_by_id(self, element_id, timeout=None):
310
320
equals to the fixture's `wait_timeout` shortcut to `WebDriverWait` with
311
321
`EC.presence_of_element_located`."""
312
322
return self ._wait_for (
313
- EC .presence_of_element_located ,
314
- ((By .ID , element_id ),),
323
+ EC .presence_of_element_located (
324
+ (By .ID , element_id ),
325
+ ),
315
326
timeout ,
316
327
f"timeout { timeout or self ._wait_timeout } s => waiting for element id { element_id } " ,
317
328
)
@@ -321,8 +332,7 @@ def wait_for_class_to_equal(self, selector, classname, timeout=None):
321
332
if not set, equals to the fixture's `wait_timeout` shortcut to
322
333
`WebDriverWait` with customized `class_to_equal` condition."""
323
334
return self ._wait_for (
324
- method = class_to_equal ,
325
- args = (selector , classname ),
335
+ method = class_to_equal (selector , classname ),
326
336
timeout = timeout ,
327
337
msg = f"classname => { classname } not found within { timeout or self ._wait_timeout } s" ,
328
338
)
@@ -332,8 +342,7 @@ def wait_for_style_to_equal(self, selector, style, val, timeout=None):
332
342
if not set, equals to the fixture's `wait_timeout` shortcut to
333
343
`WebDriverWait` with customized `style_to_equal` condition."""
334
344
return self ._wait_for (
335
- method = style_to_equal ,
336
- args = (selector , style , val ),
345
+ method = style_to_equal (selector , style , val ),
337
346
timeout = timeout ,
338
347
msg = f"style val => { style } { val } not found within { timeout or self ._wait_timeout } s" ,
339
348
)
@@ -345,11 +354,12 @@ def wait_for_text_to_equal(self, selector, text, timeout=None):
345
354
shortcut to `WebDriverWait` with customized `text_to_equal`
346
355
condition.
347
356
"""
357
+ method = text_to_equal (selector , text , timeout or self .wait_timeout )
358
+
348
359
return self ._wait_for (
349
- method = text_to_equal ,
350
- args = (selector , text ),
360
+ method = method ,
351
361
timeout = timeout ,
352
- msg = f"text -> { text } not found within { timeout or self . _wait_timeout } s" ,
362
+ msg = method . message ,
353
363
)
354
364
355
365
def wait_for_contains_class (self , selector , classname , timeout = None ):
@@ -360,8 +370,7 @@ def wait_for_contains_class(self, selector, classname, timeout=None):
360
370
condition.
361
371
"""
362
372
return self ._wait_for (
363
- method = contains_class ,
364
- args = (selector , classname ),
373
+ method = contains_class (selector , classname ),
365
374
timeout = timeout ,
366
375
msg = f"classname -> { classname } not found inside element within { timeout or self ._wait_timeout } s" ,
367
376
)
@@ -373,11 +382,11 @@ def wait_for_contains_text(self, selector, text, timeout=None):
373
382
shortcut to `WebDriverWait` with customized `contains_text`
374
383
condition.
375
384
"""
385
+ method = contains_text (selector , text , timeout or self .wait_timeout )
376
386
return self ._wait_for (
377
- method = contains_text ,
378
- args = (selector , text ),
387
+ method = method ,
379
388
timeout = timeout ,
380
- msg = f"text -> { text } not found inside element within { timeout or self . _wait_timeout } s" ,
389
+ msg = method . message ,
381
390
)
382
391
383
392
def wait_for_page (self , url = None , timeout = 10 ):
0 commit comments