Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding support for checkboxgroup component #439

Merged
merged 8 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions pytest_splunk_addon_ui_smartx/components/controls/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,38 @@ class Checkbox(BaseControl):

def __init__(self, browser, container, searchable=True):
super().__init__(browser, container)
self.elements.update(
{
"internal_container": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox": Selector(select=container.select + ' [data-test="switch"]'),
"checkbox_btn": Selector(
select=container.select + ' [data-test="button"][role="checkbox"]'
),
}
)
if container.by == "xpath":
self.elements.update(
{
"internal_container": Selector(
by=By.XPATH,
select=container.select + '//div[@data-test="switch"]',
),
"checkbox": Selector(
by=By.XPATH,
select=container.select + '//div[@data-test="switch"]',
),
"checkbox_btn": Selector(
by=By.XPATH,
select=container.select + '//button[@role="checkbox"]',
),
}
)
else:
self.elements.update(
{
"internal_container": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox": Selector(
select=container.select + ' [data-test="switch"]'
),
"checkbox_btn": Selector(
select=container.select
+ ' [data-test="button"][role="checkbox"]'
),
}
)

def toggle(self, max_attempts=5):
"""
Expand Down
157 changes: 157 additions & 0 deletions pytest_splunk_addon_ui_smartx/components/controls/checkboxgroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#
# Copyright 2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from selenium.webdriver.common.by import By

from ..base_component import Selector
from .base_control import BaseControl
from .button import Button
from .checkbox import Checkbox
from .textbox import TextBox


class CheckboxGroup(BaseControl):
"""
Entity_Component : CheckboxGroup

This class represents a group of checkboxes, allowing for expanding groups,
selecting/deselecting checkboxes, and setting or retrieving values.
Note: Please use xpath as locator for this component.
"""

def __init__(self, browser, container) -> None:
"""
Initializes the CheckboxGroup.

Args:
browser (Browser): The browser instance to interact with.
container (Selector): The container element that holds the checkbox group.
"""
super().__init__(browser, container)

def is_group_expanded(self, group_name: str) -> bool:
harshilgajera-crest marked this conversation as resolved.
Show resolved Hide resolved
"""
Checks if the specified group is expanded.

Args:
group_name (str): The name of the checkbox group to check.

Returns:
bool: True if the group is expanded, False otherwise.
"""
self.elements.update(
{
f"{group_name}_button": Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f'//span[text()="{group_name}"]/ancestor::button',
)
}
)
return (
getattr(self, f"{group_name}_button").get_attribute("aria-expanded")
== "true"
)

def select_checkbox_and_set_value(
mkolasinski-splunk marked this conversation as resolved.
Show resolved Hide resolved
self, checkbox_name: str, checkbox_value: str
) -> None:
"""
Selects a checkbox and sets a value for the checkbox.

Args:
checkbox_name (str): The name of the checkbox to select.
checkbox_value (str): The value to set for the checkbox.
"""
Checkbox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}']/parent::div",
),
).check()
TextBox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}' and @data-test='number']",
),
).set_value(checkbox_value)

def select(self, group_name: str, checkbox_name: str, checkbox_value: str) -> None:
"""
Expands a group and selects a checkbox, then sets the specified value.

Args:
group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to select.
checkbox_value (str): The value to set for the checkbox.
"""
self.expand_group(group_name)
self.select_checkbox_and_set_value(
checkbox_name=checkbox_name, checkbox_value=checkbox_value
)

def deselect(self, group_name: str, checkbox_name: str) -> None:
"""
Expands a group and deselects the specified checkbox.

Args:
group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to deselect.
"""
self.expand_group(group_name)
Checkbox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}']/parent::div",
),
).uncheck()

def get_checkbox_value(self, group_name: str, checkbox_name: str) -> str:
"""
Expands a group and retrieves the value of the specified checkbox.

Args:
group_name (str): The name of the group to expand.
checkbox_name (str): The name of the checkbox to retrieve the value from.

Returns:
str: The value of the checkbox.
"""
self.expand_group(group_name)
return TextBox(
self.browser,
Selector(
by=By.XPATH,
select=self.elements.get("container").select
+ f"//div[@data-test-field='{checkbox_name}' and @data-test='number']",
),
).get_value()

def expand_group(self, group_name: str) -> None:
mkolasinski-splunk marked this conversation as resolved.
Show resolved Hide resolved
"""
Expands the specified group if it is not already expanded.

Args:
group_name (str): The name of the group to expand.
"""
is_expanded = self.is_group_expanded(group_name)
if not is_expanded:
getattr(self, f"{group_name}_button").click()
10 changes: 9 additions & 1 deletion pytest_splunk_addon_ui_smartx/components/controls/textbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import platform

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from ..base_component import Selector
from .base_control import BaseControl
Expand All @@ -37,7 +38,14 @@ def __init__(self, browser, container, encrypted=False):
self.encrypted = encrypted
self.container = container
self.browser = browser
self.elements.update({"input": Selector(select=container.select + " input")})
if container.by == "xpath":
self.elements.update(
{"input": Selector(by=By.XPATH, select=container.select + "//input")}
)
else:
self.elements.update(
{"input": Selector(select=container.select + " input")}
)

def set_value(self, value):
"""
Expand Down
Loading
Loading