28
28
from selenium .common .exceptions import NoAlertPresentException
29
29
from selenium .common .exceptions import NoSuchAttributeException
30
30
from selenium .common .exceptions import NoSuchElementException
31
- from selenium .common .exceptions import NoSuchFrameException
32
31
from selenium .common .exceptions import NoSuchWindowException
33
32
from selenium .common .exceptions import StaleElementReferenceException
34
- from selenium .common .exceptions import TimeoutException
35
33
from selenium .webdriver .common .by import By
36
34
from selenium .webdriver .common .action_chains import ActionChains
35
+ from seleniumbase .common .exceptions import TextNotVisibleException
37
36
from seleniumbase .config import settings
38
- from seleniumbase .fixtures import shared_utils as s_utils
37
+ from seleniumbase .fixtures import shared_utils
39
38
40
39
41
40
def is_element_present (driver , selector , by = By .CSS_SELECTOR ):
@@ -438,26 +437,44 @@ def wait_for_text_visible(
438
437
"""
439
438
element = None
440
439
is_present = False
440
+ full_text = None
441
441
start_ms = time .time () * 1000.0
442
442
stop_ms = start_ms + (timeout * 1000.0 )
443
443
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
445
446
try :
446
447
element = driver .find_element (by = by , value = selector )
447
448
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" :
449
461
if (
450
462
element .is_displayed ()
451
463
and text in element .get_attribute ("innerText" )
452
464
):
453
465
return element
454
466
else :
467
+ if element .is_displayed ():
468
+ full_text = element .get_attribute ("innerText" )
469
+ full_text = full_text .strip ()
455
470
element = None
456
471
raise Exception ()
457
472
else :
458
473
if element .is_displayed () and text in element .text :
459
474
return element
460
475
else :
476
+ if element .is_displayed ():
477
+ full_text = element .text .strip ()
461
478
element = None
462
479
raise Exception ()
463
480
except Exception :
@@ -478,11 +495,20 @@ def wait_for_text_visible(
478
495
)
479
496
timeout_exception (NoSuchElementException , message )
480
497
# 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 )
486
512
else :
487
513
return element
488
514
@@ -515,19 +541,35 @@ def wait_for_exact_text_visible(
515
541
"""
516
542
element = None
517
543
is_present = False
544
+ actual_text = None
518
545
start_ms = time .time () * 1000.0
519
546
stop_ms = start_ms + (timeout * 1000.0 )
520
547
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
522
550
try :
523
551
element = driver .find_element (by = by , value = selector )
524
552
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" :
526
565
if element .is_displayed () and (
527
566
text .strip () == element .get_attribute ("innerText" ).strip ()
528
567
):
529
568
return element
530
569
else :
570
+ if element .is_displayed ():
571
+ actual_text = element .get_attribute ("innerText" )
572
+ actual_text = actual_text .strip ()
531
573
element = None
532
574
raise Exception ()
533
575
else :
@@ -537,6 +579,8 @@ def wait_for_exact_text_visible(
537
579
):
538
580
return element
539
581
else :
582
+ if element .is_displayed ():
583
+ actual_text = element .text .strip ()
540
584
element = None
541
585
raise Exception ()
542
586
except Exception :
@@ -557,11 +601,20 @@ def wait_for_exact_text_visible(
557
601
)
558
602
timeout_exception (NoSuchElementException , message )
559
603
# 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 )
565
618
else :
566
619
return element
567
620
0 commit comments