diff --git a/README.md b/README.md index 6de6e78..f50bb5d 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/widgetastic_patternfly5/__init__.py b/src/widgetastic_patternfly5/__init__.py index b192e69..a191aaa 100644 --- a/src/widgetastic_patternfly5/__init__.py +++ b/src/widgetastic_patternfly5/__init__.py @@ -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 @@ -125,6 +127,9 @@ "Slider", "SplitButtonDropdown", "StandAloneChipGroup", + "Radio", + "Switch", + "SwitchDisabled", "Tab", "Title", ] diff --git a/src/widgetastic_patternfly5/components/switch.py b/src/widgetastic_patternfly5/components/switch.py new file mode 100644 index 0000000..fefbf56 --- /dev/null +++ b/src/widgetastic_patternfly5/components/switch.py @@ -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 diff --git a/src/widgetastic_patternfly5/ouia.py b/src/widgetastic_patternfly5/ouia.py index f1e9875..b74e7a7 100644 --- a/src/widgetastic_patternfly5/ouia.py +++ b/src/widgetastic_patternfly5/ouia.py @@ -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 @@ -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" @@ -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" @@ -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" diff --git a/testing/components/test_switch.py b/testing/components/test_switch.py new file mode 100644 index 0000000..397390b --- /dev/null +++ b/testing/components/test_switch.py @@ -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() diff --git a/testing/ouia/test_switch_ouia.py b/testing/ouia/test_switch_ouia.py new file mode 100644 index 0000000..d268d79 --- /dev/null +++ b/testing/ouia/test_switch_ouia.py @@ -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()