Skip to content

Commit

Permalink
REFACTOR: match.selected + ...
Browse files Browse the repository at this point in the history
+ cover with tests
+ rename match.element_is_focused to js._active_element
- left some TODOs, especially on falsy_exceptions case...
  • Loading branch information
yashaka committed Jun 19, 2024
1 parent db8c502 commit 91a191d
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 9 deletions.
21 changes: 12 additions & 9 deletions selene/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
from selene.core.entity import Collection, Element
from selene.core._browser import Browser


present_in_dom: Condition[Element] = Match(
'is present in DOM',
actual=lambda element: element.locate(),
Expand Down Expand Up @@ -164,14 +165,16 @@ def __deprecated_is_existing(element: Element) -> bool:
)


# TODO: how will it work for mobile? – it will not work:)
element_is_focused: Condition[Element] = Match(
'is focused',
by=lambda element: (
element.locate()
== element.config.driver.execute_script('return document.activeElement')
),
)
class js:
# todo: how will it work for mobile? – it will not work:)
# do we even need it? – inside js class? or maybe be multiplatform?
_active_element: Condition[Element] = Match(
'has focus',
by=lambda element: (
element.locate()
== element.config.driver.execute_script('return document.activeElement')
),
)


class _ElementHasText(Condition[Element]):
Expand Down Expand Up @@ -517,7 +520,7 @@ def _is_collection_empty(collection: Collection) -> bool:


collection_is_empty: Condition[Collection] = CollectionCondition.raise_if_not(
'is empty', _is_collection_empty
'is empty', _is_collection_empty # noqa
)


Expand Down
4 changes: 4 additions & 0 deletions selene/support/conditions/not_.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
enabled: Condition[Element] = _match.enabled.not_
disabled: Condition[Element] = _match.disabled.not_

selected: Condition[Element] = _match.selected.not_

# focused: Condition[Element] = _match.focused.not_

blank: Condition[Element] = _match.element_is_blank.not_

# --- be.not_.* DEPRECATED conditions --- #
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# MIT License
#
# Copyright (c) 2015-2022 Iakiv Kramarenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# 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 be
from selene.core import match
from selene.core.condition import Match
from tests.integration.helpers.givenpage import GivenPage


# todo: consider covering radio buttons too...


def test_should_be_selected__passed_and_failed(session_browser):
s = lambda selector: session_browser.with_(timeout=0.1).element(selector)
GivenPage(session_browser.driver).opened_with_body(
'''
<!--<input type="checkbox" id="absent">-->
<input type="checkbox" id="hidden-selected" checked style="display: none">
<input type="checkbox" id="hidden" style="display: none">
<input type="checkbox" id="visible-selected" checked style="display: block">
<input type="checkbox" id="visible" style="display: block">
<p>Just a comment</p>
'''
)

# THEN

# selected?
# - visible & selected passes
s('#visible-selected').should(be.selected)
s('#visible-selected').should(be.selected.not_.not_)
# - visible & not selected fails with mismatch
try:
s('#visible').should(be.selected)
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#visible')).is selected\n"
'\n'
'Reason: ConditionMismatch: condition not matched\n'
'Screenshot: '
) in str(error)
# - visible & non-selectable fails still just with mismatch
try:
s('p').should(be.selected)
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', 'p')).is selected\n"
'\n'
'Reason: ConditionMismatch: condition not matched\n'
'Screenshot: '
) in str(error)
# - hidden & selected passes
s('#hidden-selected').should(be.selected)
# - hidden & not selected fails with mismatch
try:
s('#hidden').should(be.selected)
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#hidden')).is selected\n"
'\n'
'Reason: ConditionMismatch: condition not matched\n'
'Screenshot: '
) in str(error)
# - absent fails with failure
try:
s('#absent').should(be.selected)
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).is selected\n"
'\n'
'Reason: ConditionMismatch: Message: no such element: Unable to locate '
'element: {"method":"css selector","selector":"#absent"}\n'
' (Session info:' # ' chrome=126.0.6478.62); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# ':\n'
# 'condition not matched\n'
) in str(error)


def test_should_be_not_selected__passed_and_failed(
session_browser,
):
s = lambda selector: session_browser.with_(timeout=0.1).element(selector)
GivenPage(session_browser.driver).opened_with_body(
'''
<!--<input type="checkbox" id="absent">-->
<input type="checkbox" id="hidden-selected" checked style="display: none">
<input type="checkbox" id="hidden" style="display: none">
<input type="checkbox" id="visible-selected" checked style="display: block">
<input type="checkbox" id="visible" style="display: block">
<p>Just a comment</p>
'''
)

# THEN

# not selected?
# - visible & selected fails with mismatch
try:
s('#visible-selected').should(be.not_.selected)
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#visible-selected')).is not (selected)\n"
'\n'
'Reason: ConditionMismatch: condition not matched\n'
'Screenshot: '
) in str(error)
# - visible & not-selectable passes
s('p').should(match.selected.not_)
# - visible & not-selected passes
s('#visible').should(match.selected.not_)
s('#visible').should(be.not_.selected)
# - hidden & selected fails with mismatch
try:
s('#hidden-selected').should(be.not_.selected)
pytest.fail('expect mismatch')
except AssertionError as error:
assert (
"browser.element(('css selector', '#hidden-selected')).is not (selected)\n"
'\n'
'Reason: ConditionMismatch: condition not matched\n'
'Screenshot: '
) in str(error)
# - hidden & not-selected passed
s('#hidden').should(be.not_.selected)
# - absent fails with failure
try:
s('#absent').should(be.not_.selected)
pytest.fail('expect failure')
except AssertionError as error:
assert (
"browser.element(('css selector', '#absent')).is not (selected)\n"
'\n'
'Reason: ConditionMismatch: Message: no such element: Unable to locate '
'element: {"method":"css selector","selector":"#absent"}\n'
' (Session info:' # ' chrome=126.0.6478.62); For documentation on this error, '
# 'please visit: '
# 'https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\n'
# ':\n'
# 'condition not matched\n'
) in str(error)

0 comments on commit 91a191d

Please sign in to comment.