Skip to content

Commit

Permalink
Improve specifying of base locators
Browse files Browse the repository at this point in the history
Add ability to specify `base_locator` and
`base_element_locator` via class attributes

Issue: #34
  • Loading branch information
M1troll committed Jan 31, 2024
1 parent 34e7f25 commit c1d172c
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 180 deletions.
15 changes: 1 addition & 14 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@
## In future

* Add arguments for `ButtonWithTextLocator` to choice `a` or `button` tag
* Provide ability to specify base locator and item locator as class attributes like:

```python
base_locator = locators.PropertyLocator(
prop="aria-label",
value="Search results",
)
relative_item_locator = locators.ClassLocator(
class_name="package-snippet",
container="a",
)
```

* Add ability to pass `**kwargs` to `ListComponent` items
* Add support for pages without a specified base URL (`APP_ROOT`)

* Add short comparison with other POM implementations
12 changes: 4 additions & 8 deletions demo/pages/common/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import TYPE_CHECKING

from pages import PyPIComponentWithBaseLocator, PyPIPage
from pages import PyPIComponentWithBaseLocator
from selenium.webdriver.common.keys import Keys

from pomcorn import locators
Expand All @@ -14,13 +14,9 @@
class Search(PyPIComponentWithBaseLocator):
"""Component representing the search input field."""

def __init__(
self,
page: PyPIPage,
base_locator: locators.XPathLocator = locators.IdLocator("search"),
wait_until_visible: bool = True,
):
super().__init__(page, base_locator, wait_until_visible)
# If you are not going to write anything in ``__init__`` and only want
# to set up ``base_locator``, you can specify it as a class attribute
base_locator = locators.IdLocator("search")

def find(self, text: str) -> SearchPage:
"""Paste the text into the search field and send `Enter` key.
Expand Down
36 changes: 15 additions & 21 deletions demo/pages/search_page/components/package_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,21 @@
class PackageList(ListComponent[Package, PyPIPage]):
"""Represent the list of search results on `SearchPage`."""

# The `ListComponent` item should always be `ComponentWithBaseLocator`,
# because all its methods depend on `base_locator`
# The ``ListComponent`` item should always be ``ComponentWithBaseLocator``,
# because all its methods depend on `base_locator`. Also this attribute is
# required.
item_class = Package

def __init__(
self,
page: PyPIPage,
# We use the empty init to set the default value for `base_locator`
base_locator: locators.XPathLocator = locators.PropertyLocator(
prop="aria-label",
value="Search results",
),
wait_until_visible: bool = True,
):
super().__init__(page, base_locator, wait_until_visible)
base_locator = locators.PropertyLocator(
prop="aria-label",
value="Search results",
)

# Set up `base_item_locator` and `item_class` is required
@property
def base_item_locator(self) -> locators.XPathLocator:
"""Get the base locator of result item."""
return self.base_locator // locators.ClassLocator(
class_name="package-snippet",
container="a",
)
# Set up ``relative_item_locator`` or ``item_locator`` is required.
# Use ``relative_item_locator`` - if you want locator nested within
# ``base_locator``, ``item_locator`` - otherwise."
# You also may override ``base_item_locator`` property.
relative_item_locator = locators.ClassLocator(
class_name="package-snippet",
container="a",
)
10 changes: 10 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ Version history

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

0.5.0
*******************************************************************************

- Add ability to specify ``base_locator`` for ``ComponentWithBaseLocator`` as a
class attribute, so as not to override `__init__` (Issue: `#34 <https://github.com/saritasa-nest/pomcorn/issues/34>`_)
- Add ability to specify ``basic_locator_element`` via ``locator_element`` and
``relative_locator_element`` attributes for ``ListComponent`` to avoid
overriding ``property`` each time and simplify creation of nested items
locators

0.4.0
*******************************************************************************

Expand Down
Loading

0 comments on commit c1d172c

Please sign in to comment.