Skip to content

Commit

Permalink
[#505] REFACTOR: fix broken resolution of descriptors...
Browse files Browse the repository at this point in the history
... built on top of other ones, that were not accessed yet
  • Loading branch information
yashaka committed Jul 21, 2024
1 parent 103a8d9 commit 6e1fcc4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
22 changes: 20 additions & 2 deletions selene/support/_pom.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Element: # todo: consider implementing LocationContext interface
def __init__(self, selector: str | Tuple[str, str], _context=None):
self.__selector = selector

# todo: should we wrap lambda below into lru_cache?
self.__context = lambda instance: (
(_context(instance) if callable(_context) else _context)
or getattr( # todo: refactor to one-liner via helper
Expand Down Expand Up @@ -98,10 +99,27 @@ def within_browser(self):
)

def Element(self, selector: str | Tuple[str, str]) -> Element:
return Element(selector, _context=self)
# return Element(selector, _context=self)
return Element(
selector,
_context=lambda instance: ( # todo: should we lru_cache it?
getattr(instance, self.__name), # ← resolving descriptors chain
self, # before returning the actual context :P
# otherwise any descriptor built on top of previously defined
# can be resolved improperly, because previous one
# might be not accessed yet, thus we have to simulate such assess
# on our own by forcing getattr
)[1],
)

def All(self, selector: str | Tuple[str, str]) -> All:
return All(selector, _context=self)
return All(
selector,
_context=lambda instance: (
getattr(instance, self.__name),
self,
)[1],
)

# --- Descriptor --- #

Expand Down
39 changes: 39 additions & 0 deletions tests/examples/pom/test_material_ui__react_x_data_grid__mit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from selene import browser, have
from selene.support._pom import Element, All


class DataGridMIT:
grid = Element('[role=grid]')

footer = Element('.MuiDataGrid-footerContainer')
pagination = footer.Element('.MuiTablePagination-root')
pagination_rows_displayed = pagination.Element('.MuiTablePagination-displayedRows')
page_to_right = pagination.Element('[data-testid=KeyboardArrowRightIcon]')
page_to_left = pagination.Element('[data-testid=KeyboardArrowLeftIcon]')

def __init__(self, context):
self.context = context


@pytest.mark.parametrize(
'characters',
[
DataGridMIT(browser.element('#DataGridDemo+* .MuiDataGrid-root')),
# DataGridMIT.by_id('demo-simple-select'),
],
)
def test_material_ui__react_x_data_grid_mit(characters):
browser.driver.refresh()

# WHEN
browser.open('https://mui.com/x/react-data-grid/#DataGridDemo')

# THEN
characters.pagination_rows_displayed.should(have.exact_text('1–5 of 9'))
characters.page_to_right.click()
characters.pagination_rows_displayed.should(have.exact_text('6–9 of 9'))
characters.page_to_left.click()
characters.pagination_rows_displayed.should(have.exact_text('1–5 of 9'))
characters.footer.should(have.text('1–5 of 9'))

0 comments on commit 6e1fcc4

Please sign in to comment.