Skip to content

Commit

Permalink
Merge pull request #55 from jrusz/expandable-section
Browse files Browse the repository at this point in the history
Add Expandable section
  • Loading branch information
digitronik authored Mar 18, 2024
2 parents e593e08 + b2a38a2 commit 734784c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ itteration of [widgetastic.patternfly4](https://github.com/RedHatQE/widgetastic.
- [description-list](https://www.patternfly.org/components/description-list)
- [drawer](https://www.patternfly.org/components/drawer)
- [dual-list-selector](https://www.patternfly.org/components/dual-list-selector)
- [expandable-section](https://www.patternfly.org/components/expandable-section)
- forms
- [form-select](https://www.patternfly.org/components/forms/form-select)
- [radio](https://www.patternfly.org/components/forms/radio)
Expand Down
2 changes: 2 additions & 0 deletions src/widgetastic_patternfly5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .components.drawer import Drawer
from .components.dual_list_selector import DualListSelector
from .components.dual_list_selector import SearchDualListSelector
from .components.expandable_section import ExpandableSection
from .components.forms.form_select import FormSelect
from .components.forms.form_select import FormSelectDisabled
from .components.forms.form_select import FormSelectOptionDisabled
Expand Down Expand Up @@ -137,4 +138,5 @@
"SwitchDisabled",
"Tab",
"Title",
"ExpandableSection",
]
60 changes: 60 additions & 0 deletions src/widgetastic_patternfly5/components/expandable_section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from wait_for import wait_for_decorator
from widgetastic.widget import View


class ExpandableSection(View):
"""Represents the Patternfly Expandable section widget.
Expands itself automatically when any child widget gets accessed, ensuring
that the widget is visible.
https://www.patternfly.org/components/expandable-section
NOTE: When using this in your code you need to set the ROOT yourself!
Example code with a button underneath the section:
class MySection(ExpandableSection):
ROOT = ".//div[contains(@data-testid, 'my-expandable-section')]"
my-button = Button(locator=".//button")
After creating the view you can run `MySection.my-button.click()` and it
will automatically expand and click on your button.
"""

ROOT = './/div[contains(@class, "-c-expandable-section")]'
BUTTON_LOCATOR = ".//button"

@property
def is_expanded(self):
"""Returns a boolean."""
if self.browser.get_attribute("aria-expanded", self.BUTTON_LOCATOR) == "true":
return True
else:
return False

def click(self):
"""Clicks the expandable section button."""
return self.browser.click(self.BUTTON_LOCATOR)

def expand(self):
"""Expands the section (checks if not expanded already first)."""
if not self.is_expanded:

@wait_for_decorator(timeout=3)
def _click():
self.click()
return self.is_expanded

def collapse(self):
"""Collapses the section (checks if expanded already first)."""
if self.is_expanded:

@wait_for_decorator(timeout=3)
def _click():
self.click()
return not self.is_expanded

def child_widget_accessed(self, widget):
# Expand the section
self.expand()
28 changes: 28 additions & 0 deletions testing/components/test_expandable_section.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from widgetastic.widget import Text
from widgetastic.widget import View

from widgetastic_patternfly5 import ExpandableSection

TESTING_PAGE_URL = "https://patternfly-react-main.surge.sh/components/expandable-section"


class SectionView(View):
@View.nested
class basic(ExpandableSection):
text = Text(".//div[contains(@class, '-c-expandable-section__content')]")


def test_section_expansion(browser):
view = SectionView(browser)
view.basic.expand()
assert view.basic.is_expanded
view.basic.collapse()
assert not view.basic.is_expanded


def test_section_text(browser):
view = SectionView(browser)
assert view.basic.is_displayed
assert view.basic.text.is_displayed
assert view.basic.text.read() == "This content is visible only when the component is expanded."
view.basic.collapse()

0 comments on commit 734784c

Please sign in to comment.