Skip to content

Commit

Permalink
[#505] REFACTOR: _pom.element over Element, _pom.all_ over All
Browse files Browse the repository at this point in the history
... for lesser interference with selene.Element, etc.
  • Loading branch information
yashaka committed Jul 22, 2024
1 parent a8115bb commit a339a5e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 38 deletions.
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
17 changes: 9 additions & 8 deletions selene/support/_pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -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):
Expand All @@ -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?
(
Expand All @@ -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: (
(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions tests/examples/pom/test_material_ui__react_select.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/element__perform__drag_and_drop_to_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/core/_pom_test.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit a339a5e

Please sign in to comment.