Skip to content

Commit 023182c

Browse files
committed
Simplify error output
1 parent ed7e54f commit 023182c

File tree

1 file changed

+94
-33
lines changed

1 file changed

+94
-33
lines changed

seleniumbase/fixtures/page_actions.py

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,34 @@ def hover_element(driver, element):
113113
hover.perform()
114114

115115

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+
116144
def hover_and_click(driver, hover_selector, click_selector,
117145
hover_by=By.CSS_SELECTOR, click_by=By.CSS_SELECTOR,
118146
timeout=settings.SMALL_TIMEOUT):
@@ -145,9 +173,10 @@ def hover_and_click(driver, hover_selector, click_selector,
145173
plural = "s"
146174
if timeout == 1:
147175
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)
151180

152181

153182
def hover_element_and_click(driver, element, click_selector,
@@ -173,9 +202,10 @@ def hover_element_and_click(driver, element, click_selector,
173202
plural = "s"
174203
if timeout == 1:
175204
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)
179209

180210

181211
def hover_element_and_double_click(driver, element, click_selector,
@@ -201,9 +231,10 @@ def hover_element_and_double_click(driver, element, click_selector,
201231
plural = "s"
202232
if timeout == 1:
203233
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)
207238

208239

209240
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,
238269
if timeout == 1:
239270
plural = ""
240271
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)
244276

245277

246278
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,
280312
if timeout == 1:
281313
plural = ""
282314
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)
286319
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)
290324

291325

292326
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,
326360
if timeout == 1:
327361
plural = ""
328362
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)
332367

333368

334369
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,
369404
if timeout == 1:
370405
plural = ""
371406
if not element:
372-
raise ElementNotVisibleException(
407+
message = (
373408
"Expected exact text {%s} for {%s} was not visible "
374409
"after %s second%s!" % (text, selector, timeout, plural))
410+
timeout_exception(ElementNotVisibleException, message)
375411

376412

377413
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,
401437
plural = "s"
402438
if timeout == 1:
403439
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)
406444

407445

408446
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,
435473
plural = "s"
436474
if timeout == 1:
437475
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)
441480

442481

443482
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,
468507
plural = "s"
469508
if timeout == 1:
470509
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)
473514

474515

475516
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):
634675
if now_ms >= stop_ms:
635676
break
636677
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)
638681

639682

640683
def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
@@ -671,7 +714,13 @@ def switch_to_frame(driver, frame, timeout=settings.SMALL_TIMEOUT):
671714
if now_ms >= stop_ms:
672715
break
673716
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)
675724

676725

677726
def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
@@ -697,7 +746,13 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
697746
if now_ms >= stop_ms:
698747
break
699748
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)
701756
else:
702757
window_handle = window
703758
for x in range(int(timeout * 10)):
@@ -710,4 +765,10 @@ def switch_to_window(driver, window, timeout=settings.SMALL_TIMEOUT):
710765
if now_ms >= stop_ms:
711766
break
712767
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

Comments
 (0)