Skip to content

Commit cc7332f

Browse files
committed
Improve error messages
1 parent 74f9a65 commit cc7332f

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

seleniumbase/fixtures/page_actions.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ def wait_for_element_present(driver, selector, by=By.CSS_SELECTOR,
217217
timeout=settings.LARGE_TIMEOUT):
218218
"""
219219
Searches for the specified element by the given selector. Returns the
220-
element object if the element is present on the page. The element can be
221-
invisible. Raises an exception if the element does not appear in the
222-
specified timeout.
220+
element object if it exists in the HTML. (The element can be invisible.)
221+
Raises NoSuchElementException if the element does not exist in the HTML
222+
within the specified timeout.
223223
@Params
224224
driver - the webdriver object
225225
selector - the locator for identifying the page element (required)
@@ -256,24 +256,27 @@ def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR,
256256
"""
257257
Searches for the specified element by the given selector. Returns the
258258
element object if the element is present and visible on the page.
259-
Raises an exception if the element does not appear in the
260-
specified timeout.
259+
Raises NoSuchElementException if the element does not exist in the HTML
260+
within the specified timeout.
261+
Raises ElementNotVisibleException if the element exists in the HTML,
262+
but is not visible (eg. opacity is "0") within the specified timeout.
261263
@Params
262264
driver - the webdriver object (required)
263265
selector - the locator for identifying the page element (required)
264266
by - the type of selector being used (Default: By.CSS_SELECTOR)
265267
timeout - the time to wait for elements in seconds
266-
267268
@Returns
268269
A web element object
269270
"""
270271
element = None
272+
is_present = False
271273
start_ms = time.time() * 1000.0
272274
stop_ms = start_ms + (timeout * 1000.0)
273275
for x in range(int(timeout * 10)):
274276
s_utils.check_if_time_limit_exceeded()
275277
try:
276278
element = driver.find_element(by=by, value=selector)
279+
is_present = True
277280
if element.is_displayed():
278281
return element
279282
else:
@@ -288,6 +291,13 @@ def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR,
288291
if timeout == 1:
289292
plural = ""
290293
if not element and by != By.LINK_TEXT:
294+
if not is_present:
295+
# The element does not exist in the HTML
296+
message = (
297+
"Element {%s} was not present after %s second%s!"
298+
"" % (selector, timeout, plural))
299+
timeout_exception(NoSuchElementException, message)
300+
# The element exists in the HTML, but is not visible
291301
message = (
292302
"Element {%s} was not visible after %s second%s!"
293303
"" % (selector, timeout, plural))
@@ -304,8 +314,11 @@ def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
304314
"""
305315
Searches for the specified element by the given selector. Returns the
306316
element object if the text is present in the element and visible
307-
on the page. Raises an exception if the text or element do not appear
308-
in the specified timeout.
317+
on the page.
318+
Raises NoSuchElementException if the element does not exist in the HTML
319+
within the specified timeout.
320+
Raises ElementNotVisibleException if the element exists in the HTML,
321+
but the text is not visible within the specified timeout.
309322
@Params
310323
driver - the webdriver object (required)
311324
text - the text that is being searched for in the element (required)
@@ -316,12 +329,14 @@ def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
316329
A web element object that contains the text searched for
317330
"""
318331
element = None
332+
is_present = False
319333
start_ms = time.time() * 1000.0
320334
stop_ms = start_ms + (timeout * 1000.0)
321335
for x in range(int(timeout * 10)):
322336
s_utils.check_if_time_limit_exceeded()
323337
try:
324338
element = driver.find_element(by=by, value=selector)
339+
is_present = True
325340
if element.is_displayed() and text in element.text:
326341
return element
327342
else:
@@ -336,6 +351,13 @@ def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
336351
if timeout == 1:
337352
plural = ""
338353
if not element:
354+
if not is_present:
355+
# The element does not exist in the HTML
356+
message = (
357+
"Element {%s} was not present after %s second%s!"
358+
"" % (selector, timeout, plural))
359+
timeout_exception(NoSuchElementException, message)
360+
# The element exists in the HTML, but the text is not visible
339361
message = (
340362
"Expected text {%s} for {%s} was not visible after %s second%s!"
341363
"" % (text, selector, timeout, plural))
@@ -348,8 +370,10 @@ def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
348370
Searches for the specified element by the given selector. Returns the
349371
element object if the text matches exactly with the text in the element,
350372
and the text is visible.
351-
Raises an exception if the text or element do not appear
352-
in the specified timeout.
373+
Raises NoSuchElementException if the element does not exist in the HTML
374+
within the specified timeout.
375+
Raises ElementNotVisibleException if the element exists in the HTML,
376+
but the exact text is not visible within the specified timeout.
353377
@Params
354378
driver - the webdriver object (required)
355379
text - the exact text that is expected for the element (required)
@@ -360,12 +384,14 @@ def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
360384
A web element object that contains the text searched for
361385
"""
362386
element = None
387+
is_present = False
363388
start_ms = time.time() * 1000.0
364389
stop_ms = start_ms + (timeout * 1000.0)
365390
for x in range(int(timeout * 10)):
366391
s_utils.check_if_time_limit_exceeded()
367392
try:
368393
element = driver.find_element(by=by, value=selector)
394+
is_present = True
369395
if element.is_displayed() and text.strip() == element.text.strip():
370396
return element
371397
else:
@@ -380,6 +406,13 @@ def wait_for_exact_text_visible(driver, text, selector, by=By.CSS_SELECTOR,
380406
if timeout == 1:
381407
plural = ""
382408
if not element:
409+
if not is_present:
410+
# The element does not exist in the HTML
411+
message = (
412+
"Element {%s} was not present after %s second%s!"
413+
"" % (selector, timeout, plural))
414+
timeout_exception(NoSuchElementException, message)
415+
# The element exists in the HTML, but the exact text is not visible
383416
message = (
384417
"Expected exact text {%s} for {%s} was not visible "
385418
"after %s second%s!" % (text, selector, timeout, plural))

0 commit comments

Comments
 (0)