From bfa4944a58212acf6ab09b3b06fb0addc1f91c9c Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 2 Oct 2021 22:05:39 -0400 Subject: [PATCH] Expected condition for checking attribute value Adds expected condition function text_to_be_present_in_element_attribute for checking if an element's attribute contains certain text. Fixes #7628 --- .../webdriver/support/expected_conditions.py | 24 +++++++++++++++++-- .../webdriver/common/webdriverwait_tests.py | 9 +++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/py/selenium/webdriver/support/expected_conditions.py b/py/selenium/webdriver/support/expected_conditions.py index 8936aa355d0dd..05c157825f3ed 100644 --- a/py/selenium/webdriver/support/expected_conditions.py +++ b/py/selenium/webdriver/support/expected_conditions.py @@ -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 """ @@ -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 @@ -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 """ diff --git a/py/test/selenium/webdriver/common/webdriverwait_tests.py b/py/test/selenium/webdriver/common/webdriverwait_tests.py index 4bb5e91c1aa7b..b1e23bc3e8f7c 100644 --- a/py/test/selenium/webdriver/common/webdriverwait_tests.py +++ b/py/test/selenium/webdriver/common/webdriverwait_tests.py @@ -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):