Skip to content

Commit

Permalink
Restore item_class attribute in ListComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
M1troll committed Nov 14, 2024
1 parent 44227eb commit 7f8257c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ words:
- STDLIB
- pydocstyle
- redef
- prefs

# RST blocks
- autodoc
Expand Down
8 changes: 7 additions & 1 deletion demo/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
@pytest.fixture(scope="session")
def webdriver() -> WebDriver:
"""Initialize `Chrome` webdriver."""
webdriver = selenium_webdriver.Chrome()
options = selenium_webdriver.ChromeOptions()

# Set browser's language to English
prefs = {"intl.accept_languages": "en,en_U"}
options.add_experimental_option("prefs", prefs)

webdriver = selenium_webdriver.Chrome(options)
webdriver.set_window_size(1920, 1080)
return webdriver

Expand Down
12 changes: 9 additions & 3 deletions demo/pages/search_page/components/package_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
from .package import Package


# The ``ListComponent`` item (Package) should always be ``Component``, because
# all its methods depend on `base_locator`.
class PackageList(ListComponent[Package, PyPIPage]):
"""Represent the list of search results on `SearchPage`."""

# The ``ListComponent`` item should always be ``Component``, because all
# its methods depend on `base_locator`. Also this attribute is required.
item_class = Package
# By default `ListComponent` have `item_class` attribute with stored first
# Generic variable (Package in current case). But during
# development/inheritance/addition of other generic variables it may become
# unavailable.

# In this case it is necessary to explicitly set `item_class` attribute:
# item_class = Package

base_locator = locators.PropertyLocator(
prop="aria-label",
Expand Down
11 changes: 11 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ Version history

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

0.8.1 (15.11.24)
*******************************************************************************
- Restore ability to specify ``item_class`` in ``ListComponent``. This is
because getting ``item_class`` from the first generic variable may become
invalid when inheriting and adding new generic variables in component.

**Note**: The ``item_class`` auto-filling with value passed in
``Generic[ListItemType]`` is still available. But it will be overridden by the
``item_class`` attribute if it is explicitly specified in the component class.


0.8.0 (05.07.24)
*******************************************************************************
- Add ability to not specify ``item_class`` in ``ListComponent``. Instead, it
Expand Down
29 changes: 18 additions & 11 deletions pomcorn/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ class ListComponent(Generic[ListItemType, TPage], Component[TPage]):
"""

item_class: type[ListItemType]

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

Expand All @@ -192,21 +194,26 @@ def __init__(
wait_until_visible: bool = True,
):
super().__init__(page, base_locator, wait_until_visible)
if item_class := getattr(self, "item_class", None):
list_item_type = self._get_list_item_class()
item_class = getattr(self, "item_class", None)

if item_class:
self.item_class = item_class
else:
self.item_class = list_item_type

if item_class == list_item_type:
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.",
f"The `item_class` you specify ({item_class}) is the same "
"as type specified in Generic[ListItemType] "
f"({list_item_type}). You do not need to "
f"specify `item_class` for `{self.__class__}",
),
stacklevel=2,
)
self._item_class = item_class
else:
self._item_class = self._get_list_item_class()

@property
def base_item_locator(self) -> locators.XPathLocator:
Expand Down Expand Up @@ -253,7 +260,7 @@ 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),
self.item_class(page=self.page, base_locator=locator),
)
return items

Expand All @@ -262,7 +269,7 @@ def get_item_by_text(self, text: str) -> ListItemType:
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]`."""
Expand All @@ -272,7 +279,7 @@ 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.8.0"
version = "0.8.1"
description = "Base implementation of Page Object Model"
authors = [
"Saritasa <[email protected]>",
Expand Down

0 comments on commit 7f8257c

Please sign in to comment.