From 85cc3693422006564c155ca8e3286059c8ad1e5b Mon Sep 17 00:00:00 2001 From: "GISINC\\Nathan.Hannig" Date: Thu, 6 May 2021 13:14:46 -0700 Subject: [PATCH] add Wait Until keywords for Textarea Wait Until Textarea Contains - wait_until_textarea_contains Wait Until Textarea Does Not Contains - wait_until_textarea_does_not_contain Wait Until Textarea Value Is - wait_until_textarea_value_is Wait Until Textarea Value Is Not - wait_until_textarea_value_is_not --- src/SeleniumLibrary/keywords/waiting.py | 103 ++++++++++++++++++ .../test_keyword_arguments_waiting.py | 56 ++++++++++ 2 files changed, 159 insertions(+) diff --git a/src/SeleniumLibrary/keywords/waiting.py b/src/SeleniumLibrary/keywords/waiting.py index 62c84c9f2..39639a1b6 100644 --- a/src/SeleniumLibrary/keywords/waiting.py +++ b/src/SeleniumLibrary/keywords/waiting.py @@ -417,6 +417,106 @@ def wait_until_element_does_not_contain( error, ) + @keyword + def wait_until_textarea_contains( + self, + locator: Union[WebElement, None, str], + text: str, + timeout: Optional[timedelta] = None, + message: Optional[str] = None, + ): + """Waits until the textarea ``locator`` contains ``text``. + + Fails if ``timeout`` expires before the text appears. See + the `Timeouts` section for more information about using timeouts and + their default value and the `Locating elements` section for details + about the locator syntax. + + The ``message`` argument can be used to override the default error + message. + """ + self._wait_until( + lambda: text in self._get_value(locator, "text area"), + f"Textarea '{locator}' did not get text '{text}' in .", + timeout, + message, + ) + + @keyword + def wait_until_textarea_does_not_contain( + self, + locator: Union[WebElement, None, str], + text: str, + timeout: Optional[timedelta] = None, + message: Optional[str] = None, + ): + """Waits until the textarea ``locator`` does not contain ``text``. + + Fails if ``timeout`` expires before the text disappears. See + the `Timeouts` section for more information about using timeouts and + their default value and the `Locating elements` section for details + about the locator syntax. + + The ``message`` argument can be used to override the default error + message. + """ + self._wait_until( + lambda: text not in self._get_value(locator, "text area"), + f"Textarea '{locator}' still had text '{text}' after .", + timeout, + message, + ) + + @keyword + def wait_until_textarea_value_is( + self, + locator: Union[WebElement, None, str], + text: str, + timeout: Optional[timedelta] = None, + message: Optional[str] = None, + ): + """Waits until the textarea ``locator`` has exactly text ``text``. + + Fails if ``timeout`` expires before the text appears. See + the `Timeouts` section for more information about using timeouts and + their default value and the `Locating elements` section for details + about the locator syntax. + + The ``message`` argument can be used to override the default error + message. + """ + self._wait_until( + lambda: text == self._get_value(locator, "text area"), + f"Textarea '{locator}' did not get text '{text}' in .", + timeout, + message, + ) + + @keyword + def wait_until_textarea_value_is_not( + self, + locator: Union[WebElement, None, str], + text: str, + timeout: Optional[timedelta] = None, + message: Optional[str] = None, + ): + """Waits until the textarea ``locator`` does not has exactly text ``text``. + + Fails if ``timeout`` expires before the text appears. See + the `Timeouts` section for more information about using timeouts and + their default value and the `Locating elements` section for details + about the locator syntax. + + The ``message`` argument can be used to override the default error + message. + """ + self._wait_until( + lambda: text != self._get_value(locator, "text area"), + f"Textarea '{locator}' still had text '{text}' in .", + timeout, + message, + ) + def _wait_until(self, condition, error, timeout=None, custom_error=None): timeout = self.get_timeout(timeout) if custom_error is None: @@ -441,3 +541,6 @@ def _wait_until_worker(self, condition, timeout, error): not_found = None time.sleep(0.2) raise AssertionError(not_found or error) + + def _get_value(self, locator, tag): + return self.find_element(locator, tag).get_attribute("value") diff --git a/utest/test/keywords/test_keyword_arguments_waiting.py b/utest/test/keywords/test_keyword_arguments_waiting.py index 9809b8314..19b5a900c 100644 --- a/utest/test/keywords/test_keyword_arguments_waiting.py +++ b/utest/test/keywords/test_keyword_arguments_waiting.py @@ -40,3 +40,59 @@ def test_wait_until_page_contains(waiting): with pytest.raises(AssertionError) as error: waiting.wait_until_page_contains(text, None, "error") assert "error" in str(error.value) + + +def test_wait_until_textarea_contains(waiting): + locator = "//textarea" + element = mock() + when(waiting).find_element(locator, "text area").thenReturn(element) + when(element).get_attribute("value").thenReturn("no") + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_contains(locator, "value") + assert "did not get text" in str(error.value) + + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_contains(locator, "value", None, "foobar error") + assert "foobar error" in str(error.value) + + +def test_wait_until_textarea_does_not_contain(waiting): + locator = "//textarea" + element = mock() + when(waiting).find_element(locator, "text area").thenReturn(element) + when(element).get_attribute("value").thenReturn("value") + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_does_not_contain(locator, "value") + assert "still had text" in str(error.value) + + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_does_not_contain(locator, "value", None, "foobar error") + assert "foobar error" in str(error.value) + + +def test_wait_until_textarea_value_is(waiting): + locator = "//textarea" + element = mock() + when(waiting).find_element(locator, "text area").thenReturn(element) + when(element).get_attribute("value").thenReturn("no") + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_value_is(locator, "value") + assert "did not get text" in str(error.value) + + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_value_is(locator, "value", None, "foobar error") + assert "foobar error" in str(error.value) + + +def test_wait_until_textarea_value_is_not(waiting): + locator = "//textarea" + element = mock() + when(waiting).find_element(locator, "text area").thenReturn(element) + when(element).get_attribute("value").thenReturn("value") + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_value_is_not(locator, "value") + assert "still had text" in str(error.value) + + with pytest.raises(AssertionError) as error: + waiting.wait_until_textarea_value_is_not(locator, "value", None, "foobar error") + assert "foobar error" in str(error.value)