Skip to content

Commit

Permalink
Refactor after PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
QuirrelForU committed Jan 31, 2025
1 parent e99064e commit 5832119
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
5 changes: 3 additions & 2 deletions pomcorn/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,9 @@ def is_valid_item_class(cls, item_class: Any) -> bool:
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(.,"
f" {self.base_item_locator._escape_quotes(text)})]",
extra_query=(
f"[contains(., {self.base_item_locator._escape_quotes(text)})]"
),
)
return self._item_class(page=self.page, base_locator=locator)

Expand Down
22 changes: 11 additions & 11 deletions pomcorn/locators/base_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def __bool__(self) -> bool:
"""Return whether query of current locator is empty or not."""
return bool(self.related_query)

@staticmethod
def _escape_quotes(text: str) -> str:
@classmethod
def _escape_quotes(cls, text: str) -> str:
"""Escape single and double quotes in given text for use in locators. # noqa: D202, E501.
This method is useful when locating elements
Expand All @@ -175,22 +175,22 @@ def _escape_quotes(text: str) -> str:
"""

if ('"' not in text and "'" not in text) or (not text):
if not text or ('"' not in text and "'" not in text):
return f'"{text}"'

escaped_parts = []
buffer = "" # Temporary storage for normal characters

for char in text:
if char in ('"', "'"):
if buffer:
escaped_parts.append(f'"{buffer}"')
buffer = ""
escaped_parts.append(
"'" + char + "'" if char == '"' else '"' + char + '"',
)
else:
if char not in ('"', "'"):
buffer += char
continue
if buffer:
escaped_parts.append(f'"{buffer}"')
buffer = ""
escaped_parts.append(
"'" + char + "'" if char == '"' else '"' + char + '"',
)

if buffer:
escaped_parts.append(f'"{buffer}"')
Expand Down
12 changes: 8 additions & 4 deletions pomcorn/locators/xpath_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ class InputByLabelLocator(XPathLocator):
def __init__(self, label: str):
"""Init XPathLocator."""
super().__init__(
query=f"//label[contains(.,"
f" {self._escape_quotes(label)})]/following-sibling::input",
query=(
f"//label[contains(., {self._escape_quotes(label)})]"
"/following-sibling::input"
),
)


Expand All @@ -260,6 +262,8 @@ class TextAreaByLabelLocator(XPathLocator):
def __init__(self, label: str):
"""Init XPathLocator."""
super().__init__(
query="//*[label[contains(text(),"
f" {self._escape_quotes(label)})]]/textarea",
query=(
"//*[label[contains(text(), "
f"{self._escape_quotes(label)})]]/textarea"
),
)

0 comments on commit 5832119

Please sign in to comment.