@@ -113,6 +113,34 @@ def hover_element(driver, element):
113
113
hover .perform ()
114
114
115
115
116
+ def format_message (exception , message ):
117
+ """
118
+ Formats an exception message to make the output cleaner.
119
+ """
120
+ if exception == Exception :
121
+ pass
122
+ elif exception == ElementNotVisibleException :
123
+ message = "ElementNotVisibleException: %s" % message
124
+ elif exception == NoSuchElementException :
125
+ message = "NoSuchElementException: %s" % message
126
+ elif exception == NoAlertPresentException :
127
+ message = "NoAlertPresentException: %s" % message
128
+ elif exception == NoSuchFrameException :
129
+ message = "NoSuchFrameException: %s" % message
130
+ elif exception == NoSuchWindowException :
131
+ message = "NoSuchWindowException: %s" % message
132
+ elif type (exception ) is str :
133
+ message = "%s: %s" % (exception , message )
134
+ else :
135
+ pass
136
+ return message
137
+
138
+
139
+ def timeout_exception (exception , message ):
140
+ message = format_message (exception , message )
141
+ raise Exception (message )
142
+
143
+
116
144
def hover_and_click (driver , hover_selector , click_selector ,
117
145
hover_by = By .CSS_SELECTOR , click_by = By .CSS_SELECTOR ,
118
146
timeout = settings .SMALL_TIMEOUT ):
@@ -145,9 +173,10 @@ def hover_and_click(driver, hover_selector, click_selector,
145
173
plural = "s"
146
174
if timeout == 1 :
147
175
plural = ""
148
- raise NoSuchElementException (
149
- "Element {%s} was not present after %s second%s!" % (
150
- click_selector , timeout , plural ))
176
+ message = (
177
+ "Element {%s} was not present after %s second%s!"
178
+ "" % (click_selector , timeout , plural ))
179
+ timeout_exception (NoSuchElementException , message )
151
180
152
181
153
182
def hover_element_and_click (driver , element , click_selector ,
@@ -173,9 +202,10 @@ def hover_element_and_click(driver, element, click_selector,
173
202
plural = "s"
174
203
if timeout == 1 :
175
204
plural = ""
176
- raise NoSuchElementException (
177
- "Element {%s} was not present after %s second%s!" % (
178
- click_selector , timeout , plural ))
205
+ message = (
206
+ "Element {%s} was not present after %s second%s!"
207
+ "" % (click_selector , timeout , plural ))
208
+ timeout_exception (NoSuchElementException , message )
179
209
180
210
181
211
def hover_element_and_double_click (driver , element , click_selector ,
@@ -201,9 +231,10 @@ def hover_element_and_double_click(driver, element, click_selector,
201
231
plural = "s"
202
232
if timeout == 1 :
203
233
plural = ""
204
- raise NoSuchElementException (
205
- "Element {%s} was not present after %s second%s!" % (
206
- click_selector , timeout , plural ))
234
+ message = (
235
+ "Element {%s} was not present after %s second%s!"
236
+ "" % (click_selector , timeout , plural ))
237
+ timeout_exception (NoSuchElementException , message )
207
238
208
239
209
240
def wait_for_element_present (driver , selector , by = By .CSS_SELECTOR ,
@@ -238,9 +269,10 @@ def wait_for_element_present(driver, selector, by=By.CSS_SELECTOR,
238
269
if timeout == 1 :
239
270
plural = ""
240
271
if not element :
241
- raise NoSuchElementException (
242
- "Element {%s} was not present after %s second%s!" % (
243
- selector , timeout , plural ))
272
+ message = (
273
+ "Element {%s} was not present after %s second%s!"
274
+ "" % (selector , timeout , plural ))
275
+ timeout_exception (NoSuchElementException , message )
244
276
245
277
246
278
def wait_for_element_visible (driver , selector , by = By .CSS_SELECTOR ,
@@ -280,13 +312,15 @@ def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR,
280
312
if timeout == 1 :
281
313
plural = ""
282
314
if not element and by != By .LINK_TEXT :
283
- raise ElementNotVisibleException (
284
- "Element {%s} was not visible after %s second%s!" % (
285
- selector , timeout , plural ))
315
+ message = (
316
+ "Element {%s} was not visible after %s second%s!"
317
+ "" % (selector , timeout , plural ))
318
+ timeout_exception (ElementNotVisibleException , message )
286
319
if not element and by == By .LINK_TEXT :
287
- raise ElementNotVisibleException (
288
- "Link text {%s} was not visible after %s second%s!" % (
289
- selector , timeout , plural ))
320
+ message = (
321
+ "Link text {%s} was not visible after %s second%s!"
322
+ "" % (selector , timeout , plural ))
323
+ timeout_exception (ElementNotVisibleException , message )
290
324
291
325
292
326
def wait_for_text_visible (driver , text , selector , by = By .CSS_SELECTOR ,
@@ -326,9 +360,10 @@ def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
326
360
if timeout == 1 :
327
361
plural = ""
328
362
if not element :
329
- raise ElementNotVisibleException (
330
- "Expected text {%s} for {%s} was not visible after %s second%s!" %
331
- (text , selector , timeout , plural ))
363
+ message = (
364
+ "Expected text {%s} for {%s} was not visible after %s second%s!"
365
+ "" % (text , selector , timeout , plural ))
366
+ timeout_exception (ElementNotVisibleException , message )
332
367
333
368
334
369
def wait_for_exact_text_visible (driver , text , selector , by = By .CSS_SELECTOR ,
@@ -369,9 +404,10 @@ def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
369
404
if timeout == 1 :
370
405
plural = ""
371
406
if not element :
372
- raise ElementNotVisibleException (
407
+ message = (
373
408
"Expected exact text {%s} for {%s} was not visible "
374
409
"after %s second%s!" % (text , selector , timeout , plural ))
410
+ timeout_exception (ElementNotVisibleException , message )
375
411
376
412
377
413
def wait_for_element_absent (driver , selector , by = By .CSS_SELECTOR ,
@@ -401,8 +437,10 @@ def wait_for_element_absent(driver, selector, by=By.CSS_SELECTOR,
401
437
plural = "s"
402
438
if timeout == 1 :
403
439
plural = ""
404
- raise Exception ("Element {%s} was still present after %s second%s!" %
405
- (selector , timeout , plural ))
440
+ message = (
441
+ "Element {%s} was still present after %s second%s!"
442
+ "" % (selector , timeout , plural ))
443
+ timeout_exception (Exception , message )
406
444
407
445
408
446
def wait_for_element_not_visible (driver , selector , by = By .CSS_SELECTOR ,
@@ -435,9 +473,10 @@ def wait_for_element_not_visible(driver, selector, by=By.CSS_SELECTOR,
435
473
plural = "s"
436
474
if timeout == 1 :
437
475
plural = ""
438
- raise Exception (
439
- "Element {%s} was still visible after %s second%s!" % (
440
- selector , timeout , plural ))
476
+ message = (
477
+ "Element {%s} was still visible after %s second%s!"
478
+ "" % (selector , timeout , plural ))
479
+ timeout_exception (Exception , message )
441
480
442
481
443
482
def wait_for_text_not_visible (driver , text , selector , by = By .CSS_SELECTOR ,
@@ -468,8 +507,10 @@ def wait_for_text_not_visible(driver, text, selector, by=By.CSS_SELECTOR,
468
507
plural = "s"
469
508
if timeout == 1 :
470
509
plural = ""
471
- raise Exception ("Text {%s} in {%s} was still visible after %s "
472
- "second%s!" % (text , selector , timeout , plural ))
510
+ message = (
511
+ "Text {%s} in {%s} was still visible after %s "
512
+ "second%s!" % (text , selector , timeout , plural ))
513
+ timeout_exception (Exception , message )
473
514
474
515
475
516
def find_visible_elements (driver , selector , by = By .CSS_SELECTOR ):
@@ -634,7 +675,9 @@ def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT):
634
675
if now_ms >= stop_ms :
635
676
break
636
677
time .sleep (0.1 )
637
- raise Exception ("Alert was not present after %s seconds!" % timeout )
678
+ message = (
679
+ "Alert was not present after %s seconds!" % timeout )
680
+ timeout_exception (Exception , message )
638
681
639
682
640
683
def switch_to_frame (driver , frame , timeout = settings .SMALL_TIMEOUT ):
@@ -671,7 +714,13 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
671
714
if now_ms >= stop_ms :
672
715
break
673
716
time .sleep (0.1 )
674
- raise Exception ("Frame was not present after %s seconds!" % timeout )
717
+ plural = "s"
718
+ if timeout == 1 :
719
+ plural = ""
720
+ message = (
721
+ "Frame {%s} was not visible after %s second%s!"
722
+ "" % (frame , timeout , plural ))
723
+ timeout_exception (Exception , message )
675
724
676
725
677
726
def switch_to_window (driver , window , timeout = settings .SMALL_TIMEOUT ):
@@ -697,7 +746,13 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
697
746
if now_ms >= stop_ms :
698
747
break
699
748
time .sleep (0.1 )
700
- raise Exception ("Window was not present after %s seconds!" % timeout )
749
+ plural = "s"
750
+ if timeout == 1 :
751
+ plural = ""
752
+ message = (
753
+ "Window {%s} was not present after %s second%s!"
754
+ "" % (window , timeout , plural ))
755
+ timeout_exception (Exception , message )
701
756
else :
702
757
window_handle = window
703
758
for x in range (int (timeout * 10 )):
@@ -710,4 +765,10 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
710
765
if now_ms >= stop_ms :
711
766
break
712
767
time .sleep (0.1 )
713
- raise Exception ("Window was not present after %s seconds!" % timeout )
768
+ plural = "s"
769
+ if timeout == 1 :
770
+ plural = ""
771
+ message = (
772
+ "Window {%s} was not present after %s second%s!"
773
+ "" % (window , timeout , plural ))
774
+ timeout_exception (Exception , message )
0 commit comments