Skip to content

Commit 75a1caf

Browse files
committed
Add type-checking for methods that have "text" args
1 parent d95073c commit 75a1caf

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,7 @@ def update_text(
530530
pass # Clearing the text field first might not be necessary
531531
self.__demo_mode_pause_if_active(tiny=True)
532532
pre_action_url = self.driver.current_url
533-
if type(text) is int or type(text) is float:
534-
text = str(text)
533+
text = self.__get_type_checked_text(text)
535534
try:
536535
if not text.endswith("\n"):
537536
element.send_keys(text)
@@ -590,8 +589,7 @@ def add_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
590589
if not self.demo_mode and not self.slow_mode:
591590
self.__scroll_to_element(element, selector, by)
592591
pre_action_url = self.driver.current_url
593-
if type(text) is int or type(text) is float:
594-
text = str(text)
592+
text = self.__get_type_checked_text(text)
595593
try:
596594
if not text.endswith("\n"):
597595
element.send_keys(text)
@@ -988,6 +986,7 @@ def click_link_text(self, link_text, timeout=None):
988986
timeout = self.__get_new_timeout(timeout)
989987
pre_action_url = self.driver.current_url
990988
pre_window_count = len(self.driver.window_handles)
989+
link_text = self.__get_type_checked_text(link_text)
991990
if self.browser == "phantomjs":
992991
if self.is_link_text_visible(link_text):
993992
element = self.wait_for_link_text_visible(
@@ -1109,6 +1108,7 @@ def click_partial_link_text(self, partial_link_text, timeout=None):
11091108
timeout = settings.SMALL_TIMEOUT
11101109
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
11111110
timeout = self.__get_new_timeout(timeout)
1111+
partial_link_text = self.__get_type_checked_text(partial_link_text)
11121112
if self.browser == "phantomjs":
11131113
if self.is_partial_link_text_visible(partial_link_text):
11141114
element = self.wait_for_partial_link_text(partial_link_text)
@@ -4765,6 +4765,32 @@ def __fix_unicode_conversion(self, text):
47654765
text = text.replace("\xe2\xbd\x85", "\xe6\x96\xb9")
47664766
return text
47674767

4768+
def __get_type_checked_text(self, text):
4769+
""" Do type-checking on text. Then return it when valid.
4770+
If the text is acceptable, return the text or str(text).
4771+
If the text is not acceptable, raise a Python Exception.
4772+
"""
4773+
if type(text) is str:
4774+
return text
4775+
elif type(text) is int or type(text) is float:
4776+
return str(text) # Convert num to string
4777+
elif type(text) is bool:
4778+
raise Exception("text must be a string! Boolean found!")
4779+
elif type(text).__name__ == "NoneType":
4780+
raise Exception("text must be a string! NoneType found!")
4781+
elif type(text) is list:
4782+
raise Exception("text must be a string! List found!")
4783+
elif type(text) is tuple:
4784+
raise Exception("text must be a string! Tuple found!")
4785+
elif type(text) is set:
4786+
raise Exception("text must be a string! Set found!")
4787+
elif type(text) is dict:
4788+
raise Exception("text must be a string! Dict found!")
4789+
elif not python3 and type(text) is unicode: # noqa: F821
4790+
return text # (For old Python versions with unicode)
4791+
else:
4792+
return str(text)
4793+
47684794
def get_pdf_text(
47694795
self,
47704796
pdf,
@@ -5703,8 +5729,7 @@ def set_value(
57035729
self.__demo_mode_highlight_if_active(orginal_selector, by)
57045730
if scroll and not self.demo_mode and not self.slow_mode:
57055731
self.scroll_to(orginal_selector, by=by, timeout=timeout)
5706-
if type(text) is int or type(text) is float:
5707-
text = str(text)
5732+
text = self.__get_type_checked_text(text)
57085733
value = re.escape(text)
57095734
value = self.__escape_quotes_if_needed(value)
57105735
pre_escape_css_selector = css_selector
@@ -5765,8 +5790,7 @@ def js_update_text(self, selector, text, by=By.CSS_SELECTOR, timeout=None):
57655790
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
57665791
timeout = self.__get_new_timeout(timeout)
57675792
selector, by = self.__recalculate_selector(selector, by)
5768-
if type(text) is int or type(text) is float:
5769-
text = str(text)
5793+
text = self.__get_type_checked_text(text)
57705794
self.set_value(selector, text, by=by, timeout=timeout)
57715795
if not text.endswith("\n"):
57725796
try:
@@ -5838,8 +5862,7 @@ def set_text_content(
58385862
self.__demo_mode_highlight_if_active(orginal_selector, by)
58395863
if not self.demo_mode and not self.slow_mode:
58405864
self.scroll_to(orginal_selector, by=by, timeout=timeout)
5841-
if type(text) is int or type(text) is float:
5842-
text = str(text)
5865+
text = self.__get_type_checked_text(text)
58435866
value = re.escape(text)
58445867
value = self.__escape_quotes_if_needed(value)
58455868
css_selector = re.escape(css_selector) # Add "\\" to special chars
@@ -6185,8 +6208,7 @@ def __shadow_type(self, selector, text, timeout, clear_first=True):
61856208
element.send_keys(backspaces)
61866209
except Exception:
61876210
pass
6188-
if type(text) is int or type(text) is float:
6189-
text = str(text)
6211+
text = self.__get_type_checked_text(text)
61906212
if not text.endswith("\n"):
61916213
element.send_keys(text)
61926214
if settings.WAIT_FOR_RSC_ON_PAGE_LOADS:
@@ -9390,6 +9412,7 @@ def wait_for_text_visible(
93909412
timeout = settings.LARGE_TIMEOUT
93919413
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
93929414
timeout = self.__get_new_timeout(timeout)
9415+
text = self.__get_type_checked_text(text)
93939416
selector, by = self.__recalculate_selector(selector, by)
93949417
if self.__is_shadow_selector(selector):
93959418
return self.__wait_for_shadow_text_visible(

0 commit comments

Comments
 (0)