Skip to content

Commit

Permalink
Merge pull request #74 from saritasa-nest/feature/add-ability-to-not-…
Browse files Browse the repository at this point in the history
…specify-item-class-in-list-component

Allow optional `item_class` in `ListComponent`
  • Loading branch information
M1troll authored Jul 8, 2024
2 parents 254efe4 + 2263596 commit 5d68fd1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
8 changes: 8 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ Version history

We follow `Semantic Versions <https://semver.org/>`_.

0.8.0 (05.07.24)
*******************************************************************************
- Add ability to not specify ``item_class`` in ``ListComponent``. Instead, it
will be automatically filled with value passed in ``Generic[ListItemType]``.

**Warning**: The ``item_class`` specification is still available, but it is
deprecated and will be removed soon.

0.7.5
*******************************************************************************
- Remove redundant call of ``scroll_to`` in ``PomcornElement.click()``.
Expand Down
44 changes: 33 additions & 11 deletions pomcorn/component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Generic, TypeVar, overload
from typing import Generic, TypeVar, get_args, overload

from . import locators
from .element import XPathElement
Expand Down Expand Up @@ -177,21 +177,37 @@ class ListComponent(Generic[ListItemType, TPage], Component[TPage]):
* all
* get_item_by_text()
Waits for `base_item_locator` property to be implemented and value of
`item_class` to be set.
Waits for `base_item_locator` property to be overridden or one of the
attributes (`item_locator` or `relative_item_locator`) to be specified.
The `item_class` attribute is also required.
"""

item_class: type[ListItemType]

item_locator: locators.XPathLocator | None = None
relative_item_locator: locators.XPathLocator | None = None

def __init__(
self,
page: TPage,
base_locator: locators.XPathLocator | None = None,
wait_until_visible: bool = True,
):
super().__init__(page, base_locator, wait_until_visible)
if item_class := getattr(self, "item_class", None):
import warnings

warnings.warn(
DeprecationWarning(
"\nSpecifying `item_class` attribute in `ListComponent` "
f"({self.__class__}) is DEPRECATED. It is now "
"automatically substituted from Generic[ListItemType]. "
"Ability to specify this attribute will be removed soon.",
),
stacklevel=2,
)
self._item_class = item_class
else:
self._item_class = self._get_list_item_class()

@property
def base_item_locator(self) -> locators.XPathLocator:
"""Get the base locator of list item.
Expand Down Expand Up @@ -236,21 +252,27 @@ def all(self) -> list[ListItemType]:

items: list[ListItemType] = []
for locator in self.iter_locators(self.base_item_locator):
items.append(self.item_class(page=self.page, base_locator=locator))
items.append(
self._item_class(page=self.page, base_locator=locator),
)
return items

def get_item_by_text(self, text: str) -> ListItemType:
"""Get list item by text."""
locator = self.base_item_locator.extend_query(
extra_query=f"[contains(.,'{text}')]",
)
return self.item_class(page=self.page, base_locator=locator)
return self._item_class(page=self.page, base_locator=locator)

def _get_list_item_class(self) -> type[ListItemType]:
"""Return class passed in `Generic[ListItemType]`."""
return get_args(self.__orig_bases__[0])[0] # type: ignore

def __repr__(self) -> str:
return (
"ListComponent("
f"component={self.__class__}, "
f"item_class={self.item_class}, "
f"item_class={self._item_class}, "
f"base_item_locator={self.base_item_locator}, "
f"count={self.count}, "
f"items={self.all}, "
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pomcorn"
version = "0.7.5"
version = "0.8.0"
description = "Base implementation of Page Object Model"
authors = [
"Saritasa <pypi@saritasa.com>",
Expand Down

0 comments on commit 5d68fd1

Please sign in to comment.