Skip to content

Commit 095ad85

Browse files
committed
Merge pull request #408 from molsky/wait_until_contain_keywords
Add 'Wait Until Element Contains' and 'Wait Until Element Does Not Contain' Keywords
2 parents 4cf850a + c30c52c commit 095ad85

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
@@ -27,6 +27,9 @@ Release Notes
2727
- Added new keyword 'Wait Until Page Does Not Contain Element'.
2828
[molsky]
2929

30+
- Added new keywords 'Wait Until Element Contains' and 'Wait Until Element Does Not Contain'
31+
[molsky]
32+
3033
- Added new locator strategy, scLocator, for finding SmartClient and SmartGWT elements.
3134
[IlfirinPL]
3235

src/Selenium2Library/keywords/_waiting.py

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

177177
self._wait_until_no_error(timeout, check_enabled)
178178

179+
def wait_until_element_contains(self, locator, text, timeout=None, error=None):
180+
"""Waits until given element contains `text`.
181+
182+
Fails if `timeout` expires before the text appears on given element. See
183+
`introduction` for more information about `timeout` and its
184+
default value.
185+
186+
`error` can be used to override the default error message.
187+
188+
See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`,
189+
`Wait Until Element Is Visible` and BuiltIn keyword `Wait Until
190+
Keyword Succeeds`.
191+
"""
192+
element = self._element_find(locator, True, True)
193+
def check_text():
194+
actual = element.text
195+
if text in actual:
196+
return
197+
else:
198+
return error or "Text '%s' did not appear in %s to element '%s'. " \
199+
"Its text was '%s'." % (text, self._format_timeout(timeout), locator, actual)
200+
self._wait_until_no_error(timeout, check_text)
201+
202+
203+
def wait_until_element_does_not_contain(self, locator, text, timeout=None, error=None):
204+
"""Waits until given element does not contain `text`.
205+
206+
Fails if `timeout` expires before the text disappears from given element. See
207+
`introduction` for more information about `timeout` and its
208+
default value.
209+
210+
`error` can be used to override the default error message.
211+
212+
See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`,
213+
`Wait Until Element Is Visible` and BuiltIn keyword `Wait Until
214+
Keyword Succeeds`.
215+
"""
216+
element = self._element_find(locator, True, True)
217+
def check_text():
218+
actual = element.text
219+
if not text in actual:
220+
return
221+
else:
222+
return error or "Text '%s' did not disappear in %s from element '%s'." % (text, self._format_timeout(timeout), locator)
223+
self._wait_until_no_error(timeout, check_text)
224+
179225
# Private
180226

181227
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
@@ -38,3 +38,16 @@ Wait Until Element Is Enabled
3838
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
3939
Run Keyword And Expect Error User error message Wait Until Element Is Enabled id=invalid 0.1 User error message
4040

41+
Wait Until Element Contains
42+
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
43+
Wait Until Element Contains content New Content 2 s
44+
Wait Until Element Contains content New 2 s
45+
Run Keyword And Expect Error User error message Wait Until Element Contains content Error 0.1 User error message
46+
Run Keyword And Expect Error ValueError: Element locator 'id=invalid' did not match any elements. Wait Until Element Contains id=invalid content 0.1
47+
48+
Wait Until Element Does Not Contain
49+
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
50+
Wait Until Element Does Not Contain content This is 2 s
51+
Wait Until Element Does Not Contain id=content content 2 s
52+
Run Keyword And Expect Error User error message Wait Until Element Does Not Contain content New Content 0.1 User error message
53+

0 commit comments

Comments
 (0)