Skip to content

Commit ae326c6

Browse files
committed
Make improvements to assert_text() and assert_exact_text()
1 parent 1756deb commit ae326c6

File tree

1 file changed

+70
-17
lines changed

1 file changed

+70
-17
lines changed

seleniumbase/fixtures/page_actions.py

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@
2828
from selenium.common.exceptions import NoAlertPresentException
2929
from selenium.common.exceptions import NoSuchAttributeException
3030
from selenium.common.exceptions import NoSuchElementException
31-
from selenium.common.exceptions import NoSuchFrameException
3231
from selenium.common.exceptions import NoSuchWindowException
3332
from selenium.common.exceptions import StaleElementReferenceException
34-
from selenium.common.exceptions import TimeoutException
3533
from selenium.webdriver.common.by import By
3634
from selenium.webdriver.common.action_chains import ActionChains
35+
from seleniumbase.common.exceptions import TextNotVisibleException
3736
from seleniumbase.config import settings
38-
from seleniumbase.fixtures import shared_utils as s_utils
37+
from seleniumbase.fixtures import shared_utils
3938

4039

4140
def is_element_present(driver, selector, by=By.CSS_SELECTOR):
@@ -438,26 +437,44 @@ def wait_for_text_visible(
438437
"""
439438
element = None
440439
is_present = False
440+
full_text = None
441441
start_ms = time.time() * 1000.0
442442
stop_ms = start_ms + (timeout * 1000.0)
443443
for x in range(int(timeout * 10)):
444-
s_utils.check_if_time_limit_exceeded()
444+
shared_utils.check_if_time_limit_exceeded()
445+
full_text = None
445446
try:
446447
element = driver.find_element(by=by, value=selector)
447448
is_present = True
448-
if browser == "safari":
449+
if element.tag_name == "input":
450+
if (
451+
element.is_displayed()
452+
and text in element.get_property("value")
453+
):
454+
return element
455+
else:
456+
if element.is_displayed():
457+
full_text = element.get_property("value").strip()
458+
element = None
459+
raise Exception()
460+
elif browser == "safari":
449461
if (
450462
element.is_displayed()
451463
and text in element.get_attribute("innerText")
452464
):
453465
return element
454466
else:
467+
if element.is_displayed():
468+
full_text = element.get_attribute("innerText")
469+
full_text = full_text.strip()
455470
element = None
456471
raise Exception()
457472
else:
458473
if element.is_displayed() and text in element.text:
459474
return element
460475
else:
476+
if element.is_displayed():
477+
full_text = element.text.strip()
461478
element = None
462479
raise Exception()
463480
except Exception:
@@ -478,11 +495,20 @@ def wait_for_text_visible(
478495
)
479496
timeout_exception(NoSuchElementException, message)
480497
# The element exists in the HTML, but the text is not visible
481-
message = (
482-
"Expected text {%s} for {%s} was not visible after %s second%s!"
483-
% (text, selector, timeout, plural)
484-
)
485-
timeout_exception(ElementNotVisibleException, message)
498+
message = None
499+
if not full_text or len(str(full_text.replace("\n", ""))) > 320:
500+
message = (
501+
"Expected text substring {%s} for {%s} was not visible "
502+
"after %s second%s!" % (text, selector, timeout, plural)
503+
)
504+
else:
505+
full_text = full_text.replace("\n", "\\n ")
506+
message = (
507+
"Expected text substring {%s} for {%s} was not visible "
508+
"after %s second%s!\n (The string searched was {%s})"
509+
% (text, selector, timeout, plural, full_text)
510+
)
511+
timeout_exception(TextNotVisibleException, message)
486512
else:
487513
return element
488514

@@ -515,19 +541,35 @@ def wait_for_exact_text_visible(
515541
"""
516542
element = None
517543
is_present = False
544+
actual_text = None
518545
start_ms = time.time() * 1000.0
519546
stop_ms = start_ms + (timeout * 1000.0)
520547
for x in range(int(timeout * 10)):
521-
s_utils.check_if_time_limit_exceeded()
548+
shared_utils.check_if_time_limit_exceeded()
549+
actual_text = None
522550
try:
523551
element = driver.find_element(by=by, value=selector)
524552
is_present = True
525-
if browser == "safari":
553+
if element.tag_name == "input":
554+
if (
555+
element.is_displayed()
556+
and text.strip() == element.get_property("value").strip()
557+
):
558+
return element
559+
else:
560+
if element.is_displayed():
561+
actual_text = element.get_property("value").strip()
562+
element = None
563+
raise Exception()
564+
elif browser == "safari":
526565
if element.is_displayed() and (
527566
text.strip() == element.get_attribute("innerText").strip()
528567
):
529568
return element
530569
else:
570+
if element.is_displayed():
571+
actual_text = element.get_attribute("innerText")
572+
actual_text = actual_text.strip()
531573
element = None
532574
raise Exception()
533575
else:
@@ -537,6 +579,8 @@ def wait_for_exact_text_visible(
537579
):
538580
return element
539581
else:
582+
if element.is_displayed():
583+
actual_text = element.text.strip()
540584
element = None
541585
raise Exception()
542586
except Exception:
@@ -557,11 +601,20 @@ def wait_for_exact_text_visible(
557601
)
558602
timeout_exception(NoSuchElementException, message)
559603
# The element exists in the HTML, but the exact text is not visible
560-
message = (
561-
"Expected exact text {%s} for {%s} was not visible "
562-
"after %s second%s!" % (text, selector, timeout, plural)
563-
)
564-
timeout_exception(ElementNotVisibleException, message)
604+
message = None
605+
if not actual_text or len(str(actual_text)) > 120:
606+
message = (
607+
"Expected exact text {%s} for {%s} was not visible "
608+
"after %s second%s!" % (text, selector, timeout, plural)
609+
)
610+
else:
611+
actual_text = actual_text.replace("\n", "\\n")
612+
message = (
613+
"Expected exact text {%s} for {%s} was not visible "
614+
"after %s second%s!\n (Actual text was {%s})"
615+
% (text, selector, timeout, plural, actual_text)
616+
)
617+
timeout_exception(TextNotVisibleException, message)
565618
else:
566619
return element
567620

0 commit comments

Comments
 (0)