Skip to content

Commit

Permalink
Merge pull request #13 from AdCombo/py310_ready
Browse files Browse the repository at this point in the history
ListOfElementDescriptor with suffix
  • Loading branch information
dpanfilovadcombo authored Oct 8, 2024
2 parents 49c70cf + 891df10 commit 272a684
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions combo_e2e/pages/base_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def _search_element(self, page):
class ListOfElementDescriptor:
"""
A descriptor class similar to ElementDescriptor, but to describe a group of elements,
which differ in indices.
which differ in indices or suffixes.
<button name="row_1"></button>
<button name="row_2"></button>
This class is needed in order not to multiply the description of such elements in the base page.
Expand All @@ -291,8 +291,14 @@ class ListOfElementDescriptor:
Also allows to describe elements like this:
<button name="row_1_foo"></button>
<button name="row_2_foo"></button>
You can use end_name_part parametr:
You can use end_name_part parameter:
elements = ListOfElementDescriptor(base_name_parts=['row_'], end_name_part='_foo')
Additionally, elements can be accessed by a suffix string:
elements.get_by_suffix('foo') will return the element with name "row_foo"
Elements can also be accessed using bracket notation like this:
elements['foo'] will return the element with name "row_foo"
Multiple elements can be accessed using a list of suffixes:
elements[['foo', 'bar']] will return a list of elements with names "row_foo" and "row_bar"
"""

base_name_parts: list = None
Expand Down Expand Up @@ -344,6 +350,33 @@ def get_by_index(self, *numbers) -> WebElementProxy:
raise BasePageException("all of parameters must be int")
return self.get(*numbers)

def get_by_suffix(self, suffix: str) -> WebElementProxy:
"""
Get an element by its suffix part on the rendered page.
:param suffix: The suffix to identify the element
:return:
"""
if not isinstance(suffix, str):
raise BasePageException("suffix must be a string")
attr_name = self._make_attr_name_with_suffix(suffix)
descriptor = self._get_attribute_descriptor(attr_name)
return descriptor.__get__(self.page)

def get_multiple_by_suffixes(self, suffixes: List[str]) -> List[WebElementProxy]:
"""
Get multiple elements by their suffix parts on the rendered page.
:param suffixes: List of suffixes to identify the elements
:return: List of WebElementProxy objects
"""
if not all([isinstance(suffix, str) for suffix in suffixes]):
raise BasePageException("all suffixes must be strings")
elements = []
for suffix in suffixes:
attr_name = self._make_attr_name_with_suffix(suffix)
descriptor = self._get_attribute_descriptor(attr_name)
elements.append(descriptor.__get__(self.page))
return elements

def get_no_load(self, *numbers) -> ElementDescriptor:
"""
Returns element descriptor.
Expand Down Expand Up @@ -403,15 +436,31 @@ def _make_attr_name(self, args):

return "_".join(indexed_names)

def _make_attr_name_with_suffix(self, suffix: str) -> str:
"""
Create the attribute name by appending the suffix to the base name parts.
:param suffix: The suffix to append
:return: The full attribute name
"""
base_name = "_".join(self.base_name_parts)
if self.end_name_part:
base_name = f"{base_name}_{self.end_name_part}"
return f"{base_name}_{suffix}"

def __get__(self, page, objtype=None):
self.page = page
return self

def __getitem__(self, item: int):
if not isinstance(item, int):
def __getitem__(self, item):
if isinstance(item, int):
if getattr(self.page, "nested_table", None) is True:
return self.get_relative(item)
return self.get(item)
elif isinstance(item, str):
return self.get_by_suffix(item)
elif isinstance(item, list):
return self.get_multiple_by_suffixes(item)
else:
raise BasePageException(
"ListOfElementDescriptor support only number access to attributes"
"ListOfElementDescriptor supports only number, string, or list access to attributes"
)
if getattr(self.page, "nested_table", None) is True:
return self.get_relative(item)
return self.get(item)

0 comments on commit 272a684

Please sign in to comment.