Skip to content

Commit

Permalink
Merge pull request #11 from LightOfHeaven1994/switch-widget
Browse files Browse the repository at this point in the history
Switch widget
  • Loading branch information
digitronik authored Aug 24, 2023
2 parents aa6cce4 + 5f835f1 commit f95fdaa
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ itteration of [widgetastic.patternfly4](https://github.com/RedHatQE/widgetastic.
- forms
- [radio](https://www.patternfly.org/components/forms/radio)
- [progress](https://www.patternfly.org/components/progress)
- [switch](https://www.patternfly.org/components/switch)
- [table](https://www.patternfly.org/components/table)
- [tabs](https://www.patternfly.org/components/tabs)
- [title](https://www.patternfly.org/components/title)
Expand Down
5 changes: 5 additions & 0 deletions src/widgetastic_patternfly5/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
from .components.progress import Progress
from .components.slider import InputSlider
from .components.slider import Slider
from .components.switch import Switch
from .components.switch import SwitchDisabled
from .components.table import ColumnNotExpandable
from .components.table import CompoundExpandableTable
from .components.table import ExpandableTable
Expand Down Expand Up @@ -125,6 +127,9 @@
"Slider",
"SplitButtonDropdown",
"StandAloneChipGroup",
"Radio",
"Switch",
"SwitchDisabled",
"Tab",
"Title",
]
34 changes: 34 additions & 0 deletions src/widgetastic_patternfly5/components/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from widgetastic.widget import GenericLocatorWidget


class SwitchDisabled(Exception):
pass


class BaseSwitch:
"""Represents the Patternfly Switch.
https://www.patternfly.org/components/switch
"""

CHECKBOX_LOCATOR = "./input"

@property
def is_enabled(self):
"""Returns a boolean detailing if the switch is enabled."""
return self.browser.get_attribute("disabled", self.CHECKBOX_LOCATOR) is None

def click(self):
"""Click on a Switch."""
if not self.is_enabled:
raise SwitchDisabled("{} is disabled".format(repr(self)))
else:
self.browser.click(self.CHECKBOX_LOCATOR)
return True

def __repr__(self):
return "{}({!r})".format(type(self).__name__, self.locator)


class Switch(BaseSwitch, GenericLocatorWidget):
pass
13 changes: 9 additions & 4 deletions src/widgetastic_patternfly5/ouia.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from widgetastic_patternfly5.components.navigation import BaseNavigation
from widgetastic_patternfly5.components.pagination import BaseCompactPagination
from widgetastic_patternfly5.components.pagination import BasePagination
from widgetastic_patternfly5.components.switch import BaseSwitch
from widgetastic_patternfly5.components.table import BaseExpandableTable
from widgetastic_patternfly5.components.table import BasePatternflyTable
from widgetastic_patternfly5.components.title import BaseTitle
Expand All @@ -37,6 +38,10 @@ class Card(BaseCard, OUIAGenericWidget):
OUIA_COMPONENT_TYPE = "PF5/Card"


class CheckboxMenu(BaseCheckboxMenu):
OUIA_COMPONENT_TYPE = "PF5/Menu"


class FormSelect(BaseFormSelect, OUIAGenericWidget):
OUIA_COMPONENT_TYPE = "PF5/FormSelect"

Expand All @@ -45,10 +50,6 @@ class Menu(BaseMenu):
OUIA_COMPONENT_TYPE = "PF5/Menu"


class CheckboxMenu(BaseCheckboxMenu):
OUIA_COMPONENT_TYPE = "PF5/Menu"


class Modal(BaseModal, OUIAGenericView):
OUIA_COMPONENT_TYPE = "PF5/ModalContent"

Expand Down Expand Up @@ -100,6 +101,10 @@ class ExpandableTable(BaseExpandableTable, PatternflyTable):
pass


class Switch(BaseSwitch, OUIAGenericWidget):
OUIA_COMPONENT_TYPE = "PF5/Switch"


class Title(BaseTitle, OUIAGenericWidget):
OUIA_COMPONENT_TYPE = "PF5/Title"

Expand Down
32 changes: 32 additions & 0 deletions testing/components/test_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from widgetastic.widget import View

from widgetastic_patternfly5 import Switch
from widgetastic_patternfly5 import SwitchDisabled

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


@pytest.fixture
def view(browser):
class TestView(View):
switch = Switch(locator='.//label[@for="simple-switch"]')
disabled_switch_on = Switch(locator='.//label[@for="disabled-switch-on"]')

return TestView(browser)


def test_switch_is_displayed(view):
assert view.switch.is_displayed
assert view.disabled_switch_on.is_displayed


def test_switch_is_enabled(view):
assert view.switch.is_enabled
assert not view.disabled_switch_on.is_enabled


def test_switch_click(view):
assert view.switch.click()
with pytest.raises(SwitchDisabled):
view.disabled_switch_on.click()
26 changes: 26 additions & 0 deletions testing/ouia/test_switch_ouia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from widgetastic.widget import View

from widgetastic_patternfly5.ouia import Switch as SwitchOUIA

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


@pytest.fixture
def view(browser):
class TestView(View):
switch = SwitchOUIA("BasicSwitch")

return TestView(browser)


def test_switch_is_displayed(view):
assert view.switch.is_displayed


def test_switch_is_enabled(view):
assert view.switch.is_enabled


def test_switch_click(view):
assert view.switch.click()

0 comments on commit f95fdaa

Please sign in to comment.