Skip to content

Commit c096326

Browse files
Expected condition for checking attribute value (#9881)
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 <[email protected]>
1 parent b8de36f commit c096326

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

py/selenium/webdriver/support/expected_conditions.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _predicate(driver):
218218

219219
def text_to_be_present_in_element_value(locator, text_):
220220
"""
221-
An expectation for checking if the given text is present in the element's
221+
An expectation for checking if the given text is present in the element's value.
222222
locator, text
223223
"""
224224

@@ -234,6 +234,26 @@ def _predicate(driver):
234234
return _predicate
235235

236236

237+
def text_to_be_present_in_element_attribute(locator, attribute_, text_):
238+
"""
239+
An expectation for checking if the given text is present in the element's attribute.
240+
locator, attribute, text
241+
"""
242+
243+
def _predicate(driver):
244+
try:
245+
if not element_attribute_to_include(locator, attribute_):
246+
return False
247+
element_text = driver.find_element(*locator).get_attribute(attribute_)
248+
return text_ in element_text
249+
except InvalidSelectorException as e:
250+
raise e
251+
except StaleElementReferenceException:
252+
return False
253+
254+
return _predicate
255+
256+
237257
def frame_to_be_available_and_switch_to_it(locator):
238258
""" An expectation for checking whether the given frame is available to
239259
switch to. If the frame is available it switches the given driver to the
@@ -414,7 +434,7 @@ def _predicate(driver):
414434

415435

416436
def element_attribute_to_include(locator, attribute_):
417-
""" An expectation for checking if the given attribute is include in the
437+
""" An expectation for checking if the given attribute is included in the
418438
specified element.
419439
locator, attribute
420440
"""

py/test/selenium/webdriver/common/webdriverwait_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ def testExpectedConditionTextToBePresentInElementValue(driver, pages):
212212
assert 'Example Expected text' == driver.find_element(By.ID, 'inputRequired').get_attribute('value')
213213

214214

215+
def testExpectedConditionTextToBePresentInElementAttribute(driver, pages):
216+
pages.load('booleanAttributes.html')
217+
with pytest.raises(TimeoutException):
218+
WebDriverWait(driver, 1).until(EC.text_to_be_present_in_element_attribute((By.ID, 'inputRequired'), 'value', 'Expected'))
219+
driver.execute_script("setTimeout(function(){document.getElementById('inputRequired').value = 'Example Expected text'}, 200)")
220+
WebDriverWait(driver, 1).until(EC.text_to_be_present_in_element_attribute((By.ID, 'inputRequired'), 'value', 'Expected'))
221+
assert 'Example Expected text' == driver.find_element(By.ID, 'inputRequired').get_attribute('value')
222+
223+
215224
# xfail can be removed after 23 March 2021
216225
@pytest.mark.xfail_firefox(reason="https://bugzilla.mozilla.org/show_bug.cgi?id=1691348")
217226
def testExpectedConditionFrameToBeAvailableAndSwitchToItByLocator(driver, pages):

0 commit comments

Comments
 (0)