Skip to content

Commit 0c7c7ad

Browse files
committed
Add methods for checkboxes and radio buttons
1 parent cf7d0d1 commit 0c7c7ad

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,59 @@ def click_if_visible(self, selector, by=By.CSS_SELECTOR):
10521052
if self.is_element_visible(selector, by=by):
10531053
self.click(selector, by=by)
10541054

1055+
def is_checked(self, selector, by=By.CSS_SELECTOR, timeout=None):
1056+
""" Determines if a checkbox or a radio button element is checked.
1057+
Returns True if the element is checked.
1058+
Returns False if the element is not checked.
1059+
If the element is not present on the page, raises an exception.
1060+
If the element is not a checkbox or radio, raises an exception. """
1061+
if not timeout:
1062+
timeout = settings.SMALL_TIMEOUT
1063+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
1064+
timeout = self.__get_new_timeout(timeout)
1065+
selector, by = self.__recalculate_selector(selector, by)
1066+
kind = self.get_attribute(selector, "type", by=by, timeout=timeout)
1067+
if kind != "checkbox" and kind != "radio":
1068+
raise Exception("Expecting a checkbox or a radio button element!")
1069+
is_checked = self.get_attribute(
1070+
selector, "checked", by=by, timeout=timeout, hard_fail=False)
1071+
if is_checked:
1072+
return True
1073+
else: # (NoneType)
1074+
return False
1075+
1076+
def is_selected(self, selector, by=By.CSS_SELECTOR, timeout=None):
1077+
""" Same as is_checked() """
1078+
return self.is_checked(selector, by=by, timeout=timeout)
1079+
1080+
def check_if_unchecked(self, selector, by=By.CSS_SELECTOR):
1081+
""" If a checkbox or radio button is not checked, will check it. """
1082+
selector, by = self.__recalculate_selector(selector, by)
1083+
if not self.is_checked(selector, by=by):
1084+
if self.is_element_visible(selector, by=by):
1085+
self.click(selector, by=by)
1086+
else:
1087+
selector = self.convert_to_css_selector(selector, by=by)
1088+
self.js_click(selector, by=By.CSS_SELECTOR)
1089+
1090+
def select_if_unselected(self, selector, by=By.CSS_SELECTOR):
1091+
""" Same as check_if_unchecked() """
1092+
self.check_if_unchecked(selector, by=by)
1093+
1094+
def uncheck_if_checked(self, selector, by=By.CSS_SELECTOR):
1095+
""" If a checkbox is checked, will uncheck it. """
1096+
selector, by = self.__recalculate_selector(selector, by)
1097+
if self.is_checked(selector, by=by):
1098+
if self.is_element_visible(selector, by=by):
1099+
self.click(selector, by=by)
1100+
else:
1101+
selector = self.convert_to_css_selector(selector, by=by)
1102+
self.js_click(selector, by=By.CSS_SELECTOR)
1103+
1104+
def unselect_if_selected(self, selector, by=By.CSS_SELECTOR):
1105+
""" Same as uncheck_if_checked() """
1106+
self.uncheck_if_checked(selector, by=by)
1107+
10551108
def is_element_in_an_iframe(self, selector, by=By.CSS_SELECTOR):
10561109
""" Returns True if the selector's element is located in an iframe.
10571110
Otherwise returns False. """

0 commit comments

Comments
 (0)