Skip to content

Commit

Permalink
Expected condition for checking attribute value (#9881)
Browse files Browse the repository at this point in the history
Adds expected condition function text_to_be_present_in_element_attribute
 for checking if an element's attribute contains certain text.

Fixes #7628

Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
  • Loading branch information
joesho112358 and AutomatedTester committed Oct 12, 2021
1 parent b8de36f commit c096326
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 22 additions & 2 deletions py/selenium/webdriver/support/expected_conditions.py
Expand Up @@ -218,7 +218,7 @@ def _predicate(driver):

def text_to_be_present_in_element_value(locator, text_):
"""
An expectation for checking if the given text is present in the element's
An expectation for checking if the given text is present in the element's value.
locator, text
"""

Expand All @@ -234,6 +234,26 @@ def _predicate(driver):
return _predicate


def text_to_be_present_in_element_attribute(locator, attribute_, text_):
"""
An expectation for checking if the given text is present in the element's attribute.
locator, attribute, text
"""

def _predicate(driver):
try:
if not element_attribute_to_include(locator, attribute_):
return False
element_text = driver.find_element(*locator).get_attribute(attribute_)
return text_ in element_text
except InvalidSelectorException as e:
raise e
except StaleElementReferenceException:
return False

return _predicate


def frame_to_be_available_and_switch_to_it(locator):
""" An expectation for checking whether the given frame is available to
switch to. If the frame is available it switches the given driver to the
Expand Down Expand Up @@ -414,7 +434,7 @@ def _predicate(driver):


def element_attribute_to_include(locator, attribute_):
""" An expectation for checking if the given attribute is include in the
""" An expectation for checking if the given attribute is included in the
specified element.
locator, attribute
"""
Expand Down
9 changes: 9 additions & 0 deletions py/test/selenium/webdriver/common/webdriverwait_tests.py
Expand Up @@ -212,6 +212,15 @@ def testExpectedConditionTextToBePresentInElementValue(driver, pages):
assert 'Example Expected text' == driver.find_element(By.ID, 'inputRequired').get_attribute('value')


def testExpectedConditionTextToBePresentInElementAttribute(driver, pages):
pages.load('booleanAttributes.html')
with pytest.raises(TimeoutException):
WebDriverWait(driver, 1).until(EC.text_to_be_present_in_element_attribute((By.ID, 'inputRequired'), 'value', 'Expected'))
driver.execute_script("setTimeout(function(){document.getElementById('inputRequired').value = 'Example Expected text'}, 200)")
WebDriverWait(driver, 1).until(EC.text_to_be_present_in_element_attribute((By.ID, 'inputRequired'), 'value', 'Expected'))
assert 'Example Expected text' == driver.find_element(By.ID, 'inputRequired').get_attribute('value')


# xfail can be removed after 23 March 2021
@pytest.mark.xfail_firefox(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1691348")
def testExpectedConditionFrameToBeAvailableAndSwitchToItByLocator(driver, pages):
Expand Down

0 comments on commit c096326

Please sign in to comment.