Skip to content

Commit 7d402a3

Browse files
committed
Add self.wait_for_attribute_not_present() with assert
1 parent d9fc9eb commit 7d402a3

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7499,6 +7499,36 @@ def assert_text_not_visible(
74997499

75007500
############
75017501

7502+
def wait_for_attribute_not_present(
7503+
self, selector, attribute, value=None, by=By.CSS_SELECTOR, timeout=None
7504+
):
7505+
self.__check_scope()
7506+
if not timeout:
7507+
timeout = settings.LARGE_TIMEOUT
7508+
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
7509+
timeout = self.__get_new_timeout(timeout)
7510+
selector, by = self.__recalculate_selector(selector, by)
7511+
return page_actions.wait_for_attribute_not_present(
7512+
self.driver, selector, attribute, value, by, timeout
7513+
)
7514+
7515+
def assert_attribute_not_present(
7516+
self, selector, attribute, value=None, by=By.CSS_SELECTOR, timeout=None
7517+
):
7518+
"""Similar to wait_for_attribute_not_present()
7519+
Raises an exception if the attribute is still present after timeout.
7520+
Returns True if successful. Default timeout = SMALL_TIMEOUT."""
7521+
self.__check_scope()
7522+
if not timeout:
7523+
timeout = settings.SMALL_TIMEOUT
7524+
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
7525+
timeout = self.__get_new_timeout(timeout)
7526+
return self.wait_for_attribute_not_present(
7527+
selector, attribute, value=value, by=by, timeout=timeout
7528+
)
7529+
7530+
############
7531+
75027532
def wait_for_and_accept_alert(self, timeout=None):
75037533
self.__check_scope()
75047534
if not timeout:

seleniumbase/fixtures/page_actions.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def wait_for_attribute(
524524
attribute - the attribute that is expected for the element (required)
525525
value - the attribute value that is expected (Default: None)
526526
by - the type of selector being used (Default: By.CSS_SELECTOR)
527-
timeout - the time to wait for elements in seconds
527+
timeout - the time to wait for the element attribute in seconds
528528
@Returns
529529
A web element object that contains the expected attribute/value
530530
"""
@@ -701,6 +701,55 @@ def wait_for_text_not_visible(
701701
timeout_exception(Exception, message)
702702

703703

704+
def wait_for_attribute_not_present(
705+
driver,
706+
selector,
707+
attribute,
708+
value=None,
709+
by=By.CSS_SELECTOR,
710+
timeout=settings.LARGE_TIMEOUT
711+
):
712+
"""
713+
Searches for the specified element attribute by the given selector.
714+
Returns True if the attribute isn't present on the page within the timeout.
715+
Also returns True if the element is not present within the timeout.
716+
Raises an exception if the attribute is still present after the timeout.
717+
@Params
718+
driver - the webdriver object (required)
719+
selector - the locator for identifying the page element (required)
720+
attribute - the element attribute (required)
721+
value - the attribute value (Default: None)
722+
by - the type of selector being used (Default: By.CSS_SELECTOR)
723+
timeout - the time to wait for the element attribute in seconds
724+
"""
725+
start_ms = time.time() * 1000.0
726+
stop_ms = start_ms + (timeout * 1000.0)
727+
for x in range(int(timeout * 10)):
728+
s_utils.check_if_time_limit_exceeded()
729+
if not is_attribute_present(
730+
driver, selector, attribute, value=value, by=by
731+
):
732+
return True
733+
now_ms = time.time() * 1000.0
734+
if now_ms >= stop_ms:
735+
break
736+
time.sleep(0.1)
737+
plural = "s"
738+
if timeout == 1:
739+
plural = ""
740+
message = (
741+
"Attribute {%s} of element {%s} was still present after %s second%s!"
742+
"" % (attribute, selector, timeout, plural)
743+
)
744+
if value:
745+
message = (
746+
"Value {%s} for attribute {%s} of element {%s} was still present "
747+
"after %s second%s!"
748+
"" % (value, attribute, selector, timeout, plural)
749+
)
750+
timeout_exception(Exception, message)
751+
752+
704753
def find_visible_elements(driver, selector, by=By.CSS_SELECTOR):
705754
"""
706755
Finds all WebElements that match a selector and are visible.

0 commit comments

Comments
 (0)