Skip to content

Commit c30c52c

Browse files
committed
Add 'Wait Until Element Contains' and 'Wait Until Element Does Not Contain' keywords
1 parent 4efcb9c commit c30c52c

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Release Notes
2424
- Added new keyword 'Wait Until Page Does Not Contain Element'.
2525
[molsky]
2626

27+
- Added new keywords 'Wait Until Element Contains' and 'Wait Until Element Does Not Contain'
28+
[molsky]
29+
2730
- Added new locator strategy, scLocator, for finding SmartClient and SmartGWT elements.
2831
[IlfirinPL]
2932

src/Selenium2Library/keywords/_waiting.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,52 @@ def check_enabled():
155155

156156
self._wait_until_no_error(timeout, check_enabled)
157157

158+
def wait_until_element_contains(self, locator, text, timeout=None, error=None):
159+
"""Waits until given element contains `text`.
160+
161+
Fails if `timeout` expires before the text appears on given element. See
162+
`introduction` for more information about `timeout` and its
163+
default value.
164+
165+
`error` can be used to override the default error message.
166+
167+
See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`,
168+
`Wait Until Element Is Visible` and BuiltIn keyword `Wait Until
169+
Keyword Succeeds`.
170+
"""
171+
element = self._element_find(locator, True, True)
172+
def check_text():
173+
actual = element.text
174+
if text in actual:
175+
return
176+
else:
177+
return error or "Text '%s' did not appear in %s to element '%s'. " \
178+
"Its text was '%s'." % (text, self._format_timeout(timeout), locator, actual)
179+
self._wait_until_no_error(timeout, check_text)
180+
181+
182+
def wait_until_element_does_not_contain(self, locator, text, timeout=None, error=None):
183+
"""Waits until given element does not contain `text`.
184+
185+
Fails if `timeout` expires before the text disappears from given element. See
186+
`introduction` for more information about `timeout` and its
187+
default value.
188+
189+
`error` can be used to override the default error message.
190+
191+
See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`,
192+
`Wait Until Element Is Visible` and BuiltIn keyword `Wait Until
193+
Keyword Succeeds`.
194+
"""
195+
element = self._element_find(locator, True, True)
196+
def check_text():
197+
actual = element.text
198+
if not text in actual:
199+
return
200+
else:
201+
return error or "Text '%s' did not disappear in %s from element '%s'." % (text, self._format_timeout(timeout), locator)
202+
self._wait_until_no_error(timeout, check_text)
203+
158204
# Private
159205

160206
def _wait_until(self, timeout, error, function, *args):

test/acceptance/keywords/waiting.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,16 @@ Wait Until Element Is Enabled
3434
Run Keyword And Expect Error Element locator 'id=invalid' did not match any elements after 100 milliseconds Wait Until Element Is Enabled id=invalid 0.1
3535
Run Keyword And Expect Error User error message Wait Until Element Is Enabled id=invalid 0.1 User error message
3636

37+
Wait Until Element Contains
38+
Run Keyword And Expect Error Text 'New' did not appear in 100 milliseconds to element 'id=content'. Its text was 'This is content'. Wait Until Element Contains id=content New 0.1
39+
Wait Until Element Contains content New Content 2 s
40+
Wait Until Element Contains content New 2 s
41+
Run Keyword And Expect Error User error message Wait Until Element Contains content Error 0.1 User error message
42+
Run Keyword And Expect Error ValueError: Element locator 'id=invalid' did not match any elements. Wait Until Element Contains id=invalid content 0.1
43+
44+
Wait Until Element Does Not Contain
45+
Run Keyword And Expect Error Text 'This is' did not disappear in 100 milliseconds from element 'id=content'. Wait Until Element Does Not Contain id=content This is 0.1
46+
Wait Until Element Does Not Contain content This is 2 s
47+
Wait Until Element Does Not Contain id=content content 2 s
48+
Run Keyword And Expect Error User error message Wait Until Element Does Not Contain content New Content 0.1 User error message
49+

0 commit comments

Comments
 (0)