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
47 changes: 37 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,54 @@ 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.

Use `Element Text Should Be` if you want to match the exact text,
not a substring.
Copy link
Contributor

Choose a reason for hiding this comment

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

In new line, add text: ignore_case argument is new in SeleniumLibrary 3.1.

Please fix in keywords where the new argument is added.

"""
actual = self.find_element(locator).text
actual = actual_before = self.find_element(locator).text
expected_before = expected

Copy link
Contributor

Choose a reason for hiding this comment

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

We are not in favor of code blocks. Please remove the empty line from this line and from lines 71 and 76.

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.
"""
actual = self.find_element(locator).text
expected_before = expected

Copy link
Contributor

Choose a reason for hiding this comment

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

We are not in favor of code blocks. Please remove the empty line from this line and from line 99.

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 +307,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 +316,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.

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
7 changes: 7 additions & 0 deletions test/acceptance/keywords/content_assertions.robot
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,20 @@ 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
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 '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
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Please add line: Element Should Not Contain | some_id | This text is not inside an identified element | ignore_case=True |
  2. Please also add new test where Element Should Not Contain when ignore_caseargument is used with True and False values but the keyword fails. In the test use Run Keyword And Expect Errorto verify that correct error is raised.

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.
Expand All @@ -138,10 +143,12 @@ Element Should Not Contain

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


Copy link
Contributor

Choose a reason for hiding this comment

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

Empty line here is not needed.

Get Text
${str} = Get Text some_id
Should Match ${str} This text is inside an identified element
Expand Down