Skip to content

849 verify text case insensitive #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 12, 2018
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release Notes
3.1.0
-----
- Added a message param to `Title Should Be` to display custom error message [rubygeek]
- Compare text regardless of case in: `Element Should Contain`, `Element Should Not Contain` and `Element Text Should Be` by passing `ignore_case=True`. The default is `False`

3.0.0
-----
Expand Down
46 changes: 36 additions & 10 deletions src/SeleniumLibrary/keywords/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_webelements(self, locator):
return self.find_elements(locator)

@keyword
def element_should_contain(self, locator, expected, message=None):
def element_should_contain(self, locator, expected, message=None, ignore_case=False):
"""Verifies that element ``locator`` contains text ``expected``.

See the `Locating elements` section for details about the locator
Expand All @@ -56,35 +56,53 @@ def element_should_contain(self, locator, expected, message=None):
The ``message`` argument can be used to override the default error
message.

The ``ignore_case`` argument can be set to True to compare case
insensitive, default is False. New in SeleniumLibrary 3.1.

``ignore_case`` argument new in SeleniumLibrary 3.1.

Use `Element Text Should Be` if you want to match the exact text,
not a substring.
"""
actual = self.find_element(locator).text
actual = actual_before = self.find_element(locator).text
expected_before = expected
if is_truthy(ignore_case):
actual = actual.lower()
expected = expected.lower()
if expected not in actual:
if is_noney(message):
message = "Element '%s' should have contained text '%s' but "\
"its text was '%s'." % (locator, expected, actual)
"its text was '%s'." % (locator, expected_before, actual_before)
raise AssertionError(message)
self.info("Element '%s' contains text '%s'." % (locator, expected))
self.info("Element '%s' contains text '%s'." % (locator, expected_before))

@keyword
def element_should_not_contain(self, locator, expected, message=None):
def element_should_not_contain(self, locator, expected, message=None, ignore_case=False ):
"""Verifies that element ``locator`` does not contains text ``expected``.

See the `Locating elements` section for details about the locator
syntax.

The ``message`` argument can be used to override the default error
message.

The ``ignore_case`` argument can be set to True to compare case
insensitive, default is False.

``ignore_case`` argument new in SeleniumLibrary 3.1.
"""
actual = self.find_element(locator).text
expected_before = expected
if is_truthy(ignore_case):
actual = actual.lower()
expected = expected.lower()
if expected in actual:
if is_noney(message):
message = "Element '%s' should not contain text '%s' but " \
"it did." % (locator, expected)
"it did." % (locator, expected_before)
raise AssertionError(message)
self.info("Element '%s' does not contain text '%s'."
% (locator, expected))
% (locator, expected_before))

@keyword
def page_should_contain(self, text, loglevel='INFO'):
Expand Down Expand Up @@ -288,7 +306,7 @@ def element_should_not_be_visible(self, locator, message=None):
raise AssertionError(message)

@keyword
def element_text_should_be(self, locator, expected, message=None):
def element_text_should_be(self, locator, expected, message=None, ignore_case=False):
"""Verifies that element ``locator`` contains exact text ``expected``.

See the `Locating elements` section for details about the locator
Expand All @@ -297,16 +315,24 @@ def element_text_should_be(self, locator, expected, message=None):
The ``message`` argument can be used to override the default error
message.

The ``ignore_case`` argument can be set to True to compare case
insensitive, default is False.

``ignore_case`` argument new in SeleniumLibrary 3.1.

Use `Element Should Contain` if a substring match is desired.
"""
self.info("Verifying element '%s' contains exact text '%s'."
% (locator, expected))
text = self.find_element(locator).text
text = before_text = self.find_element(locator).text
if is_truthy(ignore_case):
text = text.lower()
expected = expected.lower()
if text != expected:
if is_noney(message):
message = ("The text of element '%s' should have been '%s' "
"but it was '%s'."
% (locator, expected, text))
% (locator, expected, before_text))
raise AssertionError(message)

@keyword
Expand Down
17 changes: 17 additions & 0 deletions test/acceptance/keywords/content_assertions.robot
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,42 @@ Page Should Not Contain Element With Disabling Source Logging

Element Should Contain
Element Should Contain some_id This text is inside an identified element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add line: | Element Should Contain | some_id | This text is inside an identified element | ignore_case=False |

Element Should Contain some_id THIS TEXT IS INSIDE AN IDENTIFIED ELEMENT ignore_case=True
Element Should Contain some_id This text is inside an identified element ignore_case=False
Run Keyword And Expect Error
... Element 'some_id' should have contained text 'non existing text' but its text was 'This text is inside an identified element'.
... Element Should Contain some_id non existing text
Run Keyword And Expect Error
... Element with locator 'missing_id' not found.
... Element Should Contain missing_id This should report missing element.
Run Keyword And Expect Error
... Element 'some_id' should have contained text 'THIS TEXT' but its text was 'This text is inside an identified element'.
... Element Should Contain some_id THIS TEXT ignore_case=False
Run Keyword And Expect Error
... Element 'some_id' should have contained text 'foobar' but its text was 'This text is inside an identified element'.
... Element Should Contain some_id foobar ignore_case=True

Element Should Not Contain
Element Should Not Contain some_id This text is not inside an identified element
Element Should Not Contain some_id This text is not inside an identified element ignore_case=False
Element Should Not Contain some_id THIS TEXT is not inside an identified element ignore_case=True
Element Should Not Contain some_id elementypo
Run Keyword And Expect Error
... Element 'some_id' should not contain text 'This text is inside an identified element' but it did.
... Element Should Not Contain some_id This text is inside an identified element
Run Keyword And Expect Error
... Element 'some_id' should not contain text 'TEXT' but it did.
... Element Should Not Contain some_id TEXT ignore_case=True
Run Keyword And Expect Error
... Element 'some_id' should not contain text 'text' but it did.
... Element Should Not Contain some_id text ignore_case=False
Run Keyword And Expect Error
... Element with locator 'missing_id' not found.
... Element Should Not Contain missing_id This should report missing element.

Element Text Should Be
Element Text Should Be some_id This text is inside an identified element
Element Text Should Be some_id This TEXT IS INSIDE AN IDENTIFIED ELEMENT ignore_case=True
Run Keyword And Expect Error
... The text of element 'some_id' should have been 'inside' but it was 'This text is inside an identified element'.
... Element Text Should Be some_id inside
Expand Down