Skip to content

Commit 9a36a5f

Browse files
committed
Add more methods for text-based assertions
1 parent 63c830c commit 9a36a5f

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

Diff for: help_docs/method_summary.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,9 @@ self.find_text(text, selector="html", by="css selector", timeout=None)
713713
# Duplicates: self.wait_for_text(text, selector="html", by="css selector", timeout=None)
714714
# self.wait_for_text_visible(text, selector="html", by="css selector", timeout=None)
715715

716-
self.wait_for_exact_text_visible(
717-
text, selector="html", by="css selector", timeout=None)
716+
self.find_exact_text(text, selector="html", by="css selector", timeout=None)
717+
# Duplicates: self.wait_for_exact_text(text, selector="html", by="css selector", timeout=None)
718+
# self.wait_for_exact_text_visible(text, selector="html", by="css selector", timeout=None)
718719

719720
self.assert_text(text, selector="html", by="css selector", timeout=None)
720721
# Duplicates: self.assert_text_visible(text, selector="html", by="css selector", timeout=None)
@@ -762,8 +763,12 @@ self.assert_element_not_visible(selector, by="css selector", timeout=None)
762763

763764
self.wait_for_text_not_visible(text, selector="html", by="css selector", timeout=None)
764765

766+
self.wait_for_exact_text_not_visible(text, selector="html", by="css selector", timeout=None)
767+
765768
self.assert_text_not_visible(text, selector="html", by="css selector", timeout=None)
766769

770+
self.assert_exact_text_not_visible(text, selector="html", by="css selector", timeout=None)
771+
767772
############
768773

769774
self.wait_for_attribute_not_present(

Diff for: seleniumbase/fixtures/base_case.py

+69
Original file line numberDiff line numberDiff line change
@@ -4337,6 +4337,7 @@ def __process_recorded_actions(self):
43374337
ext_actions.append("as_at")
43384338
ext_actions.append("as_te")
43394339
ext_actions.append("astnv")
4340+
ext_actions.append("aetnv")
43404341
ext_actions.append("as_et")
43414342
ext_actions.append("wf_el")
43424343
ext_actions.append("sw_fr")
@@ -4740,6 +4741,7 @@ def __process_recorded_actions(self):
47404741
action[0] == "as_te"
47414742
or action[0] == "as_et"
47424743
or action[0] == "astnv"
4744+
or action[0] == "aetnv"
47434745
or action[0] == "da_te"
47444746
or action[0] == "da_et"
47454747
):
@@ -4752,6 +4754,8 @@ def __process_recorded_actions(self):
47524754
method = "assert_exact_text"
47534755
elif action[0] == "astnv":
47544756
method = "assert_text_not_visible"
4757+
elif action[0] == "aetnv":
4758+
method = "assert_exact_text_not_visible"
47554759
elif action[0] == "da_te":
47564760
method = "deferred_assert_text"
47574761
elif action[0] == "da_et":
@@ -8541,6 +8545,19 @@ def wait_for_text(
85418545
text, selector, by=by, timeout=timeout
85428546
)
85438547

8548+
def wait_for_exact_text(
8549+
self, text, selector="html", by="css selector", timeout=None
8550+
):
8551+
"""The shorter version of wait_for_exact_text_visible()"""
8552+
self.__check_scope()
8553+
if not timeout:
8554+
timeout = settings.LARGE_TIMEOUT
8555+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
8556+
timeout = self.__get_new_timeout(timeout)
8557+
return self.wait_for_exact_text_visible(
8558+
text, selector, by=by, timeout=timeout
8559+
)
8560+
85448561
def find_text(
85458562
self, text, selector="html", by="css selector", timeout=None
85468563
):
@@ -8554,6 +8571,19 @@ def find_text(
85548571
text, selector, by=by, timeout=timeout
85558572
)
85568573

8574+
def find_exact_text(
8575+
self, text, selector="html", by="css selector", timeout=None
8576+
):
8577+
"""Same as wait_for_exact_text_visible() - returns the element"""
8578+
self.__check_scope()
8579+
if not timeout:
8580+
timeout = settings.LARGE_TIMEOUT
8581+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
8582+
timeout = self.__get_new_timeout(timeout)
8583+
return self.wait_for_exact_text_visible(
8584+
text, selector, by=by, timeout=timeout
8585+
)
8586+
85578587
def assert_text_visible(
85588588
self, text, selector="html", by="css selector", timeout=None
85598589
):
@@ -8920,6 +8950,19 @@ def wait_for_text_not_visible(
89208950
self.driver, text, selector, by, timeout, self.browser
89218951
)
89228952

8953+
def wait_for_exact_text_not_visible(
8954+
self, text, selector="html", by="css selector", timeout=None
8955+
):
8956+
self.__check_scope()
8957+
if not timeout:
8958+
timeout = settings.LARGE_TIMEOUT
8959+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
8960+
timeout = self.__get_new_timeout(timeout)
8961+
selector, by = self.__recalculate_selector(selector, by)
8962+
return page_actions.wait_for_exact_text_not_visible(
8963+
self.driver, text, selector, by, timeout, self.browser
8964+
)
8965+
89238966
def assert_text_not_visible(
89248967
self, text, selector="html", by="css selector", timeout=None
89258968
):
@@ -8944,6 +8987,32 @@ def assert_text_not_visible(
89448987
self.__extra_actions.append(action)
89458988
return True
89468989

8990+
def assert_exact_text_not_visible(
8991+
self, text, selector="html", by="css selector", timeout=None
8992+
):
8993+
"""Similar to wait_for_exact_text_not_visible()
8994+
Raises an exception if the exact text is still visible after timeout.
8995+
Returns True if successful. Default timeout = SMALL_TIMEOUT."""
8996+
self.__check_scope()
8997+
if not timeout:
8998+
timeout = settings.SMALL_TIMEOUT
8999+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
9000+
timeout = self.__get_new_timeout(timeout)
9001+
self.wait_for_exact_text_not_visible(
9002+
text, selector, by=by, timeout=timeout
9003+
)
9004+
if self.recorder_mode:
9005+
url = self.get_current_url()
9006+
if url and len(url) > 0:
9007+
if ("http:") in url or ("https:") in url or ("file:") in url:
9008+
if self.get_session_storage_item("pause_recorder") == "no":
9009+
time_stamp = self.execute_script("return Date.now();")
9010+
origin = self.get_origin()
9011+
text_selector = [text, selector]
9012+
action = ["aetnv", text_selector, origin, time_stamp]
9013+
self.__extra_actions.append(action)
9014+
return True
9015+
89479016
############
89489017

89499018
def wait_for_attribute_not_present(

Diff for: seleniumbase/fixtures/page_actions.py

+48
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,54 @@ def wait_for_text_not_visible(
10311031
timeout_exception(Exception, message)
10321032

10331033

1034+
def wait_for_exact_text_not_visible(
1035+
driver,
1036+
text,
1037+
selector,
1038+
by="css selector",
1039+
timeout=settings.LARGE_TIMEOUT,
1040+
browser=None,
1041+
):
1042+
"""
1043+
Searches for the text in the element of the given selector on the page.
1044+
Returns True if the element is missing the exact text within the timeout.
1045+
Raises an exception if the exact text is still present after the timeout.
1046+
@Params
1047+
driver - the webdriver object (required)
1048+
text - the text that is being searched for in the element (required)
1049+
selector - the locator for identifying the page element (required)
1050+
by - the type of selector being used (Default: "css selector")
1051+
timeout - the time to wait for elements in seconds
1052+
@Returns
1053+
A web element object that contains the text searched for
1054+
"""
1055+
text = str(text)
1056+
start_ms = time.time() * 1000.0
1057+
stop_ms = start_ms + (timeout * 1000.0)
1058+
for x in range(int(timeout * 10)):
1059+
shared_utils.check_if_time_limit_exceeded()
1060+
if not is_exact_text_visible(
1061+
driver, text, selector, by=by, browser=browser
1062+
):
1063+
return True
1064+
now_ms = time.time() * 1000.0
1065+
if now_ms >= stop_ms:
1066+
break
1067+
time.sleep(0.1)
1068+
plural = "s"
1069+
if timeout == 1:
1070+
plural = ""
1071+
message = (
1072+
"Exact text {%s} for {%s} was still visible after %s second%s!" % (
1073+
text,
1074+
selector,
1075+
timeout,
1076+
plural,
1077+
)
1078+
)
1079+
timeout_exception(Exception, message)
1080+
1081+
10341082
def wait_for_attribute_not_present(
10351083
driver,
10361084
selector,

0 commit comments

Comments
 (0)