Skip to content

Commit

Permalink
[#530] TEST: refactored have.exact_text coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
yashaka committed Jun 22, 2024
1 parent 3d457ac commit 60c18d6
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 15 deletions.
16 changes: 2 additions & 14 deletions selene/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,12 @@ def __init__(
self.__ignore_case = _ignore_case
self.__inverted = _inverted

# # actually happened to be not needed, both _ params and even closures of self
# def describe(_): # actually we don't need here passing self through describe
# return (
# f'{_describing_matched_to}'
# f'{" ignoring case: "if _ignore_case else ""} {expected}'
# )
#
# def compare(_): # here we also don't need it, closure's self is enough
# return lambda actual: (
# _compared_by_predicate_to(str(expected).lower())(str(actual).lower())
# if _ignore_case
# else _compared_by_predicate_to(str(expected))(str(actual))
# )

super().__init__(
(
f'{_describing_matched_to}'
f'{" ignoring case:" if _ignore_case else ""} {expected}'
# todo: refactor to and change tests correspondingly:
# f'{" ignoring case:" if _ignore_case else ":"} {expected}'
),
actual=query.text,
by=lambda actual: (
Expand Down
1 change: 0 additions & 1 deletion selene/support/conditions/have.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def exact_text(value: str | int | float):
return match.exact_text(value)


# TODO: consider accepting int
def text(partial_value: str | int | float):
return match.text(partial_value)

Expand Down
3 changes: 3 additions & 0 deletions selene/support/conditions/not_.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

from selene.core import match as _match

# TODO: consider breaking into be_not.* and have_no.*
# then, it can be implemented inside be.* and have.* (if utilizing classes)

# --- be.not_.* conditions --- #
from selene.core.condition import Condition
from selene.core.entity import Element, Collection
Expand Down
223 changes: 223 additions & 0 deletions tests/integration/condition__element__have_exact_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,236 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import pytest

from selene import have
from selene.core import match
from tests.integration.helpers.givenpage import GivenPage


# TODO: consider breaking it down into separate tests


def test_should_have_exact_text__passed_and_failed__with_text_to_trim(session_browser):
s = lambda selector: session_browser.with_(timeout=0.1).element(selector)
GivenPage(session_browser.driver).opened_with_body(
'''
<ul>
<!--<li id="absent"></li>-->
<li id="hidden-empty" checked style="display: none"></li>
<li id="hidden" style="display: none"> One !!!
</li>
<li id="visible-empty" checked style="display: block"></li>
<li id="visible" style="display: block"> One !!!
</li>
</ul>
'''
)

# THEN

# have exact text?
# - visible and correct expected (normalized) passes
s('#visible').should(match.exact_text('One !!!'))
s('#visible').should(have.exact_text('One !!!'))
s('#visible').should(have.exact_text('One !!!').not_.not_)
s('#visible').should(have.no.exact_text('One !!!').not_)
# - visible and incorrect expected (partial) fails
try:
s('#visible').should(have.exact_text('One'))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#visible')).has exact text One\n"
'\n'
'Reason: ConditionMismatch: actual text: One !!!\n'
'Screenshot: '
) in str(error)
# - visible & empty and correct expected passes
s('#visible-empty').should(have.exact_text(''))
s('#visible-empty').should(have.exact_text('').not_.not_)
# - visible & non-textable (like input) with always '' expected passes
'''
# let's just skip it:)
'''
# - hidden and with always '' expected passes
s('#hidden').should(have.exact_text(''))
# - hidden & empty with always '' expected passes
s('#hidden-empty').should(have.exact_text(''))
# - hidden and incorrect expected of actual exact text fails
try:
s('#hidden').should(have.exact_text('One !!!'))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#hidden')).has exact text One !!!\n"
'\n'
'Reason: ConditionMismatch: actual text: \n'
'Screenshot: ' # ...
) in str(error)
# - absent and expected '' fails with failure
try:
s('#absent').should(have.exact_text(''))
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).has exact text \n"
'\n'
'Reason: NoSuchElementException: no such element: Unable to locate element: '
'{"method":"css selector","selector":"#absent"}\n'
' (Session info: ' # 'chrome=126.0.6478.114); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# 'Screenshot: '
) in str(error)
# - absent and expected '' + double inversion fails with failure
try:
s('#absent').should(have.exact_text('').not_.not_)
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).has exact text \n"
'\n'
'Reason: NoSuchElementException: no such element: Unable to locate element: '
'{"method":"css selector","selector":"#absent"}\n'
' (Session info: ' # 'chrome=126.0.6478.114); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# 'Screenshot: '
) in str(error)


def test_should_have_no_exact_text__passed_and_failed__with_text_to_trim(
session_browser,
):
s = lambda selector: session_browser.with_(timeout=0.1).element(selector)
GivenPage(session_browser.driver).opened_with_body(
'''
<ul>
<!--<li id="absent"></li>-->
<li id="hidden-empty" checked style="display: none"></li>
<li id="hidden" style="display: none"> One !!!
</li>
<li id="visible-empty" checked style="display: block"></li>
<li id="visible" style="display: block"> One !!!
</li>
</ul>
'''
)

# THEN

# have no exact text?
# - visible and incorrect expected (not normalized) passes
s('#visible').should(match.exact_text(' One !!!\n').not_)
s('#visible').should(have.no.exact_text(' One !!!\n'))
s('#visible').should(have.no.exact_text(' One !!!\n').not_.not_)
# - visible and incorrect expected (partial) passes
s('#visible').should(match.exact_text('One').not_)
s('#visible').should(have.no.exact_text('One'))
s('#visible').should(have.no.exact_text('One').not_.not_)
# - visible and correct expected (normalized) fails
try:
s('#visible').should(have.no.exact_text('One !!!'))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#visible')).has no (exact text One !!!)\n"
'\n'
'Reason: ConditionMismatch: actual text: One !!!\n'
'Screenshot: ' # ...
) in str(error)
# - visible & empty and correct '' expected fails # todo: improve empty text rendering
try:
s('#visible-empty').should(have.no.exact_text(''))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#visible-empty')).has no (exact text )\n"
'\n'
'Reason: ConditionMismatch: actual text: \n'
'Screenshot: ' # ...
) in str(error)
# - visible & non-textable (like input) with always '' expected fails
'''
# let's just skip it:)
'''
# - hidden and with correct always '' expected fails
try:
s('#hidden').should(have.no.exact_text(''))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#hidden')).has no (exact text )\n"
'\n'
'Reason: ConditionMismatch: actual text: \n'
'Screenshot: ' # ...
) in str(error)
# - hidden & empty with always '' expected fails
try:
s('#hidden-empty').should(have.no.exact_text(''))
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#hidden-empty')).has no (exact text )\n"
'\n'
'Reason: ConditionMismatch: actual text: \n'
'Screenshot: ' # ...
) in str(error)
# - hidden and incorrect expected of actual exact text passes
s('#hidden').should(have.no.exact_text('One !!!'))
# - hidden & empty and incorrect expected of actual exact text passes
s('#hidden-empty').should(have.no.exact_text('One !!!'))
# - absent and potentially correct expected '' fails with failure
try:
s('#absent').should(have.no.exact_text(''))
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).has no (exact text )\n"
'\n'
'Reason: NoSuchElementException: no such element: Unable to locate element: '
'{"method":"css selector","selector":"#absent"}\n'
' (Session info: ' # 'chrome=126.0.6478.114); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# 'Screenshot: '
) in str(error)
# - absent and potentially correct expected '' + double inversion fails with failure
try:
s('#absent').should(have.no.exact_text('').not_.not_)
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).has no (exact text )\n"
'\n'
'Reason: NoSuchElementException: no such element: Unable to locate element: '
'{"method":"css selector","selector":"#absent"}\n'
' (Session info: ' # 'chrome=126.0.6478.114); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# 'Screenshot: '
) in str(error)
# - absent and incorrect expected STILL fails with failure
try:
s('#absent').should(have.no.exact_text('One !!!'))
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).has no (exact text One !!!)\n"
'\n'
'Reason: NoSuchElementException: no such element: Unable to locate element: '
'{"method":"css selector","selector":"#absent"}\n'
' (Session info: ' # 'chrome=126.0.6478.114); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# 'Screenshot: '
) in str(error)


# todo: consider putting/inserting (or moving from other test suite) here the ignoring_case tests


def test_unicode_text_with_trailing_and_leading_spaces(session_browser):
GivenPage(session_browser.driver).opened_with_body(
'''
Expand Down

0 comments on commit 60c18d6

Please sign in to comment.