Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __getitem__ to XPathLocator #109

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}]"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems -index+1 is enough for this case, cuz due to conditions index can only be (-inf;-2]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can
But I just decided to use the current variant because if we leave the -index+1 option, we get a less readable query:

index = -2
f"[last() - {abs(index + 1)}]"  --> [last() - 1] "

f"[last(){index + 1}]"  --> [last()-1] "  <-- *no spaces, harder to read

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤙🏻


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 <[email protected]>",
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']"


@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
Loading