diff --git a/CHANGES.rst b/CHANGES.rst index 12faafcce..d00129184 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,9 @@ Release Notes - Added new keyword 'Wait Until Page Does Not Contain Element'. [molsky] +- Added new keywords 'Wait Until Element Contains' and 'Wait Until Element Does Not Contain' + [molsky] + - Added new locator strategy, scLocator, for finding SmartClient and SmartGWT elements. [IlfirinPL] diff --git a/src/Selenium2Library/keywords/_waiting.py b/src/Selenium2Library/keywords/_waiting.py index a40fb8983..c170bfbae 100644 --- a/src/Selenium2Library/keywords/_waiting.py +++ b/src/Selenium2Library/keywords/_waiting.py @@ -155,6 +155,52 @@ def check_enabled(): self._wait_until_no_error(timeout, check_enabled) + def wait_until_element_contains(self, locator, text, timeout=None, error=None): + """Waits until given element contains `text`. + + Fails if `timeout` expires before the text appears on given element. See + `introduction` for more information about `timeout` and its + default value. + + `error` can be used to override the default error message. + + See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`, + `Wait Until Element Is Visible` and BuiltIn keyword `Wait Until + Keyword Succeeds`. + """ + element = self._element_find(locator, True, True) + def check_text(): + actual = element.text + if text in actual: + return + else: + return error or "Text '%s' did not appear in %s to element '%s'. " \ + "Its text was '%s'." % (text, self._format_timeout(timeout), locator, actual) + self._wait_until_no_error(timeout, check_text) + + + def wait_until_element_does_not_contain(self, locator, text, timeout=None, error=None): + """Waits until given element does not contain `text`. + + Fails if `timeout` expires before the text disappears from given element. See + `introduction` for more information about `timeout` and its + default value. + + `error` can be used to override the default error message. + + See also `Wait Until Page Contains`, `Wait Until Page Contains Element`, `Wait For Condition`, + `Wait Until Element Is Visible` and BuiltIn keyword `Wait Until + Keyword Succeeds`. + """ + element = self._element_find(locator, True, True) + def check_text(): + actual = element.text + if not text in actual: + return + else: + return error or "Text '%s' did not disappear in %s from element '%s'." % (text, self._format_timeout(timeout), locator) + self._wait_until_no_error(timeout, check_text) + # Private def _wait_until(self, timeout, error, function, *args): diff --git a/test/acceptance/keywords/waiting.txt b/test/acceptance/keywords/waiting.txt index ce7227eb4..e99cbc184 100644 --- a/test/acceptance/keywords/waiting.txt +++ b/test/acceptance/keywords/waiting.txt @@ -34,3 +34,16 @@ Wait Until Element Is Enabled 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 Run Keyword And Expect Error User error message Wait Until Element Is Enabled id=invalid 0.1 User error message +Wait Until Element Contains + 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 + Wait Until Element Contains content New Content 2 s + Wait Until Element Contains content New 2 s + Run Keyword And Expect Error User error message Wait Until Element Contains content Error 0.1 User error message + Run Keyword And Expect Error ValueError: Element locator 'id=invalid' did not match any elements. Wait Until Element Contains id=invalid content 0.1 + +Wait Until Element Does Not Contain + 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 + Wait Until Element Does Not Contain content This is 2 s + Wait Until Element Does Not Contain id=content content 2 s + Run Keyword And Expect Error User error message Wait Until Element Does Not Contain content New Content 0.1 User error message +