Skip to content

Commit 54b3f6b

Browse files
Locator type rework
1 parent 574eca1 commit 54b3f6b

18 files changed

+248
-242
lines changed

mops/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = '3.0.1'
1+
__version__ = '3.1.0'
22
__project_name__ = 'mops'

mops/base/element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def __init_base_class__(self) -> None:
152152
raise DriverWrapperException(f'Cant specify {self.__class__.__name__}')
153153

154154
self._set_static(self._base_cls)
155-
self._base_cls.__init__(self, locator=self.locator)
155+
self._base_cls.__init__(self)
156156
self._initialized = True
157157

158158
# Following methods works same for both Selenium/Appium and Playwright APIs using internal methods

mops/mixins/internal_mixin.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616
available_kwarg_keys = ('desktop', 'mobile', 'ios', 'android')
1717

1818

19-
def get_element_info(element: Any) -> str:
19+
def get_element_info(element: Any, is_for_container: bool = False) -> str:
2020
"""
2121
Get element selector information with parent object selector if it exists
2222
2323
:param element: element to collect log data
24+
:param is_for_container: element to collect log data
2425
:return: log string
2526
"""
27+
label = '' if is_for_container else 'Selector: '
28+
29+
selector = f"{label}'{element.log_locator}'"
30+
2631
parent = element.parent
27-
current_data = f'Selector: ["{element.locator_type}": "{element.locator}"]'
2832
if parent:
29-
parent_data = f'Parent selector: ["{parent.locator_type}": "{parent.locator}"]'
30-
current_data = f'{current_data}. {parent_data}'
31-
return current_data
33+
container_data = get_element_info(parent, is_for_container=True)
34+
selector = f"{selector} <= {container_data}"
35+
36+
return selector
3237

3338
@lru_cache(maxsize=16)
3439
def get_static(cls: Any):
@@ -63,13 +68,12 @@ def _repr_builder(self: Any):
6368
parent_class = self.parent.__class__.__name__ if parent else None
6469
locator_holder = getattr(self, 'anchor', self)
6570

66-
locator = f'locator="{locator_holder.locator}", '
67-
locator_type = f'locator_type="{locator_holder.locator_type}", ' if locator_holder.locator_type else ''
71+
locator = f'locator="{locator_holder.log_locator}", '
6872
name = f'name="{self.name}", '
6973
parent = f'parent={parent_class}'
7074
driver = f'{self.driver_wrapper.label}={self.driver}'
7175

72-
base = f'{class_name}({locator}{locator_type}{name}{parent}) at {obj_id}'
76+
base = f'{class_name}({locator}{name}{parent}) at {obj_id}'
7377
additional_info = driver
7478
return f'{base}, {additional_info}'
7579
except AttributeError:

mops/mixins/objects/locator.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Optional, Any, Union
2+
from typing import Optional
33

44

55
@dataclass
@@ -11,13 +11,6 @@ class Locator:
1111
All: The default locator for the object, used by default if no other locators are specified
1212
or if no specific platform/device type is detected.
1313
"""
14-
loc_type: Optional[str] = None
15-
"""
16-
Selenium & Appium only: Specifies the locator type
17-
(e.g., :obj:`~selenium.webdriver.common.by.By.CSS_SELECTOR`, :obj:`~selenium.webdriver.common.by.By.XPATH`, etc.).
18-
19-
If not provided, the locator type is automatically determined based on the given locator.
20-
"""
2114

2215
desktop: Optional[str] = None
2316
"""
@@ -47,13 +40,3 @@ class Locator:
4740
Appium only: The locator specifically for Android devices,
4841
allowing for targeting locators specific to Android applications.
4942
"""
50-
51-
52-
def take_locator_type(locator: Union[Locator, str]) -> Union[str, None]:
53-
"""
54-
Safe take locator type from given object.
55-
56-
:param locator: a Locator object or string containing locator.
57-
:return: locator type if given arg is Locator object.
58-
"""
59-
return locator.loc_type if type(locator) is Locator else None

mops/playwright/play_element.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from mops.mixins.objects.size import Size
1515
from mops.mixins.objects.location import Location
16-
from mops.utils.selector_synchronizer import get_platform_locator, get_playwright_locator
16+
from mops.utils.selector_synchronizer import get_platform_locator, set_playwright_locator
1717
from mops.abstraction.element_abc import ElementABC
1818
from mops.exceptions import TimeoutException
1919
from mops.utils.logs import Logging
@@ -35,14 +35,12 @@ class PlayElement(ElementABC, Logging, ABC):
3535
parent: Union[ElementABC, PlayElement]
3636
_element: Locator = None
3737

38-
def __init__(self, locator: str): # noqa
38+
def __init__(self): # noqa
3939
"""
4040
Initializing of web element with playwright driver
41-
42-
:param locator: anchor locator of page. Can be defined without locator_type
4341
"""
44-
self.locator = get_playwright_locator(get_platform_locator(self))
45-
self.locator_type = None
42+
self.locator = get_platform_locator(self)
43+
set_playwright_locator(self)
4644

4745
# Element
4846

mops/selenium/elements/mobile_element.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22

33
import time
44
from abc import ABC
5-
from typing import Union
65

76
from PIL.Image import Image
87

98
from mops.selenium.core.core_element import CoreElement
109
from mops.mixins.objects.location import Location
11-
from mops.mixins.objects.locator import Locator, take_locator_type
1210
from mops.mixins.objects.size import Size
1311
from mops.utils.internal_utils import calculate_coordinate_to_click
14-
from mops.utils.selector_synchronizer import get_platform_locator, get_selenium_locator_type, get_appium_selector
12+
from mops.utils.selector_synchronizer import get_platform_locator, set_selenium_selector, set_appium_selector
1513

1614

1715
class MobileElement(CoreElement, ABC):
1816

19-
def __init__(self, locator: Union[Locator, str]):
17+
def __init__(self):
2018
"""
2119
Initializing of mobile element with appium driver
22-
23-
:param locator: anchor locator of page. Can be defined without locator_type
2420
"""
2521
self.locator = get_platform_locator(self)
26-
locator_type = take_locator_type(locator) or get_selenium_locator_type(self.locator)
27-
self.locator, self.locator_type = get_appium_selector(self.locator, locator_type)
22+
set_selenium_selector(self)
23+
set_appium_selector(self)
2824

2925
def click_outside(self, x: int = -5, y: int = -5) -> MobileElement:
3026
"""

mops/selenium/elements/web_element.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
from __future__ import annotations
22

33
from abc import ABC
4-
from typing import Union
54

65
from mops.selenium.core.core_element import CoreElement
76
from mops.js_scripts import js_click
8-
from mops.mixins.objects.locator import take_locator_type, Locator
97
from mops.utils.internal_utils import calculate_coordinate_to_click
10-
from mops.utils.selector_synchronizer import get_platform_locator, get_selenium_locator_type
8+
from mops.utils.selector_synchronizer import get_platform_locator, set_selenium_selector
119

1210

1311
class WebElement(CoreElement, ABC):
1412

15-
def __init__(self, locator: Union[Locator, str]):
13+
def __init__(self):
1614
"""
1715
Initializing of web element with selenium driver
18-
19-
:param locator: anchor locator of page. Can be defined without locator_type
2016
"""
2117
self.locator = get_platform_locator(self)
22-
self.locator_type = take_locator_type(locator) or get_selenium_locator_type(self.locator)
18+
set_selenium_selector(self)
2319

2420
def click(self, *, force_wait: bool = True, **kwargs) -> WebElement:
2521
"""

0 commit comments

Comments
 (0)