Skip to content

Commit

Permalink
Add __getitem__ to XPathLocator
Browse files Browse the repository at this point in the history
Add ability to get related xpath
locators by index
  • Loading branch information
M1troll committed Feb 11, 2025
1 parent 161cee4 commit 333d98d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ words:
- yaspeller

# Code words
- argnames
- argvalues
- docstrings
- conftest
- kwargs
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Version history

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

0.8.5 (10.02.25)
*******************************************************************************
- Add ability to get related xpath locators by index for ``XPathLocator``

0.8.4 (30.01.25)
*******************************************************************************
- Add escaping single and double quotes in the: ``ElementWithTextLocator``,
Expand Down
22 changes: 22 additions & 0 deletions pomcorn/locators/base_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ def __or__(self, other: XPathLocator) -> XPathLocator:
"""
return XPathLocator(query=f"({self.query} | {other.query})")

def __getitem__(self, index: int) -> XPathLocator:
"""Allow get related xpath locator by index.
Example:
div_locator = XPathLocator("//div") --> `//div`
div_locator[0] --> `(//div)[1]` # xpath numeration starts with 1
div_locator[-1] --> `(//div)[last()]`
"""
query = f"({self.query})"

if index >= 0:
# `+1` is used here because numeration in xpath starts with 1
query += f"[{index + 1}]"
elif index == -1:
# To avoid ugly locators with `...)[last() - 0]`
query += "[last()]"
else:
query += f"[last() - {abs(index + 1)}]"

return XPathLocator(query)

def __bool__(self) -> bool:
"""Return whether query of current locator is empty or not."""
return bool(self.related_query)
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.4"
version = "0.8.5"
description = "Base implementation of Page Object Model"
authors = [
"Saritasa <pypi@saritasa.com>",
Expand Down
20 changes: 20 additions & 0 deletions tests/locators/test_get_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from pomcorn.locators.base_locators import XPathLocator

TEST_QUERY = "//span[text()='Users'][last()]"


@pytest.mark.parametrize(
argnames=["index", "result"],
argvalues=[
[-2, f"({TEST_QUERY})[last() - 1]"],
[-1, f"({TEST_QUERY})[last()]"],
[0, f"({TEST_QUERY})[1]"],
[1, f"({TEST_QUERY})[2]"],
],
)
def test_getting_related_xpath_locators_by_index(index: int, result: str):
"""Check that getting related xpath locators by index works correct."""
locator = XPathLocator(TEST_QUERY)
assert locator[index].query == result

0 comments on commit 333d98d

Please sign in to comment.