From a339a5e569f6b555b2be661b128378cfb8171d04 Mon Sep 17 00:00:00 2001 From: yashaka Date: Mon, 22 Jul 2024 22:19:00 +0300 Subject: [PATCH] [#505] REFACTOR: _pom.element over Element, _pom.all_ over All ... for lesser interference with selene.Element, etc. --- CHANGELOG.md | 4 ---- selene/support/_pom.py | 17 +++++++++-------- .../pom/test_material_ui__react_select.py | 10 +++++----- ...ct__explicit_PO_browser_based__with_alias.py | 10 +++++----- ...cit_PO_browser_based__with_lambda_on_self.py | 10 +++++----- .../test_material_ui__react_x_data_grid__mit.py | 6 +++--- .../element__perform__drag_and_drop_to_test.py | 10 +++++----- tests/unit/core/_pom_test.py | 6 +++--- 8 files changed, 35 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 733c9cee..69379866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -170,10 +170,6 @@ check vscode pylance, mypy, jetbrains qodana... #### TODO: decide on lru_cache vs set attr on instance... -#### TODO: consider _pom.element over _pom.Element, and _pom.all_ over _pom.All - -maybe as aliases... - ### Deprecated conditions - `be.present` in favor of `be.present_in_dom` diff --git a/selene/support/_pom.py b/selene/support/_pom.py index f2cd4f4b..eb5b5db6 100644 --- a/selene/support/_pom.py +++ b/selene/support/_pom.py @@ -48,7 +48,8 @@ # > Inside + InsideAll # > InnerElement # > The -class Element: # todo: consider implementing LocationContext interface +# todo: consider having _Element(selector, _context) as class, then element(selector) as alias +class element: # todo: consider implementing LocationContext interface def __init__(self, selector: str | Tuple[str, str], _context=None): # TODO: should we set _name on init too? # at least to None, if value was not provided... @@ -88,7 +89,7 @@ def _as_context(self, instance) -> selene.Element: ) def within(self, context, /): - return Element(self._selector, _context=context) + return element(self._selector, _context=context) @property def within_browser(self): @@ -111,8 +112,8 @@ def within_browser(self): ) ) - def element(self, selector: str | Tuple[str, str]) -> Element: - return Element( + def element(self, selector: str | Tuple[str, str]) -> element: + return element( selector, _context=lambda instance: ( # todo: should we lru_cache it? ( @@ -129,8 +130,8 @@ def element(self, selector: str | Tuple[str, str]) -> Element: ), ) - def all(self, selector: str | Tuple[str, str]) -> All: - return All( + def all(self, selector: str | Tuple[str, str]) -> all_: + return all_( selector, _context=lambda instance: ( ( @@ -159,7 +160,7 @@ def __get__(self, instance, owner): # but had to store it on instance... -class All: +class all_: def __init__(self, selector: str | Tuple[str, str], _context=None): self._selector = selector @@ -196,7 +197,7 @@ def _as_context(self, instance) -> selene.Collection: ) def within(self, context, /): - return All(self._selector, _context=context) + return all_(self._selector, _context=context) # todo: think on better name... within_page? @property diff --git a/tests/examples/pom/test_material_ui__react_select.py b/tests/examples/pom/test_material_ui__react_select.py index 03fb46dd..78a4a700 100644 --- a/tests/examples/pom/test_material_ui__react_select.py +++ b/tests/examples/pom/test_material_ui__react_select.py @@ -1,14 +1,14 @@ import pytest from selene import browser, have -from selene.support._pom import Element, All +from selene.support._pom import element, all_ class MUIBasicSelect: - label = Element('label') - selected_text = Element('.MuiSelect-select') - input = Element('input') - items = All('[role=option]').within(browser) + label = element('label') + selected_text = element('.MuiSelect-select') + input = element('input') + items = all_('[role=option]').within(browser) def __init__(self, context): self.context = context diff --git a/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_alias.py b/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_alias.py index 02e1861b..f20899ce 100644 --- a/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_alias.py +++ b/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_alias.py @@ -1,14 +1,14 @@ import pytest from selene import browser, have -from selene.support._pom import Element, All +from selene.support._pom import element, all_ class MUIBasicSelect: - label = Element('label') - selected_text = Element('.MuiSelect-select') - input = Element('input') - items = All('[role=option]').within_browser + label = element('label') + selected_text = element('.MuiSelect-select') + input = element('input') + items = all_('[role=option]').within_browser def __init__(self, context, browser): # noqa self.browser = browser diff --git a/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_lambda_on_self.py b/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_lambda_on_self.py index 3f0a9250..bdc2dae8 100644 --- a/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_lambda_on_self.py +++ b/tests/examples/pom/test_material_ui__react_select__explicit_PO_browser_based__with_lambda_on_self.py @@ -1,14 +1,14 @@ import pytest from selene import browser, have -from selene.support._pom import Element, All +from selene.support._pom import element, all_ class MUIBasicSelect: - label = Element('label') - selected_text = Element('.MuiSelect-select') - input = Element('input') - items = All('[role=option]').within(lambda self: self.browser) + label = element('label') + selected_text = element('.MuiSelect-select') + input = element('input') + items = all_('[role=option]').within(lambda self: self.browser) def __init__(self, context, browser): # noqa self.browser = browser diff --git a/tests/examples/pom/test_material_ui__react_x_data_grid__mit.py b/tests/examples/pom/test_material_ui__react_x_data_grid__mit.py index 97dbb63c..27fe04a4 100644 --- a/tests/examples/pom/test_material_ui__react_x_data_grid__mit.py +++ b/tests/examples/pom/test_material_ui__react_x_data_grid__mit.py @@ -2,11 +2,11 @@ import selene from selene import browser, have, be, command, query -from selene.support._pom import Element, All +from selene.support._pom import element, all_ class DataGridMIT: - grid = Element('[role=grid]') + grid = element('[role=grid]') header = grid.element('.MuiDataGrid-columnHeaders') toggle_all_checkbox = header.element('.PrivateSwitchBase-input') @@ -17,7 +17,7 @@ class DataGridMIT: ''' column_headers = grid.all('[role=columnheader]') - footer = Element('.MuiDataGrid-footerContainer') + footer = element('.MuiDataGrid-footerContainer') selected_rows_count = footer.element('.MuiDataGrid-selectedRowCount') pagination = footer.element('.MuiTablePagination-root') pagination_rows_displayed = pagination.element('.MuiTablePagination-displayedRows') diff --git a/tests/integration/element__perform__drag_and_drop_to_test.py b/tests/integration/element__perform__drag_and_drop_to_test.py index cfcf81c2..8dbe6562 100644 --- a/tests/integration/element__perform__drag_and_drop_to_test.py +++ b/tests/integration/element__perform__drag_and_drop_to_test.py @@ -2,7 +2,7 @@ import selene from selene import command, be, have, query -from selene.support._pom import Element +from selene.support._pom import element from tests.integration.helpers.givenpage import GivenPage @@ -171,11 +171,11 @@ def open(self): # Example of the POM-like PageObject pattern class ReactContinuousSlider: - thumb = Element('.MuiSlider-thumb') + thumb = element('.MuiSlider-thumb') thumb_input = thumb.element('input') - volume_up = Element('[data-testid=VolumeUpIcon]') - volume_down = Element('[data-testid=VolumeDownIcon]') - rail = Element('.MuiSlider-rail') + volume_up = element('[data-testid=VolumeUpIcon]') + volume_down = element('[data-testid=VolumeDownIcon]') + rail = element('.MuiSlider-rail') def __init__(self, browser: Optional[selene.Browser]): self.browser = browser or selene.browser diff --git a/tests/unit/core/_pom_test.py b/tests/unit/core/_pom_test.py index 6431d200..8ba1d995 100644 --- a/tests/unit/core/_pom_test.py +++ b/tests/unit/core/_pom_test.py @@ -1,11 +1,11 @@ -from selene.support._pom import Element, All +from selene.support._pom import element, all_ def test__pom__element_is_unique_for_each_object(): class Page: selector = '.element' - element = Element(selector) - elements = All(selector) + element = element(selector) + elements = all_(selector) page1 = Page() page2 = Page()