Skip to content

Commit 8b3d697

Browse files
committed
Add methods for asserting multiple elements from a list
1 parent f4ca116 commit 8b3d697

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5766,12 +5766,63 @@ def assert_element_present(self, selector, by=By.CSS_SELECTOR,
57665766
timeout = settings.SMALL_TIMEOUT
57675767
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
57685768
timeout = self.__get_new_timeout(timeout)
5769+
if type(selector) is list:
5770+
self.assert_elements_present(selector, by=by, timeout=timeout)
5771+
return True
57695772
if self.__is_shadow_selector(selector):
57705773
self.__assert_shadow_element_present(selector)
57715774
return True
57725775
self.wait_for_element_present(selector, by=by, timeout=timeout)
57735776
return True
57745777

5778+
def assert_elements_present(self, *args, **kwargs):
5779+
""" Similar to self.assert_element_present(),
5780+
but can assert that multiple elements are present in the HTML.
5781+
The input is a list of elements.
5782+
Optional kwargs include "by" and "timeout" (used by all selectors).
5783+
Raises an exception if any of the elements are not visible.
5784+
Examples:
5785+
self.assert_elements_present("head", "style", "script", "body")
5786+
OR
5787+
self.assert_elements_present(["head", "body", "h1", "h2"]) """
5788+
self.__check_scope()
5789+
selectors = []
5790+
timeout = None
5791+
by = By.CSS_SELECTOR
5792+
for kwarg in kwargs:
5793+
if kwarg == "timeout":
5794+
timeout = kwargs["timeout"]
5795+
elif kwarg == "by":
5796+
by = kwargs["by"]
5797+
elif kwarg == "selector":
5798+
selector = kwargs["selector"]
5799+
if type(selector) is str:
5800+
selectors.append(selector)
5801+
elif type(selector) is list:
5802+
for a_selector in selector:
5803+
if type(a_selector) is str:
5804+
selectors.append(a_selector)
5805+
else:
5806+
raise Exception('Unknown kwarg: "%s"!' % kwarg)
5807+
if not timeout:
5808+
timeout = settings.SMALL_TIMEOUT
5809+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
5810+
timeout = self.__get_new_timeout(timeout)
5811+
for arg in args:
5812+
if type(arg) is list:
5813+
for selector in arg:
5814+
if type(selector) is str:
5815+
selectors.append(selector)
5816+
elif type(arg) is str:
5817+
selectors.append(arg)
5818+
for selector in selectors:
5819+
if self.__is_shadow_selector(selector):
5820+
self.__assert_shadow_element_visible(selector)
5821+
continue
5822+
self.wait_for_element_present(selector, by=by, timeout=timeout)
5823+
continue
5824+
return True
5825+
57755826
def find_element(self, selector, by=By.CSS_SELECTOR, timeout=None):
57765827
""" Same as wait_for_element_visible() - returns the element """
57775828
self.__check_scope()
@@ -5790,6 +5841,9 @@ def assert_element(self, selector, by=By.CSS_SELECTOR, timeout=None):
57905841
timeout = settings.SMALL_TIMEOUT
57915842
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
57925843
timeout = self.__get_new_timeout(timeout)
5844+
if type(selector) is list:
5845+
self.assert_elements(selector, by=by, timeout=timeout)
5846+
return True
57935847
if self.__is_shadow_selector(selector):
57945848
self.__assert_shadow_element_visible(selector)
57955849
return True
@@ -5816,6 +5870,67 @@ def assert_element_visible(self, selector, by=By.CSS_SELECTOR,
58165870
self.assert_element(selector, by=by, timeout=timeout)
58175871
return True
58185872

5873+
def assert_elements(self, *args, **kwargs):
5874+
""" Similar to self.assert_element(), but can assert multiple elements.
5875+
The input is a list of elements.
5876+
Optional kwargs include "by" and "timeout" (used by all selectors).
5877+
Raises an exception if any of the elements are not visible.
5878+
Examples:
5879+
self.assert_elements("h1", "h2", "h3")
5880+
OR
5881+
self.assert_elements(["h1", "h2", "h3"]) """
5882+
self.__check_scope()
5883+
selectors = []
5884+
timeout = None
5885+
by = By.CSS_SELECTOR
5886+
for kwarg in kwargs:
5887+
if kwarg == "timeout":
5888+
timeout = kwargs["timeout"]
5889+
elif kwarg == "by":
5890+
by = kwargs["by"]
5891+
elif kwarg == "selector":
5892+
selector = kwargs["selector"]
5893+
if type(selector) is str:
5894+
selectors.append(selector)
5895+
elif type(selector) is list:
5896+
for a_selector in selector:
5897+
if type(a_selector) is str:
5898+
selectors.append(a_selector)
5899+
else:
5900+
raise Exception('Unknown kwarg: "%s"!' % kwarg)
5901+
if not timeout:
5902+
timeout = settings.SMALL_TIMEOUT
5903+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
5904+
timeout = self.__get_new_timeout(timeout)
5905+
for arg in args:
5906+
if type(arg) is list:
5907+
for selector in arg:
5908+
if type(selector) is str:
5909+
selectors.append(selector)
5910+
elif type(arg) is str:
5911+
selectors.append(arg)
5912+
for selector in selectors:
5913+
if self.__is_shadow_selector(selector):
5914+
self.__assert_shadow_element_visible(selector)
5915+
continue
5916+
self.wait_for_element_visible(selector, by=by, timeout=timeout)
5917+
if self.demo_mode:
5918+
selector, by = self.__recalculate_selector(selector, by)
5919+
a_t = "ASSERT"
5920+
if self._language != "English":
5921+
from seleniumbase.fixtures.words import SD
5922+
a_t = SD.translate_assert(self._language)
5923+
messenger_post = "%s %s: %s" % (a_t, by.upper(), selector)
5924+
self.__highlight_with_assert_success(
5925+
messenger_post, selector, by)
5926+
continue
5927+
return True
5928+
5929+
def assert_elements_visible(self, *args, **kwargs):
5930+
""" Same as self.assert_elements()
5931+
Raises an exception if any element cannot be found. """
5932+
return self.assert_elements(*args, **kwargs)
5933+
58195934
############
58205935

58215936
def wait_for_text_visible(self, text, selector="html", by=By.CSS_SELECTOR,

0 commit comments

Comments
 (0)