Skip to content

Commit

Permalink
Updating switch widget class with generic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurilahane committed Feb 28, 2024
1 parent e0cae05 commit 030615b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/widgetastic_patternfly5/components/switch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from widgetastic.exceptions import NoSuchElementException
from widgetastic.widget import GenericLocatorWidget


Expand All @@ -12,6 +13,8 @@ class BaseSwitch:
"""

CHECKBOX_LOCATOR = "./input"
LABEL_ON = "./span[contains(@class, 'pf-m-on')]"
LABEL_OFF = "./span[contains(@class, 'pf-m-off')]"

@property
def is_enabled(self):
Expand All @@ -26,6 +29,39 @@ def click(self):
self.browser.click(self.CHECKBOX_LOCATOR)
return True

@property
def selected(self):
"""Returns a boolean detailing if the Switch is on (True) of off (False)."""
return self.browser.get_attribute("checked", self.CHECKBOX_LOCATOR) is not None

def fill(self, value):
"""Fills a Switch with the supplied value."""
if not self.is_enabled:
raise SwitchDisabled("{} is disabled".format(repr(self)))
if bool(value) == self.selected:
return False
else:
self.browser.click(self.CHECKBOX_LOCATOR)
return True

@property
def label(self):
"""Returns the label of the Switch."""
if self.selected:
return self._read_locator(self.LABEL_ON)
else:
return self._read_locator(self.LABEL_OFF)

def read(self):
"""Returns a boolean detailing if the Switch is on (True) of off (False)."""
return self.selected

def _read_locator(self, locator):
try:
return self.browser.text(locator)
except NoSuchElementException:
return None

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

Expand Down
59 changes: 55 additions & 4 deletions testing/components/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,73 @@
def view(browser):
class TestView(View):
switch = Switch(locator='.//label[@for="simple-switch"]')
no_label_switch = Switch(locator='.//label[@for="no-label-switch-on"]')
disabled_switch_on = Switch(locator='.//label[@for="disabled-switch-on"]')
disabled_switch_off = Switch(locator='.//label[@for="disabled-switch-off"]')
disabled_no_label_switch_on = Switch(locator='.//label[@for="disabled-no-label-switch-on"]')
disabled_no_label_switch_off = Switch(
locator='.//label[@for="disabled-no-label-switch-off"]'
)

return TestView(browser)


def test_switch_is_displayed(view):
assert view.switch.is_displayed
assert view.no_label_switch.is_displayed
assert view.disabled_switch_on.is_displayed
assert view.disabled_switch_off.is_displayed
assert view.disabled_no_label_switch_on.is_displayed
assert view.disabled_no_label_switch_off.is_displayed


def test_switch_is_enabled(view):
assert view.switch.is_enabled
assert view.no_label_switch.is_enabled
assert not view.disabled_switch_on.is_enabled
assert not view.disabled_switch_off.is_enabled
assert not view.disabled_no_label_switch_on.is_enabled
assert not view.disabled_no_label_switch_off.is_enabled


def test_switch_click(view):
assert view.switch.click()
with pytest.raises(SwitchDisabled):
view.disabled_switch_on.click()
def test_switch_label(view):
assert view.switch.label == "Message when on"
assert view.no_label_switch.label is None
assert view.disabled_switch_on.label == "Message when on"
assert view.disabled_switch_off.label == "Message when off"
assert view.disabled_no_label_switch_on.label is None
assert view.disabled_no_label_switch_off.label is None


def test_switch_selected(view):
assert view.switch.read()
assert view.no_label_switch.read()
assert view.disabled_switch_on.read()
assert not view.disabled_switch_off.read()
assert view.disabled_no_label_switch_on.read()
assert not view.disabled_no_label_switch_off.read()


def test_switch_fill(view):
assert view.switch.selected
assert view.switch.label == "Message when on"
assert not view.switch.fill(True)
assert view.switch.selected
assert view.switch.label == "Message when on"
assert view.switch.fill(False)
assert not view.switch.selected
assert view.switch.label == "Message when off"
assert view.switch.fill(True)
assert view.switch.selected
assert view.switch.label == "Message when on"


def test_switch_fill_disabled(view):
for disabled_switch in (
view.disabled_switch_on,
view.disabled_switch_off,
view.disabled_no_label_switch_on,
view.disabled_no_label_switch_off,
):
with pytest.raises(SwitchDisabled):
disabled_switch.fill(True)

0 comments on commit 030615b

Please sign in to comment.