Skip to content

Commit

Permalink
[#530] REFACTOR: support ignore_case on match.url*
Browse files Browse the repository at this point in the history
  • Loading branch information
yashaka committed Jul 12, 2024
1 parent 292dcaf commit 9a32470
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ should we even refactor out them from Condition and move to Match only?

### TODO: rename all conditions inside match.py so match can be fully used instead be + have #530

### TODO: should we support ignorecase on asserting urls too?

### TODO: should we make ingorecase optionally default for all conditions supporting it?
### TODO: should we make ignorecase optionally default for all conditions supporting it?

### Deprecated conditions

Expand Down Expand Up @@ -538,6 +536,8 @@ See a bit more in documented ["FAQ: How to work with iFrames in Selene?"](https:
- `have.size(dict_of_element_size)`
- browser conditions
- `have.size(dict_of_browser_size)`
- `have.url(string).ignore_case`
- `have.url_containing(string).ignore_case`

#### Additional Config options relating conditions behavior

Expand Down
39 changes: 21 additions & 18 deletions selene/core/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

from selene.common import predicate, helpers, appium_tools
from selene.core import query
from selene.core.condition import Condition, Match
from selene.core.condition import Condition, Match, E
from selene.core.conditions import (
ElementCondition,
CollectionCondition,
Expand All @@ -59,7 +59,7 @@
# GENERAL CONDITION BUILDERS ------------------------------------------------- #


class _ElementHasSomethingSupportingIgnoreCase(Match[Element]):
class _EntityHasSomethingSupportingIgnoreCase(Match[E]):
def __init__(
self,
name,
Expand Down Expand Up @@ -96,7 +96,7 @@ def __init__(
)

@property
def ignore_case(self) -> Condition[Element]:
def ignore_case(self) -> Condition[E]:
return self.__class__(
self.__name,
self.__expected,
Expand All @@ -108,7 +108,7 @@ def ignore_case(self) -> Condition[Element]:
)


class _CollectionHasSomethingSupportingIgnoreCase(Match[Collection]):
class _CollectionHasSomeThingsSupportingIgnoreCase(Match[Collection]):
def __init__(
self,
name,
Expand Down Expand Up @@ -350,7 +350,7 @@ def __x_exact_text(expected: str | int | float, _ignore_case=False, _inverted=Fa


def text(expected: str | int | float, _ignore_case=False, _inverted=False):
return _ElementHasSomethingSupportingIgnoreCase(
return _EntityHasSomethingSupportingIgnoreCase(
'has text', # TODO: is it here name or description? probably it's even a "name prefix"
expected,
actual=query.text,
Expand All @@ -361,7 +361,7 @@ def text(expected: str | int | float, _ignore_case=False, _inverted=False):


def exact_text(expected: str | int | float, _ignore_case=False, _inverted=False):
return _ElementHasSomethingSupportingIgnoreCase(
return _EntityHasSomethingSupportingIgnoreCase(
'has exact text',
expected,
actual=query.text,
Expand Down Expand Up @@ -523,7 +523,7 @@ def __init__(self, name: str, _inverted=False):
)

def value(self, expected):
return _ElementHasSomethingSupportingIgnoreCase(
return _EntityHasSomethingSupportingIgnoreCase(
f"has attribute '{self.__expected}' with value",
expected=expected,
actual=query.attribute(self.__expected),
Expand All @@ -532,7 +532,7 @@ def value(self, expected):
)

def value_containing(self, expected):
return _ElementHasSomethingSupportingIgnoreCase(
return _EntityHasSomethingSupportingIgnoreCase(
f"has attribute '{self.__expected}' with value containing",
expected=expected,
actual=query.attribute(self.__expected),
Expand All @@ -541,7 +541,7 @@ def value_containing(self, expected):
)

def values(self, *expected: str | int | float | Iterable[str]):
return _CollectionHasSomethingSupportingIgnoreCase(
return _CollectionHasSomeThingsSupportingIgnoreCase(
f"has attribute '{self.__expected}' with values",
*expected,
actual=query.attributes(self.__expected),
Expand All @@ -550,7 +550,7 @@ def values(self, *expected: str | int | float | Iterable[str]):
)

def values_containing(self, *expected: str | int | float | Iterable[str]):
return _CollectionHasSomethingSupportingIgnoreCase(
return _CollectionHasSomeThingsSupportingIgnoreCase(
f"has attribute '{self.__expected}' with values containing",
*expected,
actual=query.attributes(self.__expected),
Expand Down Expand Up @@ -579,7 +579,7 @@ def css_class(name: str, _inverted=False):
def class_attribute_value(element: Element):
return element.locate().get_attribute('class')

return _ElementHasSomethingSupportingIgnoreCase(
return _EntityHasSomethingSupportingIgnoreCase(
'has css class',
expected=name,
actual=class_attribute_value,
Expand Down Expand Up @@ -836,7 +836,7 @@ def texts(
*expected: str | int | float | Iterable[str], _ignore_case=False, _inverted=False
):
# todo: consider counting _match_only_visible_elements_texts in name
return _CollectionHasSomethingSupportingIgnoreCase(
return _CollectionHasSomeThingsSupportingIgnoreCase(
'have texts',
*expected,
actual=lambda collection: (
Expand All @@ -854,7 +854,7 @@ def exact_texts(
*expected: str | int | float | Iterable[str], _ignore_case=False, _inverted=False
):
# todo: consider counting _match_only_visible_elements_texts in name
return _CollectionHasSomethingSupportingIgnoreCase(
return _CollectionHasSomeThingsSupportingIgnoreCase(
'have exact texts',
*expected,
actual=lambda collection: (
Expand Down Expand Up @@ -1426,13 +1426,16 @@ def process_wildcards(item: str) -> str:
# )


# TODO: should we make it supporting ignorecase?
def url(expected: str, _name='has url', _by=predicate.equals) -> Condition[Browser]:
return Match(f"{_name} '{expected}'", query.url, by=_by(expected))
def url(
expected: str, _name='has url', _by=predicate.equals, _inverted=False
) -> _EntityHasSomethingSupportingIgnoreCase[Browser]:
return _EntityHasSomethingSupportingIgnoreCase(
_name, expected, actual=query.url, by=_by, _inverted=_inverted
)


def url_containing(expected: str) -> Condition[Browser]:
return url(expected, 'has url containing', predicate.includes)
def url_containing(expected: str, _inverted=False):
return url(expected, 'has url containing', predicate.includes, _inverted=_inverted)


def browser_has_title(
Expand Down
8 changes: 4 additions & 4 deletions selene/support/conditions/not_.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ def _texts_like(*contained_texts_or_item_placeholders: str | int | float | Itera
return _match._texts_like(*contained_texts_or_item_placeholders, _inverted=True)


def url(exact_value: str) -> Condition[Browser]:
return _match.url(exact_value).not_
def url(exact_value: str):
return _match.url(exact_value, _inverted=True)


def url_containing(partial_value: str) -> Condition[Browser]:
return _match.url_containing(partial_value).not_
def url_containing(partial_value: str):
return _match.url_containing(partial_value, _inverted=True)


def title(exact_value: str) -> Condition[Browser]:
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/condition__browser__have_url_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ def test_have_url_containing(session_browser):
session_browser.should(have.no.url_containing('start_page.xhtml'))


def test_have_url_containing__ignore_case(session_browser):
browser = session_browser.with_(timeout=0.1)
GivenPage(browser.driver).opened_empty()
browser.should(have.url_containing('EMPTY.html').ignore_case)
try:
browser.should(have.no.url_containing('EMPTY.html').ignore_case)
pytest.fail('expect mismatch')
except AssertionError as error:
assert re.findall(
re.escape(
"browser.has no (url containing ignoring case: 'EMPTY.html')\n"
'\n'
'Reason: ConditionMismatch: actual url: '
)
+ r'file:.*empty\.html\n',
str(error),
)
browser.should(have.no.url_containing('start_page.xhtml'))
browser.should(have.no.url_containing('start_page.xhtml').ignore_case)


def test_fails_on_timeout_during_waiting_for_exact_url(session_browser):
browser = session_browser.with_(timeout=0.1)

Expand Down

0 comments on commit 9a32470

Please sign in to comment.